[JAVA] Lombok ① Introduction

What is Lombok

It is a library that automatically generates redundant descriptions when creating Java classes at compile time.

For example, the following class

DateTime.java


public class DateTime
{
    private LocalDateTime value = LocalDateTime.MIN;

    public DateTime()
    {
    }

    public DateTime( LocalDateTime value )
    {
        this.value = value;
    }

    public boolean isEmpty()
    {
        return value == null;
    }

    public boolean isNotEmpty()
    {
        return !isEmpty();
    }
    
    public String format( String pattern )
    {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern( pattern);
        return formatter.format( value );
    }
    
    public LocalDateTime getValue()
    {
        return value;
    }

    public void setValue( LocalDateTime value )
    {
        this.value = value;
    }

    @Override
    public String toString()
    {
        String format = "Person(name=%s)";
        return String.format(format, value);
    }

}

It can be refactored as follows.

DateTime.java


@AllArgsConstructor
@NoArgsConstructor
@toString
public class DateTime
{
    @Getter
    @Setter
    private LocalDateTime value = LocalDateTime.MIN;

    public boolean isEmpty()
    {
        return value == null;
    }

    public boolean isNotEmpty()
    {
        return !isEmpty();
    }    
    
    public String format( String pattern )
    {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern( pattern);
        return formatter.format( value );
    }
}

further

DateTime.java


@AllArgsConstructor
@NoArgsConstructor
@Data
public class DateTime
{
    private LocalDateTime value = LocalDateTime.MIN;

    public boolean isEmpty()
    {
        return value == null;
    }

    public boolean isNotEmpty()
    {
        return !isEmpty();
    }    
    
    public String format( String pattern )
    {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern( pattern);
        return formatter.format( value );
    }}

Can be omitted. * @Data means a composite of multiple annotations such as @Getter and @Setter

It's refreshing because there are only meaningful methods in the class.

There are various other annotations, but

If you are at the introductory level, if you hold down the above, I think that it is okay for the time being.

Introduction

Development environment

1-a. Added dependency in provided scope (only for gradle 2.11 and below)

lombok generates each method according to the annotation at compile time, If it is gradle 2.11 or less, you need to add provided Configurations by yourself.

build.gradle


...
configurations {
    provided
}
...
dependencies {        
...
    provided "org.projectlombok:lombok:1.16.4"
...
}

In addition, when developing with Eclipse, the following description is also required.

build.gradle


eclipse {
    classpath {
        plusConfigurations += [configurations.provided]
        noExportConfigurations += [configurations.provided]
    }
}

1-b. Add dependency with war plugin (only for gradle 2.12 and above)

In 1-a, I defined providedConfigurations by myself, but by adding gradle war plugin, providedCompile can be used, so you can use it.

build.gradle


...
apply plugin: 'war'
...
dependencies {        
...
    providedCompile "org.projectlombok:lombok:1.16.4"
...
}

1-c. Add dependency with compileOnly (only for gradle 2.11 or lower)

From gradle 2.12, a scope called compileOnly, which is provided by default, has been added, so use that.

build.gradle


...

dependencies {        
...
    compileOnly "org.projectlombok:lombok:1.16.4"
...
}

2. Add lombok plugin to Eclipse

If you just add the dependency, the built app will work fine, but it will not be recognized in the IDE, so you need to add the lombok plugin. スクリーンショット 2017-03-25 20.29.31.png

Download the jar from the official website https://projectlombok.org/download.html

For Windows Double-click the downloaded jar to start the installer, so select the Eclipse exe file to complete the setting.

For Mac Copy the downloaded Jar to the following directory

cp ./lombok.jar /Applications/Eclipse.app/Contents/MacOS/lombok.jar

Add the following to /Applications/Eclipse.app/Contents/Eclipse/eclipse.ini

eclipse.ini


-Xbootclasspath/a:lombok.jar
-javaagent:lombok.jar

This will enable Lombok annotations in Eclipse.

スクリーンショット 2017-03-25 20.27.12.png

This makes it possible to reduce wasted coding time.

However, depending on the object, there are cases where it is better not to create a Setter, so let's use it properly and spend a comfortable Java life ♪

next time

Next, I would like to introduce annotations related to object creation.

Well then.

Recommended Posts

Lombok ① Introduction
Introduction (self-introduction)
Lombok memo
[Java] Introduction
Introduction (editing)
Try Lombok
Introduction to Ruby 2
Spring Fox ① Introduction
Ruby Learning # 1 Introduction
Rspec introduction memo_Rails
Introduction of pay.jp
Functional interface introduction
Introduction to SWING
Introduction of milkode
[Rails 6] cocoon_ introduction
About MacinCloud introduction
Introduction to web3j
Introduction to Micronaut 1 ~ Introduction ~
[Java] Introduction to Java
Introduction to migration
Introduction to java
Lombok ② Object generation
Introduction to Doma
Ractor super introduction