Detailed explanation of @NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor annotations in Lombok

Since the project of Party B uses the lombok plug-in, I have studied and summarized the lombok plug-in annotations. If there is anything wrong, please correct me.

One, reference

https://projectlombok.org/features/constructor

2. Summary

annotation Constructor parameters nonnull check Remarks
@NoArgsConstructor No parameters no Available parameters
@RequiredArgsConstructor Uninitialized final fields, fields marked as nonnull Have You can use the parameter @RequiredArgsConstructor(staticName = "of") to privatize the constructor and provide a static function of to create a constructor.
@AllArgsConstrutor All fields have available parameters Have @AllArgsConstrutor(access = AccessLevel.PROTECTED) to control the access level of the constructor.

Three, detailed

@NoArgsConstructor

@NoArgsConstructor is
used to generate parameterless constructors. If the construction fails because the final field is not initialized, the compilation will fail. You can use the @NoArgsConstructor(force
= true) parameter to force the uninitialized fianl field to be assigned 0 / false / null. For constrained fields (such as @NonNull fields), no check will be performed. So please note that these constraints are invalid until these fields are properly initialized.

@RequiredArgsConstructor

@RequiredArgsConstructor
generates a constructor with one parameter for fields that require special treatment. All fianl fields that fail to be initialized, and fields that are marked as @NonNull and are not initialized at the time of declaration are the parameters of the constructor.
The order of the parameters is consistent with the order of the fields in the class. The field of @NonNull will also be checked for null values. If the verification rules are not met, a null pointer exception will be thrown.

@AllArgsConstructor

@AllArgsConstructor: Generate a constructor with 1 parameter for each field in the class. Fields marked as @Non Null will be checked for null values.

Four, case

Use lombok annotations

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.NonNull;

@RequiredArgsConstructor(staticName = "of")
//增加RequiredArgsConstructor注解,可以对description字段生成私有构造函数,同时提供一个静态函数of进行创建。
@AllArgsConstructor(access = AccessLevel.PROTECTED)
//增加了AllArgsConstructor注解,可以生成一个全参构造函数,访问权限为protected。
public class ConstructorExample<T> {
    
    
  private int x, y;
  @NonNull private T description;
  
  @NoArgsConstructor
  //静态内部类,创建一个无参构造,对于field字段不做校验。
  public static class NoArgsExample {
    
    
    @NonNull private String field;
  }
}

Do not use lombok annotations

public class ConstructorExample<T> {
    
    
  private int x, y;
  @NonNull private T description;
  
  //创建了私有构造方法,参数为description,并对其进行非null校验。然后创建了静态of方法创建构造方法。
  private ConstructorExample(T description) {
    
    
    if (description == null) throw new NullPointerException("description");
    this.description = description;
  }
  
  public static <T> ConstructorExample<T> of(T description) {
    
    
    return new ConstructorExample<T>(description);
  }
  
  @java.beans.ConstructorProperties({
    
    "x", "y", "description"})
  //按参数顺序创建构造了函数,并对description字段做了校验。
  protected ConstructorExample(int x, int y, T description) {
    
    
    if (description == null) throw new NullPointerException("description");
    this.x = x;
    this.y = y;
    this.description = description;
  }
  
  public static class NoArgsExample {
    
    
    @NonNull private String field;
    //静态内部类的无参构造
    public NoArgsExample() {
    
    
    }
  }
}

Guess you like

Origin blog.csdn.net/weixin_44159662/article/details/109990456