ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Lombok

2022-04-10 11:00:53  阅读:149  来源: 互联网

标签:lombok description int private Lombok public String


简介

Lombok项目是一个 Java 库,它会自动插入编辑器和构建工具中,Lombok 提供了一组有用的注释,用来消除Java类中的大量样板代码。

常用注解

@NonNull

可以在记录组件或方法或构造函数的参数上使用。lombok 会生成空检查语句。

lombok

import lombok.NonNull;

public class NonNullExample extends Something {
  private String name;
  
  public NonNullExample(@NonNull Person person) {
    super("Hello");
    this.name = person.getName();
  }
}

java

import lombok.NonNull;

public class NonNullExample extends Something {
  private String name;
  
  public NonNullExample(@NonNull Person person) {
    super("Hello");
    if (person == null) {
      throw new NullPointerException("person is marked @NonNull but is null");
    }
    this.name = person.getName();
  }
}

@Cleanup

可以使用它来确保在代码执行路径退出当前作用域之前自动清理给定资源。

lombok

import lombok.Cleanup;
import java.io.*;

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);
    }
  }
}

java

import java.io.*;

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();
      }
    }
  }
}

@Getter 和 @Setter

二者可以一起使用也可以只使用其中一个用来注释到对应的字段或者类上,二者默认生成的访问修饰符是 public 的,可以通过 AccessLevel 将其修改成其他访问修饰符。

ccessLevel

public enum AccessLevel {
    PUBLIC,
    MODULE,
    PROTECTED,
    PACKAGE,
    PRIVATE,
    NONE;

    private AccessLevel() {
    }
}

Lombok

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

public class GetterSetterExample {
  /**
   * 添加后自动生成 get/set 方法
   */
  @Getter @Setter private int age = 10;
  
  /**
   * 生成 set 方法时,指定其访问修饰符为 PROTECTED
   */
  @Setter(AccessLevel.PROTECTED) private String name;
    
  /**
   * 不生成 get/set 方法
   */
  @Getter(AccessLevel.NONE) @Setter (AccessLevel.NONE) private int id;
    
  public int getId() {
      return id;
  }

  public void setId(int id) {
      this.id = id;
  }
}

Java

public class GetterSetterExample {

  private int id;
  
  private int age = 10;

  private String name;
  
  public int getId() {
      return id;
  }

  public void setId(int id) {
      this.id = id;
  }
    
  public int getAge() {
    return age;
  }
  
  public void setAge(int age) {
    this.age = age;
  }
    
  protected void setName(String name) {
    this.name = name;
  }
}

@ToString

任何类定义都可以用 @ToString 注释,让 lombok 生成 toString() 方法的实现。默认情况下,它将按顺序打印 class 的名称以及每个字段,以逗号分隔。

默认情况下,将打印所有非静态字段。可以使用 exclude 将不想打印的字段剔除掉 @ToString(exclude = {"要剔除的字段"})

Lombok

import lombok.ToString;

@ToString
public class ToStringExample {
  private static final int STATIC_VAR = 10;
  private String name;
  private Shape shape = new Square(5, 10);
  private String[] tags;
  @ToString.Exclude private int id;
  
  public String getName() {
    return this.name;
  }
  
  @ToString(callSuper=true, includeFieldNames=true)
  public static class Square extends Shape {
    private final int width, height;
    
    public Square(int width, int height) {
      this.width = width;
      this.height = height;
    }
  }
}

java

import java.util.Arrays;

public class ToStringExample {
  private static final int STATIC_VAR = 10;
  private String name;
  private Shape shape = new Square(5, 10);
  private String[] tags;
  private int id;
  
  public String getName() {
    return this.name;
  }
  
  public static class Square extends Shape {
    private final int width, height;
    
    public Square(int width, int height) {
      this.width = width;
      this.height = height;
    }
    
    @Override public String toString() {
      return "Square(super=" + super.toString() + ", width=" + this.width + ", height=" + this.height + ")";
    }
  }
  
  @Override public String toString() {
    return "ToStringExample(" + this.getName() + ", " + this.shape + ", " + Arrays.deepToString(this.tags) + ")";
  }
}

@NoArgsConstructor, @AllArgsConstructor

@NoArgsConstructor

将生成一个没有参数的构造函数。如果这是不可能的(因为 final 字段),则会导致编译器错误,除非使用 @NoArgsConstructor(force = true),否则所有 final 字段都将初始化为 0 / false / null。对于具有约束的字段,例如@NonNull 字段,不会生成检查,因此请注意,这些约束通常不会在以后正确初始化这些字段之前得到满足。某些 java 构造,例如 hibernate 和服务提供者接口需要一个无参数构造函数。此注释主要与 @Data 或其他生成注释的构造函数之一结合使用。

@AllArgsConstructor

为类中生成一个带有所有参数的构造函数。标有 @NonNull 的字段会导致对这些参数进行空检查。

Lombok

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

@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class ConstructorExample<T> {
  private int x, y;
  @NonNull private T description;
  
  @NoArgsConstructor
  public static class NoArgsExample {
    @NonNull private String field;
  }
}

java

public class ConstructorExample<T> {
  private int x, y;
  @NonNull private T description;
  
  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"})
  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() {
    }
  }
}

引用

标签:lombok,description,int,private,Lombok,public,String
来源: https://www.cnblogs.com/lhnstart/p/16125009.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有