当前位置: 移动技术网 > IT编程>开发语言>Java > 极简代码神器:Lombok使用教程

极简代码神器:Lombok使用教程

2019年07月30日  | 移动技术网IT编程  | 我要评论

lombok 是一个非常神奇的 java 类库,会利用注解自动生成 java bean 中烦人的 getter、setter,还能自动生成 logger、tostring、hashcode、builder 等 java特色的函数或是符合设计模式的函数,能够让你 java bean 更简洁,更美观。

lombok 的思想非常先进,它让我们省略繁琐的样板代码,不要在重复的代码上花费太长时间,它也是java语言演进过程中必然出现的一种思想,要用20% 的时间做 80%的事情。

下面就来看一下 lombok 的具体用法。

@data

@data 是一个很方便的注解,它和@tostring、 @equalandhashcode@getter/@setter、和@requiredargsconstructor 绑定在一起。换句话说,@data 生成通常与简单的pojo(plain old java objects) 和 bean 相关联的所有样板代码,例如:获取所有的属性,设置所有不可继承的属性,适应tostring、equals 和 hashcode 的实现,通过构造方法初始化所有final 的属性,以及所有没有使用@nonnull标记的初始化程序的非final字段,以确保该字段永远不为null。

@data 就像在类上隐含使用 @tostring 、 @equalandhashcode、 @getter、 @setter 和 @requiredargsconstructor 注释一样。@data = @getter + @setter + @tostring + @equalsandhashcode + @requiredargsconstructor 

但是,@data 无法设置这些注解的参数,例如callsuper、includefieldnames 和 exclude

如果您需要为这些参数中的任何一个设置非默认值,只需显式添加这些注释;

生成的所有getters/setters 默认都是public 的,为了覆盖访问级别,请使用显式的@setter \ @getter批注对字段或类进行注释。你可以使用这个注释(通过与 accesslevel.none结合)来禁止使用 getter或setter。

所有使用 transient 标记的字段都不会视为 hashcode 和 equals。将完全跳过所有静态字段(不考虑任何生成的方法,并且不会为它们创建setter / getter)。

如果类已经包含与通常生成的任何方法具有相同名称和参数计数的方法,则不会生成该方法,也不会发出警告或错误。

例如:如果你使用 equals 标记了一个方法,那么不会再生成 equals 方法,即使从技术上讲,由于具有不同的参数类型,它可能是完全不同的方法。同样的规则适用于构造函数(任何显式构造函数都会阻止 @data 生成一个),以及tostring,equals和所有getter和setter。

您可以使用@ lombok.experimental.tolerate 标记任何构造函数或方法,以将它们隐藏在 lombok 中

例如:

import lombok.accesslevel;
import lombok.data;
import lombok.setter;
import lombok.tostring;

@data
public class dataexample {

    private final string name;

    @setter(accesslevel.package)
    private int age;

    private double score;

    private string[] tags;

    @tostring(includefieldnames = true)
    @data(staticconstructor = "of")
    public static class exercise<t> {
        private final string name;
        private final t value;
    }

}

 

就相当于是不用 lombok 的如下示例:

import java.util.arrays;

public class dataexample {
  private final string name;
  private int age;
  private double score;
  private string[] tags;

  public dataexample(string name) {
    this.name = name;
  }

  public string getname() {
    return this.name;
  }

  void setage(int age) {
    this.age = age;
  }

  public int getage() {
    return this.age;
  }

  public void setscore(double score) {
    this.score = score;
  }

  public double getscore() {
    return this.score;
  }

  public string[] gettags() {
    return this.tags;
  }

  public void settags(string[] tags) {
    this.tags = tags;
  }

  @override public string tostring() {
    return "dataexample(" + this.getname() + ", " + this.getage() + ", " + this.getscore() + ", " + arrays.deeptostring(this.gettags()) + ")";
  }

  protected boolean canequal(object other) {
    return other instanceof dataexample;
  }

  @override public boolean equals(object o) {
    if (o == this) return true;
    if (!(o instanceof dataexample)) return false;
    dataexample other = (dataexample) o;
    if (!other.canequal((object)this)) return false;
    if (this.getname() == null ? other.getname() != null : !this.getname().equals(other.getname())) return false;
    if (this.getage() != other.getage()) return false;
    if (double.compare(this.getscore(), other.getscore()) != 0) return false;
    if (!arrays.deepequals(this.gettags(), other.gettags())) return false;
    return true;
  }

  @override public int hashcode() {
    final int prime = 59;
    int result = 1;
    final long temp1 = double.doubletolongbits(this.getscore());
    result = (result*prime) + (this.getname() == null ? 43 : this.getname().hashcode());
    result = (result*prime) + this.getage();
    result = (result*prime) + (int)(temp1 ^ (temp1 >>> 32));
    result = (result*prime) + arrays.deephashcode(this.gettags());
    return result;
  }

  public static class exercise<t> {
    private final string name;
    private final t value;

    private exercise(string name, t value) {
      this.name = name;
      this.value = value;
    }

    public static <t> exercise<t> of(string name, t value) {
      return new exercise<t>(name, value);
    }

    public string getname() {
      return this.name;
    }

    public t getvalue() {
      return this.value;
    }

    @override public string tostring() {
      return "exercise(name=" + this.getname() + ", value=" + this.getvalue() + ")";
    }

    protected boolean canequal(object other) {
      return other instanceof exercise;
    }

    @override public boolean equals(object o) {
      if (o == this) return true;
      if (!(o instanceof exercise)) return false;
      exercise<?> other = (exercise<?>) o;
      if (!other.canequal((object)this)) return false;
      if (this.getname() == null ? other.getvalue() != null : !this.getname().equals(other.getname())) return false;
      if (this.getvalue() == null ? other.getvalue() != null : !this.getvalue().equals(other.getvalue())) return false;
      return true;
    }

    @override public int hashcode() {
      final int prime = 59;
      int result = 1;
      result = (result*prime) + (this.getname() == null ? 43 : this.getname().hashcode());
      result = (result*prime) + (this.getvalue() == null ? 43 : this.getvalue().hashcode());
      return result;
    }
  }
}

 

@nonnull

你可以使用 @nonnull 对方法或者构造器生成 null - check

如果lombok为您生成整个方法或构造函数(例如@data),lombok总是将字段上通常称为@nonnull的各种注释视为生成空值检查的信号。但是,现在,在参数上使用lombok自己的@lombok.nonnull会导致在您自己的方法或构造函数中只插入null-check语句。

null - check 语句看起来像是如下语句

if(param == null){
  throw new nullpointerexception("param is marked @nonnull but is null")
}

 

这条判空语句会在方法的最开始进行判断

public class nonnullexample {

    @getter
    private string name;

    public nonnullexample(@nonnull string name){
        this.name = name;
    }
}

 

这个加上 @nonnull 判空的代码就相当于如下代码

import lombok.nonnull;

public class nonnullexample {
  private string name;

  public nonnullexample(@nonnull string name) {
    if (name == null) {
      throw new nullpointerexception("name is marked @nonnull but is null");
    }
    this.name = name;
  }
}

 

@getter & @setter

你可以使用 @getter 和 @setter 自动生成任何 getter/setter。

默认的 getter 只返回字段的名称,如果字段的名称为 foo,则返回的是 getfoo(),如果字段类型为 boolean ,则返回 isfoo()。如果字段为 foo 的话,默认的 setter 返回 setfoo,并且类型是 void ,并且带有一个和该属性相同的字段作为参数,用于为此属性字段进行赋值。

除非你指定accesslevel 访问级别,否则使用 getter / setter 生成的方法默认是 public 的作用范围。accesslevel的访问级别有 publicprotectedpackage, and private.

你也可以在类上使用 @getter / @setter ,在这种情况下,就会对该类中的所有非静态属性生成 get and set 方法

你也可以通过设置 accesslevel.none 禁用任何 get and set 方法的生成。这会使 @data、@getter / @setter 的注解失效。

public class gettersetterexample {

    @setter
    @getter
    private int age = 10;

    @setter(accesslevel.protected)
    private string name;

    @getter(accesslevel.private)
    private string high;
}

 

就等同于

public class gettersetterexample {

  private int age = 10;

  private string name;

  private string high;

  public int getage() {
    return age;
  }

  public void setage(int age) {
    this.age = age;
  }

  protected void setname(string name) {
    this.name = name;
  }

  private string gethigh(){
    return high;
  }
}

 

@tostring

@tostring 注解用来替换掉生成 tostring() 方法的实现,默认情况下,它会按顺序打印你的班级名称以及每个字段,并以逗号分隔。

通过设置 includefieldnames = true 能够使 tostring() 方法打印每个字段的属性值和名称。

默认情况下,所有非静态属性都被打印,如果你想要排除某些字段的话,需要设置 @tostring.exclude,或者,你可以指定tostring(onlyexplicitlyincluded = true)来指定哪些你希望使用的字段。然后使用@ tostring.include标记要包含的每个字段。

通过设置 callsuper 为 true ,可以将tostring的超类实现的输出包含到输出中。请注意,java.lang.object 的 tostring() 实现没有任何意义,所以你可能不会这样做除非你想要扩展另一个类。

你还可以在tostring 中包含方法调用的输出。只能包含不带参数的实例(非静态)方法,为此,请使用@ tostring.include标记方法。

你可以使用 @tostring.include(name =“some other name”)更改用于标识成员的名称,并且可以通过 @ tostring.include(rank = -1)更改成员的打印顺序。没有定义等级的成员默认是0级,等级高的成员优先被打印,优先级相同的成员按照它们在源文件中出现的顺序打印。

@tostring
public class tostringexample {

  // 静态属性不会包含
  private static final int static_var = 10;
  private string name;
  private string[] tags;
  private shape shape = new square(5, 10);

  // 排除指定字段不会被 tostring 打印
  @tostring.exclude
  private int id;

  public string getname() {
    return this.name;
  }

  // callsuper 表示是否扩展父类的 tostring(), 
  // includefieldnames 表示是否包含属性名称
  @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;
    }
  }

  public static class shape {}
}

 

测试一下上面的示例

tostringexample tostringexample = new tostringexample();
system.out.println(tostringexample);

 

输出如下

tostringexample(name=null, tags=null, shape=tostringexample.square(super=com.project.lombok.tostringexample$square@1b9e1916, width=5, height=10))

 

注释掉 callsuper = true, 测试结果如下

tostringexample(name=null, tags=null, shape=tostringexample.square(width=5, height=10))

 

从输出可以看出,如果不扩展父类,不会输出关于 shape 的内部类信息,callsuper 默认为 false

注释掉 includefieldnames,测试结果不会发生变化,所以 includefieldnames 默认值为 true

更改 includefieldnames = false,测试结果如下

tostringexample(name=null, tags=null, shape=tostringexample.square(super=com.project.lombok.tostringexample$square@1b9e1916, 5, 10))

 

从输出可以看出,如果设置 includefieldnames = false ,不会输出shape 中的字段名称信息。

上面用@tostring 注解修饰的例子就相当于是下面这段代码

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

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

  public static class shape {}
}

 

@equalsandhashcode

任何类的定义都可以用@equalsandhashcode 标注,让 lombok 为其生成 equals和 hashcode 方法。默认情况下,将会用在非静态,非 transient 标记的字段上,但是你可以通过 @equalsandhashcode.include或 @equalsandhashcode.exclude 标记类型成员来修改使用哪些字段。

或者,你可以通过使用 @equalsandhashcode.include 并使用 @equalsandhashcode(onlyexplicitlyincluded = true)标记它们来准确指定你希望使用的字段或方法。

如果将 @equalsandhashcode 应用于扩展另一个的类,这个特性就会变的很危险。通常来说,对类自动生成equals 和 hashcode 方法不是一个好的选择,因为超类也定义了字段,这些字段也需要equals / hashcode方法。通过设置 callsuper 为 true,可以在生成的方法中包含超类的 equals 和 hachcode 方法。

对于 hashcode 来说,super.hashcode 的结果包括了哈希算法,对于 equals 来说,如果超类实现认为它不等于传入的对象,生成的方法将返回 false。请注意,不是所有的equals 实现都能正确处理这种情况。然而,lombok生成的 equals实现可以正确处理这种情况。

如果不扩展类时(只扩展任何java.lang.object 类)时把 callsuper 设置为 true 会提示编译错误,因为 lombok 会将生成的 equals() 方法和 hashcode() 实现转换为从 object 继承过来:只有相同的 object 对象彼此相等并且具有相同的 hashcode 。

当你继承其他类时没有设置 callsuper 为 true 会进行警告,因为除非父类没有相同的属性,lombok无法为您生成考虑超类声明的字段的实现。你需要自己写实现类或者依赖 callsuper 工具。你还可以使用 lombok.equalsandhashcode.callsuper 配置key。

下面是一个例子

@equalsandhashcode
public class equalsandhashcodeexample {

    private transient int transientvar = 10;
    private string name;
    private double score;
    @equalsandhashcode.exclude private shape shape = new square(5,10);
    private string[] tags;
    @equalsandhashcode.exclude private int id;

    public string getname() {
        return name;
    }

    @equalsandhashcode(callsuper = true)
    public static class square extends shape {
        private final int width,height;

        public square(int width,int height){
            this.width = width;
            this.height = height;
        }
    }

    public static class shape {}
}

 

@noargsconstructor, @requiredargsconstructor, @allargsconstructor

lombok 有三个生成构造函数的注解,下面一起来看一下它们的使用说明和示例

@noargsconstructor 将会生成无参数的构造函数,如果有final 修饰的字段并且没有为 final 修饰的字段进行初始化的话,那么单纯的使用 @noargsconstructor 注解会提示编译错误

 

修改建议:需要为 @noargsconstructor 指定一个属性@noargsconstructor(force=true),lombok会为上面的final 字段默认添加初始值,因为id 是 int 类型,所以 id 的初始值为 0,类似的不同类型的字段的初始值还有 false / null / 0,特定的 java 构造,像是 hibernate 和 服务提供接口需要无参数的构造方法。此注解主要与 @data 或生成注解的其他构造函数组合使用。

这里有个需要注意的地方:@nonnull 不要和 @noargsconstructor 一起使用

@noargsconstructor
@getter
public class noargsconstructorexample {
    private  long id ;
    private @nonnull string name;
    private integer age;

    public static void main(string[] args) {
        system.out.println(new noargsconstructorexample().getname());
    }
}

 

输出结果是 null ,因此如果有 @nonnull 修饰的成员的变量就不要用 @noargsconstructor 修饰类

@requiredargsconstructor 将为每个需要特殊处理的字段生成一个带有1个参数的构造函数。所有未初始化的 final 字段都会获取一个参数,以及标记为 @nonnull 的任何字段也会获取一个参数。这些字段在声明它们的地方没有初始化。对于这些标记为 @nonnull 的字段,会生成特殊的null 编译检查。如果标记为 @nonnull 的字段的参数为 null,那么构造函数将会抛出 nullpointerexception。参数的顺序与字段在类中的显示顺序相匹配。

例如下面这个例子,只有 @nonnull 和 final 修饰的字段才会加入构造函数

@requiredargsconstructor
public class requiredargsconstructorexample {

    @nonnull
    private int id;
    private final string name;
    private boolean human;

}

 

生成的结果大概是这样的

public class requiredargsconstructorexample {
    @nonnull
    private int id;
    private final string name;
    private boolean human;

    public requiredargsconstructorexample(@nonnull int id, string name) {
        if (id == null) {
            throw new nullpointerexception("id is marked @nonnull but is null");
        } else {
            this.id = id;
            this.name = name;
        }
    }
}

 

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

@allargsconstructor
public class allargsconstructorexample {

    private int id;
    private string name;
    private int age;

}

 

相当于自动生成如下代码

public allargsconstructorexample(int id, string name, int age) {
    this.id = id;
    this.name = name;
    this.age = age;
}

 

这些注解中的每一个都允许使用替代形式,其中生成的构造函数始终是私有的,并且生成包含私有构造函数的附加静态工厂方法,通过为注释提供staticname值来启用此模式,@requiredargsconstructor(staticname =“of”)。看下面这个例子

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

 

就会变为

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

 

文章参考:

https://www.hellojava.com/a/74973.html
https://www.projectlombok.org/features/constructor

 

作者:cxuan

 

往期阅读

1. 

2. 

3. 

4. 

5. 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网