The article directories

  • 1. What is Lombok
  • 2. Lombok principle
  • 3. Lombok uses annotations
    • 1. @getter and @setter methods
    • 2. @ ToString method
    • 3. @EqualsAndHashCode
    • 4. @NonNull
    • @noargsconstructor and @requiredargsconstructor and @allargsconstructor
    • 6. @Data
    • 7. @Builder
    • 8. @Log
    • 9. val
    • 10. @Cleanup
  • 4. To summarize

1. What is Lombok

  • Liverpoolfc.tv: projectlombok.org/
  • Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java. Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.
  • In plain English: Lombok is a Java™ utility that helps developers eliminate verbose code in Java, especially for simple onesJava Objects (POJOs)It does this through annotations.
  • To simplify theJavaBeanThe development of
<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
</dependency>
Copy the code
  • Idea search for installationlombokThe plug-in

  • Lombok @Dataannotations
  • According to the shortcut keyAlt+7Displays all the methods in the current class, global constants, and methods including parameters and return values
  • Or View – >Tool Windows – >Structure – > click

2. Lombok principle

  • JSR 269: Pluggable Annotation Processing API
  • Website:www.jcp.org/en/jsr/deta…

    JDK6 provides a feature that leverages annotations at Javac compile time

  • View the compiledUser.class

  • Principle realization flowchart

3. Lombok uses annotations

1. @getter and @setter methods

  • test@Setterannotations

    User.java



    User.class

  • test@Getter(AccessLevel.PROTECTED)Annotation, the default ispublic, you can set the level of different methods





  • Add set and GET methods to all variables, annotating them directly in the class

  • Note:

    1. Lombok does not create set and get methods for static modified variables
    2. Lombok does not produce a set method for final modified characters



  • Custom variables do not generate set and GET methods



2. @ ToString method





  • Customize ToString methods that exclude certain fields



3. @EqualsAndHashCode

  • Customize EqualsAndHashCode methods that exclude certain fields

4. @NonNull

  • Purpose: The purpose of the annotation is to make a non-null judgment in the method
  • Case study:
public void test(@NonNull String s){
        System.out.println(s);
}
public static void main(String[] args) {
        User user = new User();
        user.test(null);
    }
Copy the code
  • Result: null pointer

  • View the compiled source code:
public void test(@NonNull String s) {
        if (s == null) {
            throw new NullPointerException("s is marked non-null but is null");
        } else{ System.out.println(s); }}Copy the code
  • You can also use it in constructors, or you can add annotations to fields
@NotNull private Integer id;
Copy the code

@noargsconstructor and @requiredargsconstructor and @allargsconstructor

  • NoArgsConstructor adds a no-argument constructor



  • RequiredArgsConstructor (); default will look for the identity@NonNullAdd a constructor to the variable, or identify itfinalModifies a variable without adding a constructor to its initial value



  • AllArgsConstructor generates the constructor for all member variables



6. @Data

  • Applied to a class, it is a collection of annotations: @toString, @equalSandHashCode, @getter, @setter @requiredargsConstructor

7. @Builder

  • Construct a series of methods with the field name method name, and the object returned by the method is UserBuilder





8. @Log

  • @log: applied to a class to generate a Log variable. There are different annotations for different logging implementations:



9. val

HashMap<String,String> map = new HashMap<String,String>();
Copy the code

The simplified way of writing it

val map = new HashMap<String,String>();
Copy the code
public static void main(String[] args) {
    val sets = new HashSet<String>();
    val lists = new ArrayList<String>();
    val maps = new HashMap<String, String>();
    //=> Equivalent to the following
    final Set<String> sets2 = new HashSet<>();
    final List<String> lists2 = new ArrayList<>();
    final Map<String, String> maps2 = new HashMap<>();
}
Copy the code

10. @Cleanup

  • Used to close and release resources that can be used on IO streams;
public class CleanupExample {
    public static void main(String[] args) throws IOException {
        @Cleanup InputStream in = new FileInputStream(args[0]);
        @Cleanup OutputStream out = new FileOutputStream(args[1]);
        byte[] b = new byte[10000];
        while (true) {
            int r = in.read(b);
            if (r == -1) break;
            out.write(b, 0, r); }}}Copy the code
  • The above is equivalent to traditional Java code like this:
public class CleanupExample {
  public static void main(String[] args) throws IOException {
    InputStream in = new FileInputStream(args[0]);
    try {
      OutputStream out = new FileOutputStream(args[1]);
      try {
        byte[] b = new byte[10000];
        while (true) {
          int r = in.read(b);
          if (r == -1) break;
          out.write(b, 0, r); }}finally {
        if(out ! =null) { out.close(); }}}finally {
      if(in ! =null) { in.close(); }}}}Copy the code

4. To summarize