Lombok
Overview¶
Project Lombok is a java library that automatically plugs into editors and build tools, spicing up Java.
Maven¶
After importing Lombok as following in Maven project, it will takes effect when compiling.
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
Support for IDE¶
Install plug-in Lombok.
Principle¶
Since Java 6, Java supports regulation of JSR 269 Pluggable Annotation Processing API which is called when javac is running. Its procedures are as follows:
- javac analyzes the source code and generates an Abstract Syntax Tree (AST).
- javac calls Lombok programs implementing JSR 269 API when compiling.
- Lombok deals with the AST above modifies its nodes by associate annotations.
- javac generates bytecode files based on the modified AST.
Usage¶
@NonNull@Cleanupcallsclose()methods, taking effect for objects implementingjava.io.Closeablemethod.@Getter,@Setter@ToString@EqualsAndHashCodegeneratesequals()andhashCode()@NoArgsConstructor,@RequiredArgsConstructorand@AllArgsConstructorgenerate constructors that take no fields,final/@NonNullfields or all fields.@Datais a shortcut for@ToString,@EqualsAndHashCode,@Getteron all fields,@Setteron all non-final fields and@RequiredArgsConstrutor.@Value@Builderbuilds types by Builder Pattern.@SneakyThrowschecks exceptions.@With@Loggenerates a logger field.