Lombok

What is Lombok?

Lombok is a Java library that reduces code through annotations, such as reducing get, set methods, constructors, etc. through annotations.

How to install Lombok?

How to install and use Lombok?

  • The first method
    is to add the lombok.jar package to the lib folder,
  • Method 2 If you are using Maven, you can add the following dependencies to the project's pom.xml file:
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.12</version>
</dependency>

Through the above two methods, Lombok's related APIs can indeed be used, but neither Intelij idea nor Eclipse can recognize it by default, and we have to let the idea recognize it.
Intellij Idea method:
File->settings->Plugins->Browse repositories->Lombok as shown:

1.3 How to use Lombok?

Lombok provides annotation methods to improve the simplicity of the code. Common annotations are:

  • @Data
  • @Setter @Getter
  • @NonNull
  • @Synchronized
  • @ToString
  • @EqualsAndHashCode
  • @Cleanup
  • @SneakyThrows

The function of each method and the corresponding source code @Data are described below.
This annotation is equivalent to adding the following annotations @Setter @Getter, @ToString, @EqualsAndHashCode, which are used in the class:

/**
 * author: andy
 * date: 17-4-22
 * blog: www.andyqian.com
 */
@Data
public class Person {
    private String name;
    private String address;
    private String city;
    private String state;
    private String zip;
    private Date brithday;
}

The effect is as follows:

@Getter@Setter
acts on properties and automatically generates get and set methods.

/**
 * author: andy
 * date: 17-4-22
 * blog: www.andyqian.com
 */
public class Person {
    @Getter@Setter
    private String name;
}

Equivalent source code:

public String getName() {
        return name;
    }

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

@NonNull
This annotation quickly determines whether it is empty, and if it is empty, throws java.lang.NullPointerException
How to use:

/**
 * author: andy
 * date: 17-4-22
 * blog: www.andyqian.com
 */
public class Person {

    private String name;
    
    @Setter@Getter@NonNull
    private List<Person> member;
}

Source code:

@NonNull
private List<Person> members;

public Family(@NonNull final List<Person> members) {
    if (members == null) throw new java.lang.NullPointerException("members");
    this.members = members;
}
    
@NonNull
public List<Person> getMembers() {
    return members;
}

public void setMembers(@NonNull final List<Person> members) {
    if (members == null) throw new java.lang.NullPointerException("members");
    this.members = members;
}

@Synchronized
该注解自动添加到同步机制,有趣的是,生成的代码并不是直接锁方法,而是锁代码块, 作用范围是方法上
使用方法:

private DateFormat format = new SimpleDateFormat("MM-dd-YYYY");

@Synchronized
public String synchronizedFormat(Date date) {
    return format.format(date);
}

生成等价源码:

private final java.lang.Object $lock = new java.lang.Object[0];
private DateFormat format = new SimpleDateFormat("MM-dd-YYYY");

public String synchronizedFormat(Date date) {
    synchronized ($lock) {
        return format.format(date);
    }
}

@ToString
该方法大家应该非常熟悉,但需要注意的是:@ToString有多个属性可以进一步设置:

  • callSuper 是否输出父类的toString方法,默认为false
  • includeFieldNames 是否包含字段名称,默认为true
  • exclude 排除生成tostring的字段

使用方法:

/**
 * author: andy
 * date: 17-4-22
 * blog: www.andyqian.com
 */
@ToString(callSuper = true,exclude ={"name"})
public class Person {
    private String name;
    private String address;
    
}

等价源码:

public String toString() {
 return "Person{" +
                "address='" + address + '\'' +
    '}';
}

@Cleanup
注释可用于确保已分配的资源被释放,如IO的连接关闭。
使用方法:

public void testCleanUp() {
    try {
        @Cleanup ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos.write(new byte[] {'Y','e','s'});
        System.out.println(baos.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

等价源码:

public void testCleanUp() {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            baos.write(new byte[]{'Y', 'e', 's'});
            System.out.println(baos.toString());
        } finally {
            baos.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Guess you like

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