Lombok: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Overview= Lombok is a Java annotation library that helps reduce boilerplate code.")
 
 
(35 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
* https://projectlombok.org
* Latest Version: https://projectlombok.org/download
=Overview=
=Overview=


Lombok is a Java annotation library that helps reduce boilerplate code.
Lombok is a Java annotation library that helps reduce boilerplate code. It uses annotation processing as a bootstrapping mechanism to include itself into the compilation process and modify the [[Formal_Languages_and_Translators#.28Abstract.29_Syntax_Tree|AST]] via internal compiler APIs. <span id='Not_a_Typical_Use'></span>This is not a typical use of [[Java Annotation Processor|annotation processing]], which usually generate new files and do not change existing ones.
 
=Gradle Dependency=
 
<syntaxhighlight lang='groovy'>
dependencies {
 
    implementation "org.projectlombok:lombok:1.18.2"
}
</syntaxhighlight>
 
=Annotations=
 
==@Slf4j==
 
The annotation generates a SLF4J Logger in the class. It has the same effect as if you were to explicitly add the following lines within the class:
 
<syntaxhighlight lang='java'>
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MyClass.class);
</syntaxhighlight>
 
==@Data==
 
{{External|https://projectlombok.org/features/Data}}
 
Generates:
* Getters for all fields.
* Setters for all non-final fields.
* A constructor with required arguments: final fields and fields with constraints such as [[#.40NonNull|@NonNull]].
* A useful toString() method.
* hashCode() and equals() implementations that check all non-transient fields.
 
Equivalent to [[#@Getter|@Getter]] [[#@Setter|@Setter]], [[#@RequiredArgsConstructor|@RequiredArgsConstructor]] [[#@ToString|@ToString]] [[#@EqualsAndHashCode|@EqualsAndHashCode]]
 
==<span id='@Getter'></span><span id='@Setter'></span>@Getter/@Setter==
 
{{External|[https://projectlombok.org/features/GetterSetter Lombok Documentation for Getter/Setter]}}
 
By default, the getters/setters such generated are public. To change the access level:
 
<syntaxhighlight lang='java'>
@Setter(AccessLevel.PACKAGE)
public class ... {
  ...
}
</syntaxhighlight>
 
==Constructors==
 
{{External|[https://projectlombok.org/features/constructor Lombok Documentation for constructors]}}
===@AllArgsConstructor===
===@RequiredArgsConstructor===
 
Generates a constructor with required arguments. Required arguments are final fields and fields with constraints such as [[#.40NonNull|@NonNull]].
 
===@NoArgsConstructor===
 
<syntaxhighlight lang='java'>
@NoArgsConstructor(access=AccessLevel.PRIVATE, force=true)
</syntaxhighlight>
 
Generates a no-argument constructor and make it private.  This is usually applied to JPA [[@Entity]] classes.  If the class has final properties that must be set, use "force=true", which results in the Lombok-generated constructor setting them to <tt>null</tt>. Note that when you use @NoArgsConstructor, an argument constructor, even if possible, will not get added, so if you need it, you will need to explicitly use [[#.40RequiredArgsConstructor|@RequiredArgsConstructor]].
 
==@ToString==
 
==@EqualsAndHashCode==
 
==@NonNull==
 
==@Builder==
 
{{External|[https://projectlombok.org/features/Builder Lombok Documentation for @Builder]}}
{{External|[https://github.com/ovidiuf/playground/tree/master/lombok/Builder NOKB Example for @Builder]}}
 
=Troubleshooting=
 
==lombok.javac.apt.LombokProcessor could not be initialized. Lombok will not run during this compilation==
 
When using:
<syntaxhighlight lang='groovy'>
dependencies {
    implementation('org.projectlombok:lombok')
}
</syntaxhighlight>
we get:
warning: lombok.javac.apt.LombokProcessor could not be initialized. Lombok will not run during this compilation:
java.lang.ClassCastException: org.gradle.api.internal.tasks.compile.processing.IncrementalFiler cannot be cast to com.sun.tools.javac.processing.JavacFiler
 
Fixed by explicitly specifying the Lombok version:
<syntaxhighlight lang='groovy'>
dependencies {
    implementation('org.projectlombok:lombok:1.18.2')
}
</syntaxhighlight>

Latest revision as of 02:16, 13 March 2019

External

Overview

Lombok is a Java annotation library that helps reduce boilerplate code. It uses annotation processing as a bootstrapping mechanism to include itself into the compilation process and modify the AST via internal compiler APIs. This is not a typical use of annotation processing, which usually generate new files and do not change existing ones.

Gradle Dependency

dependencies {

    implementation "org.projectlombok:lombok:1.18.2"
}

Annotations

@Slf4j

The annotation generates a SLF4J Logger in the class. It has the same effect as if you were to explicitly add the following lines within the class:

private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MyClass.class);

@Data

https://projectlombok.org/features/Data

Generates:

  • Getters for all fields.
  • Setters for all non-final fields.
  • A constructor with required arguments: final fields and fields with constraints such as @NonNull.
  • A useful toString() method.
  • hashCode() and equals() implementations that check all non-transient fields.

Equivalent to @Getter @Setter, @RequiredArgsConstructor @ToString @EqualsAndHashCode

@Getter/@Setter

Lombok Documentation for Getter/Setter

By default, the getters/setters such generated are public. To change the access level:

@Setter(AccessLevel.PACKAGE)
public class ... {
  ...
}

Constructors

Lombok Documentation for constructors

@AllArgsConstructor

@RequiredArgsConstructor

Generates a constructor with required arguments. Required arguments are final fields and fields with constraints such as @NonNull.

@NoArgsConstructor

@NoArgsConstructor(access=AccessLevel.PRIVATE, force=true)

Generates a no-argument constructor and make it private. This is usually applied to JPA @Entity classes. If the class has final properties that must be set, use "force=true", which results in the Lombok-generated constructor setting them to null. Note that when you use @NoArgsConstructor, an argument constructor, even if possible, will not get added, so if you need it, you will need to explicitly use @RequiredArgsConstructor.

@ToString

@EqualsAndHashCode

@NonNull

@Builder

Lombok Documentation for @Builder
NOKB Example for @Builder

Troubleshooting

lombok.javac.apt.LombokProcessor could not be initialized. Lombok will not run during this compilation

When using:

dependencies {
    implementation('org.projectlombok:lombok')
}

we get:

warning: lombok.javac.apt.LombokProcessor could not be initialized. Lombok will not run during this compilation: 
java.lang.ClassCastException: org.gradle.api.internal.tasks.compile.processing.IncrementalFiler cannot be cast to com.sun.tools.javac.processing.JavacFiler

Fixed by explicitly specifying the Lombok version:

dependencies {
    implementation('org.projectlombok:lombok:1.18.2')
}