Liverpoolfc.tv: projectlombok.org/

The official document: projectlombok.org/api/lombok/…

References:

www.jianshu.com/p/2543c71a8…

Blog.csdn.net/zhaoyanjun6…

1 Lombok to introduce

== Java beans without Lombok :==

public class DepartMent {

    private String name;

    public DepartMent(a) {}public DepartMent(String name) {
        this.name = name;
    }

    public String getName(a) {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null|| getClass() ! = o.getClass())return false;
        DepartMent that = (DepartMent) o;
        return Objects.equals(name, that.name);
    }

    @Override
    public int hashCode(a) {
        return Objects.hash(name);
    }

    @Override
    public String toString(a) {
        return "DepartMent{" +
                "name='" + name + '\' ' +
                '} '; }}Copy the code

== Java beans using Lombok :==

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@EqualsAndHashCode
public class DepartMent {
    private String name;
}  
Copy the code

Conclusion:

Lombok was created to simplify Java’s Bean objects, leaving out a lot of repetitive, non-technical code, not only to make the code look clean, but also to focus on more important business code.

Preparation before use:

1 Install Lombok in plugins of IDEA and restart IDEA.

2 Add lombok coordinates

                <dependency>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok</artifactId>
                    <version>1.18.16</version>
                </dependency>
Copy the code

2 Lombok common annotation usage

1 @Data

@data annotation, getter methods for automatically generating object properties, setter methods, equals methods, hashCode methods, toString methods, no-argument constructor.

Great kid’s shoe reminder drops

@Data
public class DepartMent {
    private String name;
}  
Copy the code

Is equal to:

public class DepartMent {
    private String name;
    
 	public DepartMent(a) {}
    
    public String getName(a) {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null|| getClass() ! = o.getClass())return false;
        DepartMent that = (DepartMent) o;
        return Objects.equals(name, that.name);
    }

    @Override
    public int hashCode(a) {
        returnObjects.hash(name); }}Copy the code

2 @AllArgsConstructor

AllArgsConstructor annotation, a full-parameter construction of an object

@NoArgsConstructor
public class DepartMent {
    private String name;
}  
Copy the code

Is equal to:

public class DepartMent {
    private String name;

    public DepartMent(String name) {
        this.name = name; }}Copy the code

3 @NoArgsConstructor

@noargsconstructor annotation, a no-argument construction of an object

@NoArgsConstructor
public class DepartMent {
    private String name;
}  
Copy the code

Is equal to:

public class DepartMent {
    private String name;

    public DepartMent(a) {}}Copy the code

4 @ToString

@toString annotation, ToString method of an object

@ToString
public class DepartMent {
    private String name;
}  
Copy the code

Is equal to:

public class DepartMent {
    private String name;
    
    @Override
    public String toString(a) {
        return "DepartMent{" +
                "name='" + name + '\' ' +
                '} '; }}Copy the code

5 @EqualsAndHashCode

The @equalSandHashCode annotation, the equals and HashCode methods of the object.

@EqualsAndHashCode
public class DepartMent {
    private String name;
}  
Copy the code

Is equal to:

public class DepartMent {
    private String name;
    
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null|| getClass() ! = o.getClass())return false;
        DepartMent that = (DepartMent) o;
        return Objects.equals(name, that.name);
    }

    @Override
    public int hashCode(a) {
        returnObjects.hash(name); }}Copy the code

6 @Getter/@Setter

@getter / @setter annotations, get/set methods on objects

@Getter
@Setter  
public class DepartMent {
    private String name;
} 
Copy the code

Is equal to:

public class DepartMent {
    private String name;
    
    public String getName(a) {
        return name;
    }

    public void setName(String name) {
        this.name = name; }}Copy the code

7 @Slf4j

Public static final Logger log = loggerFactory.getLogger (class name);

@Slf4j 
public class DepartMent {
    private String name;
    
  public static void main(String[] args) {
        log.info("Log: {}" + "Hello World!"); }}Copy the code

Is equal to:

public class DepartMent {
    private String name;
    
    public static final Logger log = LoggerFactory.getLogger(DepartMent.class);
    
    public static void main(String[] args) {
        log.info("Log: {}" + "Hello World!"); }}Copy the code

8 @NonNull

@nonNULL annotation, an object’s member variables and arguments, indicating that they cannot be null, otherwise a null pointer exception will be thrown.

public class DepartMent {
    @NonNull
    private String name;
    
    public String getName(a) {
        return name;
    }

    public void setName(@NonNull String name) {
        this.name = name; }}Copy the code

9 @Value

The @value annotation makes the object final, defines the member variable as private final by default, and does not generate the set method.

@Value
public class DepartMent {
    private String name;
} 
Copy the code

Is equal to:

public final class DepartMent {
    private final String name;
} 
Copy the code

10 @Builder

The @Builder annotation adds the constructor pattern to the class

@Builder
public class DepartMent {
    private String name;
} 
Copy the code

Is equal to:

public class DepartMent {
  
   private String name;
    
   DepartMent(String name) {
        this.name = name;
    }
   public static DepartMent.DepartMentBuilder builder(a) {
        return new DepartMent.DepartMentBuilder();
    }
    
     public static class DepartMentBuilder {
   	 	private String name;
         DepartMentBuilder(){};
         
         public DepartMent.DepartMentBuilder name(String name) {
            this.name = name;
            return this;
        }
         
         public DepartMent build(a) {
            return new DepartMent(this.name);
        }
         public String toString(a) {
            return "DepartMent.DepartMentBuilder( name=" + this.name +")"; }}}Copy the code

11 @Synchronized

@synchronized annotation, add a lock

public class DepartMent {
   
    private String name;
    
    // Common method, equivalent to object lock
    @Synchronized
    void before(a) {
       // Code logic
    }
    
    // static method, equivalent to class lock
    @Synchronized
    static void after(a) {
       // Code logic}}Copy the code

Is equal to:

public class DepartMent {
    private final Object $lock = new Object[0];
    private static final Object $LOCK = new Object[0];
    private String name;
    

    void before(a) {
        Object var1 = this.$lock;
        synchronized(this.$lock) {
             // Code logic}}static void after(a) {
	    Object var0 = $LOCK;
        synchronized($LOCK) {
             // Code logic}}}Copy the code

12 @SneakyThrows

@sneakythrows annotation, which is equivalent to catching exceptions by try/catch.

public class DepartMent {
    private String name;
    
    @SneakyThrows
    public void init(a){
        int a = 1 / 0; }}Copy the code

Is equal to:

public class DepartMent {
    private String name;
    
    public void init(a){
        try{
        int a = 1 / 0;
        }catch(Exception e){
            / / processing}}}Copy the code

13 @Cleanup

The @cleanup annotation automatically closes resources, such as IO stream objects.

public class DepartMent {
    private String name;
    
    public void init(a){
       @Cleanup InputStream in = newInputStream(); }}Copy the code

Is equal to:

public class DepartMent {
    private String name;
    
    public void init(a){
        try{          
       		InputStream in = new InputStream();  
        }finally{ in.close(); }}Copy the code

14 @Accessors

Accessors, Accessors that control the form of getter and setter methods. Fluent The fluent default is false, if true, getter and setter methods are property names, and setter methods return the current object.

@Data
@Accessors(fluent = true)
public class DepartMent {
    private String name;
    
     public String name(a) {
        return name;
    }

    public DepartMent name(String name) {
       //Xxx}}Copy the code

Chain chain defaults to false, if true,setter methods return the current object.

@Data
@Accessors(chain = true)
public class DepartMent {
    private String name;
    
     public String getName(a) {
        return name;
    }

    public DepartMent setName(String name) {
       //Xxx}}Copy the code

Prefix Prefix is an empty character array by default. Custom prefixes (f below), getters and setters ignore prefixes before property names.

@Data
@Accessors(prefix = "f")
public class DepartMent {
    private String fname;
    
     public String name(a) {
        return name;
    }

    public void name(String name) {
       //Xxx}}Copy the code

Three principles of Lombok

Jdk1.6, Javac support JSR 269 Pluggable Annotation Processing API specification, program implementation of the specification, when javAC runs, can be called.

Lombok is the program that implements this specification. Workflow in the JAVAC process:

  • 1 javAC analyzes the source code and generates an abstract syntax tree (AST)

  • 2 Javac compilation calls Lombok programs

  • 3. The plugin processes the AST, finds the annotation location, modifies the syntax tree, and adds the annotation to define the tree node of the response

  • 4 Javac generates bytecode files from the modified syntax tree (AST)

4 Summary of Lombok

Opinions on Lombok’s use vary, with some for and some against. The emergence of new technology is bound to solve some problems in life, but also bring new problems.

The use of Lombok brings simplicity and beauty to the code, saving a lot of basic code. At the same time, it also brings many problems, such as the use of one person, the whole person must use; Project upgrade, can the new framework use this plug-in?

I believe the Lombok plugin will continue to grow in the future.

tips:

IDEA2020.3 has Lombok as a built-in plug-in. The 2.1.x version of SpringBoot has Lombok dependencies built into the Starter.