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
@Cleanup
callsclose()
methods, taking effect for objects implementingjava.io.Closeable
method.@Getter
,@Setter
@ToString
@EqualsAndHashCode
generatesequals()
andhashCode()
@NoArgsConstructor
,@RequiredArgsConstructor
and@AllArgsConstructor
generate constructors that take no fields,final
/@NonNull
fields or all fields.@Data
is a shortcut for@ToString
,@EqualsAndHashCode
,@Getter
on all fields,@Setter
on all non-final fields and@RequiredArgsConstrutor
.@Value
@Builder
builds types by Builder Pattern.@SneakyThrows
checks exceptions.@With
@Log
generates a logger field.