lombok使用

简介

lombok是一个编译级别的插件,它可以在项目编译的时候生成一些代码。通俗的说,lombok可以通过注解来标示生成getter settter等代码。

引入

创建gradle项目

compile group: 'org.projectlombok', name: 'lombok', version: '1.16.20'

注解

@NonNull#

标记字段不可为null

@Setter 
public class Person { 

@NonNull 
private String name; 
@NonNull 
private Integer age; 
}

对应的字节码文件:

public class Person { 

@NonNull 
private String name; 

@NonNull 
private Integer age; 

public Person() { } 

public void setName(@NonNull String name) { 
    if (name == null) { 
         throw new NullPointerException("name"); 
    }else { this.name = name; } 
} 

public void setAge(@NonNull Integer age) { 
     if (age == null) { throw new NullPointerException("age"); 
    } else { this.age = age; } } 
}

@Getter/@Setter#

自动生成getter和setter方法

 
public class Person {
@Getter 
private String name; 
@Setter 
private Integer age; 
}

对应的字节码文件:

public class Person { 

    private String name; 
    private Integer age; 
    public Person() { } 

    public String getName() { 
        return this.name; 
    }
    public void setAge(Integer age) { 
    this.age = age; 
    } 
}

@Cleanup#

自动关闭流代码

@Cleanup 
InputStream in = new FileInputStream(args[0]);

对应的字节码文件:

InputStream in = new FileInputStream(args[0]);
if (Collections.singletonList(in).get(0) != null) { in.close(); }

@AllArgsConstructor/@NoArgsConstructor/@RequiredArgsConstructor#

自动生成全参构造函数和无参构造函数

@AllArgsConstructor 
@NoArgsConstructor 
public class Person { 
    private String name; 
    private Integer age; 
}

对应的字节码文件

public class Person { 
    private String name; 
    private Integer age; 
    public Person(String name, Integer age) { 
        this.name = name; this.age = age; 
    } 
    public Person() { } 
}

@Builder#

自动生成建造者模式的bean

@Builder 
public class Person { 
    private String name; 
    private Integer age; 
}

对应的字节码文件

public class Person {
    private String name;
    private Integer age;

    Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public static Person.PersonBuilder builder() {
        return new Person.PersonBuilder();
    }

    public static class PersonBuilder {
        private String name;
        private Integer age;

        PersonBuilder() {
        }

        public Person.PersonBuilder name(String name) {
            this.name = name;
            return this;
        }

        public Person.PersonBuilder age(Integer age) {
            this.age = age;
            return this;
        }

        public Person build() {
            return new Person(this.name, this.age);
        }

        public String toString() {
            return "Person.PersonBuilder(name=" + this.name + ", age=" + this.age + ")";
        }
    }
}

 

@EqualsAndHashCode#

自动生成equals和hashcode方法

@EqualsAndHashCode 
public class Person { 
    private String name; 
    private Integer age; 
}

对应的字节码文件

public class Person {
    private String name;
    private Integer age;

    public Person() {
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Person)) {
            return false;
        } else {
            Person other = (Person)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$name = this.name;
                Object other$name = other.name;
                if (this$name == null) {
                    if (other$name != null) {
                        return false;
                    }
                } else if (!this$name.equals(other$name)) {
                    return false;
                }

                Object this$age = this.age;
                Object other$age = other.age;
                if (this$age == null) {
                    if (other$age != null) {
                        return false;
                    }
                } else if (!this$age.equals(other$age)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof Person;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $name = this.name;
        int result = result * 59 + ($name == null ? 43 : $name.hashCode());
        Object $age = this.age;
        result = result * 59 + ($age == null ? 43 : $age.hashCode());
        return result;
    }
}

@ToString#

自动生成toString()方法

@ToString 
public class Person { 
    private String name; 
    private Integer age; 
}

对应的字节码文件

public class Person {
    private String name;
    private Integer age;

    public Person() {
    }

    public String toString() {
        return "Person(name=" + this.name + ", age=" + this.age + ")";
    }
}

@Value#

自动生成全参构造函数、Getter方法、equals方法、hashCode法、toString方法

@Value 
public class Person { 
    private String name; 
    private Integer age; 
}

 

注意:@Value不会生成Setter方法

@Synchronized#

自动为被标记的方法添加synchronized锁

public class SynchronizedExample {
  private final Object readLock = new Object();
  
  @Synchronized
  public static void hello() {
    System.out.println("world");
  }
  
  @Synchronized
  public int answerToLife() {
    return 42;
  }
  
  @Synchronized("readLock")
  public void foo() {
    System.out.println("bar");
  }
}

对应的字节码文件

public class SynchronizedExample {
    private static final Object $LOCK = new Object[0];
    private final Object $lock = new Object[0];
    private final Object readLock = new Object();

    public static void hello() {
        synchronized($LOCK) {
            System.out.println("world");
        }
    }

    public int answerToLife() {
        synchronized($lock) {
            return 42;
        }
    }

    public void foo() {
        synchronized(readLock) {
            System.out.println("bar");
        }
    }
}

@Delegate#

为标记属性生成委托方法

public class DelegateExample {
    public void show() {
        System.out.println("show...");
    }
}
@AllArgsConstructor
public class Demo {
    @Delegate
    private final DelegateExample delegateExample;
}

对应的字节码文件

public class DelegateExample {
    public DelegateExample() {
    }

    public void show() {
        System.out.println("show...");
    }
}
public class Demo {
    private final DelegateExample delegateExample;

    public Demo(DelegateExample delegateExample) {
        this.delegateExample = delegateExample;
    }

    // 委托方法
    public void show() {
        this.delegateExample.show();
    }
}

作者: gcdd1993

出处:https://www.cnblogs.com/gcdd/p/12292131.html

版权:本文采用「署名-非商业性使用-相同方式共享 4.0 国际」知识共享许可协议进行许可。

你可能感兴趣的:(lombok使用)