Lombok Brief Instructions

Lombok annotation online help documentation: http://projectlombok.org/features/index .

Common Lombok Annotations

annotation illustrate
@Data The annotation is on the class; it provides the getting and setting methods for all properties of the class, and also provides the equals, canEqual, hashCode, toString methods
@Value Used on classes, it is the immutable form of @Data, which is equivalent to adding final declarations to properties, providing only getter methods, not setter methods.
@Setter Annotations on classes and properties; provide setting methods for properties
@Getter Annotations on classes and properties; provide getters for properties
@NoArgsConstructor Annotation on the class; provides a no-argument constructor for the class
@AllArgsConstructor Annotated on the class; provides a full-parameter constructor for the class
@ToString Generate toString method
@Builder Used on classes, constructors, and methods to provide you with complex builder APIs
@SneakyThrows Automatically throw checked exceptions without explicitly using the throws statement on the method

log annotation

@Log: Different types of log objects are generated according to different annotations, but the instance names are all log, and there are six optional implementation classes.

annotation illustrate
@CommonsLog log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
@Log log = java.util.logging.Logger.getLogger(LogExample.class.getName());
@Log4j log = org.apache.log4j.Logger.getLogger(LogExample.class);
@Log4j2 log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
@ Slf4j log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
@ XSlf4j log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);

Lombok Principle

  Now that you've seen the simple usage, it's time to wonder how it's implemented. In the whole process of use, you only need to use annotations, and you don't need to do other extra work. The mystery should be in the analysis of annotations. 
   
While JDK5 introduces annotations, it also provides two parsing methods.

runtime parsing

  For annotations that can be parsed at runtime, @Retention must be set to RUNTIME, so that the annotation can be obtained through reflection. 
  

Compile-time parsing (used by Lombok)

There are two mechanisms for compile-time parsing:

1.Annotation Processing Tool

  apt was created in JDK5, and JDK7 has been marked as expired and deprecated. It has been completely removed in JDK8. Since JDK6, it can be replaced by the Pluggable Annotation Processing API. There are two main reasons for apt to be replaced:

  • The apis are all under the com.sun.mirror non-standard package
  • Not integrated into javac, need to run additionally

2.Pluggable Annotation Processing API

  JSR 269, added since JDK6, as an alternative to apt, it solves two problems of apt. When javac is executed, it will call the program that implements the API, so that we can make some enhancements to the compiler. At this time, javac The process of execution is as follows:

write picture description here

  Lombok is implemented in this way. If you are interested, you can go to the Lombok source code. The implementation of the corresponding annotation is in HandleXXX. For example, the implementation of the @Getter annotation is HandleGetter.handle(). There are some other class libraries implemented in this way, such as Google Auto, Dagger, etc. 
  

Lombok problem

  1. Overloading of multiple parameter constructors cannot be supported.
  2. Will reduce code readability.

Guess you like

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