Lombok application and analysis

Lombok is an application tool plugin for Java IDE, a tool that can help us simplify and eliminate some must-have but bloated Java code through simple annotations, such as property constructors, getters, setters, equals, hashcode, toString method. Combined with the IDE, by using the corresponding annotations, the corresponding methods can be generated when compiling the source code.

Although the above common methods can be generated by IDE, lombok is more concise and convenient. The effect that can be achieved is that there is no need to write some general methods in the source code, but the compiled bytecode files will help us generate these methods. , this is the magic of lombok.

Official address: https://projectlombok.org/

Which annotations are supported?

@Data

The annotation is on the class; it provides get and set methods for all properties of the class, and also provides equals, canEqual, hashCode, and toString methods.

@Setter @Getter

The annotation is on the property; the set method is provided for a single property; the annotation on the class provides the set method for all the properties of the class, and the default constructor is provided.

@ Slf4j

The annotation is on the class; provide a log object with a property named log for the class, and provide a default constructor.

@AllArgsConstructor

The annotation is on the class; a full-parameter constructor is provided for the class. After this annotation is added, the default constructor is not provided in the class.

@NoArgsConstructor

Annotation on a class; provides a no-argument constructor for the class.

@EqualsAndHashCode

Annotation on the class can generate equals, canEqual, hashCode methods.

@NonNull

Annotation on the property will automatically generate a non-null check for this parameter. If the parameter is empty, a null pointer exception will be thrown, and there will also be a default no-argument constructor.

@Cleanup

This annotation is used in front of a variable to ensure that the resource represented by this variable will be automatically closed. The default is to call the close() method of the resource. If the resource has other closing methods, you can use @Cleanup(“methodName”) to specify the call method, will also generate a default constructor

@ToString

This annotation is used on classes to generate toString methods for all parameters, as well as a default constructor.

@RequiredArgsConstructor

This annotation is used on classes to generate corresponding constructors using all member variables annotated with @NonNull or final-modified in the class.

@Value

This annotation is used on a class to generate a constructor with all parameters, a get method, and also provide equals, hashCode, and toString methods.

@SneakyThrows

This annotation is used on methods. You can wrap the code in the method with a try-catch statement, catch the exception, and use Lombok.sneakyThrow(e) to throw the exception in the catch. You can use the form of @SneakyThrows(Exception.class) Specifies which exception is thrown, and also generates a default constructor.

@Synchronized

This annotation is used on class methods or instance methods. The effect is the same as the synchronized keyword. The difference is that the lock objects are different. For class methods and instance methods, the lock objects of the synchronized keyword are the class object and this object of the class respectively, while @Synchronized The lock objects are the private static final object lock and the private final object lock, of course, you can also specify the lock object yourself, and also provide a default constructor.

Implement parsing

Lombok mainly takes effect through annotations. Annotations are introduced from jdk5, and there are two ways of parsing.

The first is runtime parsing, @Retention(RetentionPolicy.RUNTIME), which defines the retention policy of the annotation, so that the annotation can be obtained through reflection.

The other is compile-time parsing, which has two mechanisms.

Annotation Processing Tool,apt自JDK5产生,JDK7已标记为过期,不推荐使用,JDK8中已彻底删除,自JDK6开始,可以使用Pluggable Annotation Processing API来替换它,apt被替换主要有2点原因。api都在com.sun.mirror非标准包下,还有就是没有集成到javac中,需要额外运行。

Pluggable Annotation Processing API

lombok使用这种方式实现,基于JSR 269,自JDK6加入,作为apt的替代方案,它解决了apt的两个问题,javac在执行的时候会调用实现了该API的程序,这样我们就可以对编译器做一些增强。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325577708&siteId=291194637