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
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@toString
If you are at the introductory level, if you hold down the above, I think that it is okay for the time being.
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]
}
}
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"
...
}
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"
...
}
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.
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.
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, I would like to introduce annotations related to object creation.
Well then.
Recommended Posts