资讯专栏INFORMATION COLUMN

Lombok注解笔记

shery / 1921人阅读

摘要:设置为,直接访问字段,不调用此处列出的任何字段都不会在生成的和中使用。与相反,设置,失效添加注解,参考作用这个注解似乎没有实在的作用,就是标记这个类字段方法是自动生成的作用生成写在类上会生成该类下所有字段的。有点像的扩展函数。

</>复制代码

  1. lombok版本:1.18.2
前言

把lombok的注解过了一遍,发现有个@ExtensionMethod和kotlin的拓展函数有点类似

注解 @AllArgsConstructor

</>复制代码

  1. 作用

生成包含所有字段的构造器

</>复制代码

  1. 参数

staticName : 不为空的话,生成一个静态方法返回实例,并把构造器设置为private

</>复制代码

  1. @AllArgsConstructor(staticName = "create")
  2. public class Example {
  3. private int foo;
  4. private final String bar;
  5. }

生成:

</>复制代码

  1. public class Example {
  2. private int foo;
  3. private final String bar;
  4. private Example(int foo, String bar) {
  5. this.foo = foo;
  6. this.bar = bar;
  7. }
  8. public static Example create(int foo, String bar) {
  9. return new Example(foo, bar);
  10. }
  11. }

access : 构造器访问权限修饰符,默认public

@Builder

</>复制代码

  1. 作用

生成构建者(Builder)模式

</>复制代码

  1. 例子:

</>复制代码

  1. @Builder
  2. public class Example {
  3. private int foo;
  4. private final String bar;
  5. }

生成:

</>复制代码

  1. public class Example {
  2. private int foo;
  3. private final String bar;
  4. Example(int foo, String bar) {
  5. this.foo = foo;
  6. this.bar = bar;
  7. }
  8. public static Example.ExampleBuilder builder() {
  9. return new Example.ExampleBuilder();
  10. }
  11. public static class ExampleBuilder {
  12. private int foo;
  13. private String bar;
  14. ExampleBuilder() {
  15. }
  16. public Example.ExampleBuilder foo(int foo) {
  17. this.foo = foo;
  18. return this;
  19. }
  20. public Example.ExampleBuilder bar(String bar) {
  21. this.bar = bar;
  22. return this;
  23. }
  24. public Example build() {
  25. return new Example(this.foo, this.bar);
  26. }
  27. public String toString() {
  28. return "Example.ExampleBuilder(foo=" + this.foo + ", bar=" + this.bar + ")";
  29. }
  30. }
  31. }

</>复制代码

  1. 参数

builderMethodName : 创建构建器实例的方法名称

buildMethodName:构建器类中创建构造器实例的方法名称

builderClassName:构造器类名

toBuilder:生成toBuilder方法

</>复制代码

  1. 例子

</>复制代码

  1. public Example.ExampleBuilder toBuilder() {
  2. return (new Example.ExampleBuilder()).foo(this.foo).bar(this.bar);
  3. }
@Cleanup

</>复制代码

  1. 作用

在变量上声明@Cleanup,生成的代码会把变量用try{}包围,并在finallly块中调用close()

</>复制代码

  1. 例子

</>复制代码

  1. public class Example {
  2. public void copyFile(String in, String out) throws IOException {
  3. @Cleanup FileInputStream inStream = new FileInputStream(in);
  4. @Cleanup FileOutputStream outStream = new FileOutputStream(out);
  5. byte[] b = new byte[65536];
  6. while (true) {
  7. int r = inStream.read(b);
  8. if (r == -1) break;
  9. outStream.write(b, 0, r);
  10. }
  11. }
  12. }

生成后:

</>复制代码

  1. public class Example {
  2. public Example() {
  3. }
  4. public void copyFile(String in, String out) throws IOException {
  5. FileInputStream inStream = new FileInputStream(in);
  6. try {
  7. FileOutputStream outStream = new FileOutputStream(out);
  8. try {
  9. byte[] b = new byte[65536];
  10. while(true) {
  11. int r = inStream.read(b);
  12. if (r == -1) {
  13. return;
  14. }
  15. outStream.write(b, 0, r);
  16. }
  17. } finally {
  18. if (Collections.singletonList(outStream).get(0) != null) {
  19. outStream.close();
  20. }
  21. }
  22. } finally {
  23. if (Collections.singletonList(inStream).get(0) != null) {
  24. inStream.close();
  25. }
  26. }
  27. }
  28. }

</>复制代码

  1. 参数

value:被在finally块中调用的方法名,方法体不能带有参数,默认为close

@Data

</>复制代码

  1. 作用

生成所有字段的getter、toString()、hashCode()、equals()、所有非final字段的setter、构造器,相当于设置了 @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode

</>复制代码

  1. 例子

</>复制代码

  1. @Data
  2. public class Example {
  3. private int foo;
  4. private final String bar;
  5. }

生成:

</>复制代码

  1. public class Example {
  2. private int foo;
  3. private final String bar;
  4. public Example(String bar) {
  5. this.bar = bar;
  6. }
  7. public int getFoo() {
  8. return this.foo;
  9. }
  10. public String getBar() {
  11. return this.bar;
  12. }
  13. public void setFoo(int foo) {
  14. this.foo = foo;
  15. }
  16. public boolean equals(Object o) {
  17. if (o == this) {
  18. return true;
  19. } else if (!(o instanceof Example)) {
  20. return false;
  21. } else {
  22. Example other = (Example)o;
  23. if (!other.canEqual(this)) {
  24. return false;
  25. } else if (this.getFoo() != other.getFoo()) {
  26. return false;
  27. } else {
  28. Object this$bar = this.getBar();
  29. Object other$bar = other.getBar();
  30. if (this$bar == null) {
  31. if (other$bar != null) {
  32. return false;
  33. }
  34. } else if (!this$bar.equals(other$bar)) {
  35. return false;
  36. }
  37. return true;
  38. }
  39. }
  40. }
  41. protected boolean canEqual(Object other) {
  42. return other instanceof Example;
  43. }
  44. public int hashCode() {
  45. int PRIME = true;
  46. int result = 1;
  47. int result = result * 59 + this.getFoo();
  48. Object $bar = this.getBar();
  49. result = result * 59 + ($bar == null ? 43 : $bar.hashCode());
  50. return result;
  51. }
  52. public String toString() {
  53. return "Example(foo=" + this.getFoo() + ", bar=" + this.getBar() + ")";
  54. }
  55. }
@Delegate @EqualsAndHashCode

</>复制代码

  1. 作用

生成hashCode()、equals(),效果见@Data

</>复制代码

  1. 参数

callSuper:是否调用父类的hashCode(),默认:false

doNotUseGetters:是否不调用字段的getter,默认如果有getter会调用。设置为true,直接访问字段,不调用getter

exclude:此处列出的任何字段都不会在生成的equals和hashCode中使用。

of:与exclude相反,设置of,exclude失效

onParam:添加注解,参考@Getter#onMethod

@Generated

</>复制代码

  1. 作用

这个注解似乎没有实在的作用,就是标记这个类、字段、方法是自动生成的

@Getter

</>复制代码

  1. 作用

生成getter、写在类上会生成该类下所有字段的getter。写在某个字段上就作用与该字段

</>复制代码

  1. 参数

onMethod:把需要添加的注解写在这

</>复制代码

  1. 例子

</>复制代码

  1. public class Example {
  2. @Getter(onMethod_={@Deprecated}) // JDK7写法 @Getter(onMethod=@__({@Deprecated}))
  3. private int foo;
  4. private final String bar = "";
  5. }

生成:

</>复制代码

  1. public class Example {
  2. private int foo;
  3. private final String bar = "";
  4. public Example() {
  5. }
  6. /** @deprecated */
  7. @Deprecated
  8. public int getFoo() {
  9. return this.foo;
  10. }
  11. }

value:访问权限修饰符

@NoArgsConstructor

</>复制代码

  1. 作用

生成无参数构造器

</>复制代码

  1. 参数

access:访问权限修饰符

force:为true时,强制生成构造器,final字段初始化为null

onConstructor:添加注解,参考@Getter#onMethod

@NonNull

</>复制代码

  1. 作用

空检查

</>复制代码

  1. 例子

</>复制代码

  1. public class Example {
  2. @NonNull
  3. @Getter
  4. @Setter
  5. private Integer foo;
  6. }

生成后:

</>复制代码

  1. public class Example {
  2. @NonNull
  3. private Integer foo;
  4. public Example() {
  5. }
  6. @NonNull
  7. public Integer getFoo() {
  8. return this.foo;
  9. }
  10. public void setFoo(@NonNull Integer foo) {
  11. if (foo == null) {
  12. throw new NullPointerException("foo is marked @NonNull but is null");
  13. } else {
  14. this.foo = foo;
  15. }
  16. }
  17. }
@RequiredArgsConstructor

</>复制代码

  1. 作用

生成必须初始化字段的构造器,比如带final、@NonNull

</>复制代码

  1. 例子

</>复制代码

  1. @RequiredArgsConstructor
  2. public class Example {
  3. @NonNull
  4. private Integer foo;
  5. private final String bar;
  6. }

生成后:

</>复制代码

  1. public class Example {
  2. @NonNull
  3. private Integer foo;
  4. private final String bar;
  5. public Example(@NonNull Integer foo, String bar) {
  6. if (foo == null) {
  7. throw new NullPointerException("foo is marked @NonNull but is null");
  8. } else {
  9. this.foo = foo;
  10. this.bar = bar;
  11. }
  12. }
  13. }
@Setter

</>复制代码

  1. 作用

生成Setter

</>复制代码

  1. 参数

onMethod:在方法上添加中注解,见@Getter#onMethod

onParam:在方法的参数上添加注解,见@Getter#onMethod

value:访问权限修饰符

@Singular

</>复制代码

  1. 作用

这个注解和@Builder一起使用,为Builder生成字段是集合类型的add方法,字段名不能是单数形式,否则需要指定value值

</>复制代码

  1. 例子

</>复制代码

  1. @Builder
  2. public class Example {
  3. @Singular
  4. @Setter
  5. private List foos;
  6. }

生成:

</>复制代码

  1. public class Example {
  2. private List foos;
  3. Example(List foos) {
  4. this.foos = foos;
  5. }
  6. public static Example.ExampleBuilder builder() {
  7. return new Example.ExampleBuilder();
  8. }
  9. public void setFoos(List foos) {
  10. this.foos = foos;
  11. }
  12. public static class ExampleBuilder {
  13. private ArrayList foos;
  14. ExampleBuilder() {
  15. }
  16. // 这方法是@Singular作用生成的
  17. public Example.ExampleBuilder foo(Integer foo) {
  18. if (this.foos == null) {
  19. this.foos = new ArrayList();
  20. }
  21. this.foos.add(foo);
  22. return this;
  23. }
  24. public Example.ExampleBuilder foos(Collection foos) {
  25. if (this.foos == null) {
  26. this.foos = new ArrayList();
  27. }
  28. this.foos.addAll(foos);
  29. return this;
  30. }
  31. public Example.ExampleBuilder clearFoos() {
  32. if (this.foos != null) {
  33. this.foos.clear();
  34. }
  35. return this;
  36. }
  37. public Example build() {
  38. List foos;
  39. switch(this.foos == null ? 0 : this.foos.size()) {
  40. case 0:
  41. foos = Collections.emptyList();
  42. break;
  43. case 1:
  44. foos = Collections.singletonList(this.foos.get(0));
  45. break;
  46. default:
  47. foos = Collections.unmodifiableList(new ArrayList(this.foos));
  48. }
  49. return new Example(foos);
  50. }
  51. public String toString() {
  52. return "Example.ExampleBuilder(foos=" + this.foos + ")";
  53. }
  54. }
  55. }
@SneakyThrows

</>复制代码

  1. 作用

用try{}catch{}捕捉异常

</>复制代码

  1. 例子

</>复制代码

  1. public class Example {
  2. @SneakyThrows(UnsupportedEncodingException.class)
  3. public String utf8ToString(byte[] bytes) {
  4. return new String(bytes, "UTF-8");
  5. }
  6. }

生成后:

</>复制代码

  1. public class Example {
  2. public Example() {
  3. }
  4. public String utf8ToString(byte[] bytes) {
  5. try {
  6. return new String(bytes, "UTF-8");
  7. } catch (UnsupportedEncodingException var3) {
  8. throw var3;
  9. }
  10. }
  11. }
@Synchronized

</>复制代码

  1. 作用

生成Synchronized(){}包围代码

</>复制代码

  1. 例子

</>复制代码

  1. public class Example {
  2. @Synchronized
  3. public String utf8ToString(byte[] bytes) {
  4. return new String(bytes, Charset.defaultCharset());
  5. }
  6. }

生成后:

</>复制代码

  1. public class Example {
  2. private final Object $lock = new Object[0];
  3. public Example() {
  4. }
  5. public String utf8ToString(byte[] bytes) {
  6. Object var2 = this.$lock;
  7. synchronized(this.$lock) {
  8. return new String(bytes, Charset.defaultCharset());
  9. }
  10. }
  11. }
@ToString

</>复制代码

  1. 作用

生成toString()方法

@val

</>复制代码

  1. 作用

变量声明类型推断

</>复制代码

  1. 例子

</>复制代码

  1. public class ValExample {
  2. public String example() {
  3. val example = new ArrayList();
  4. example.add("Hello, World!");
  5. val foo = example.get(0);
  6. return foo.toLowerCase();
  7. }
  8. public void example2() {
  9. val map = new HashMap();
  10. map.put(0, "zero");
  11. map.put(5, "five");
  12. for (val entry : map.entrySet()) {
  13. System.out.printf("%d: %s
  14. ", entry.getKey(), entry.getValue());
  15. }
  16. }
  17. }

生成后:

</>复制代码

  1. public class ValExample {
  2. public ValExample() {
  3. }
  4. public String example() {
  5. ArrayList example = new ArrayList();
  6. example.add("Hello, World!");
  7. String foo = (String)example.get(0);
  8. return foo.toLowerCase();
  9. }
  10. public void example2() {
  11. HashMap map = new HashMap();
  12. map.put(0, "zero");
  13. map.put(5, "five");
  14. Iterator var2 = map.entrySet().iterator();
  15. while(var2.hasNext()) {
  16. Entry entry = (Entry)var2.next();
  17. System.out.printf("%d: %s
  18. ", entry.getKey(), entry.getValue());
  19. }
  20. }
  21. }
@Value

</>复制代码

  1. 作用

把类声明为final,并添加toString()、hashCode()等方法,相当于 @Getter @FieldDefaults(makeFinal=true, level=AccessLevel.PRIVATE) @AllArgsConstructor @ToString @EqualsAndHashCode.

</>复制代码

  1. 例子

</>复制代码

  1. @Value
  2. public class Example {
  3. private Integer foo;
  4. }

生成后:

</>复制代码

  1. public final class Example {
  2. private final Integer foo;
  3. public Example(Integer foo) {
  4. this.foo = foo;
  5. }
  6. public Integer getFoo() {
  7. return this.foo;
  8. }
  9. public boolean equals(Object o) {
  10. if (o == this) {
  11. return true;
  12. } else if (!(o instanceof Example)) {
  13. return false;
  14. } else {
  15. Example other = (Example)o;
  16. Object this$foo = this.getFoo();
  17. Object other$foo = other.getFoo();
  18. if (this$foo == null) {
  19. if (other$foo != null) {
  20. return false;
  21. }
  22. } else if (!this$foo.equals(other$foo)) {
  23. return false;
  24. }
  25. return true;
  26. }
  27. }
  28. public int hashCode() {
  29. int PRIME = true;
  30. int result = 1;
  31. Object $foo = this.getFoo();
  32. int result = result * 59 + ($foo == null ? 43 : $foo.hashCode());
  33. return result;
  34. }
  35. public String toString() {
  36. return "Example(foo=" + this.getFoo() + ")";
  37. }
  38. }
@var

</>复制代码

  1. 作用

和val一样,官方文档中说区别就是var不加final修饰,但测试的效果是一样的

Experimental注解

</>复制代码

  1. lombok.experimental包下
@Accessors

</>复制代码

  1. 作用

默认情况下,没什么作用,需要设置参数

</>复制代码

  1. 参数

chain:为true时,setter链式返回,即setter的返回值为this

fluent:为true时,默认设置chain为true,setter的方法名修改为字段名

@Delegate

</>复制代码

  1. 作用

代理模式,把字段的方法代理给类,默认代理所有方法

</>复制代码

  1. 参数

types:指定代理的方法

excludes:和types相反

</>复制代码

  1. 例子

</>复制代码

  1. public class Example {
  2. private interface Add {
  3. boolean add(String x);
  4. boolean addAll(Collection x);
  5. }
  6. private @Delegate(types = Add.class) List strings;
  7. }

生成后:

</>复制代码

  1. public class Example {
  2. private List strings;
  3. public Example() {
  4. }
  5. public boolean add(String x) {
  6. return this.strings.add(x);
  7. }
  8. public boolean addAll(Collection x) {
  9. return this.strings.addAll(x);
  10. }
  11. private interface Add {
  12. boolean add(String var1);
  13. boolean addAll(Collection var1);
  14. }
  15. }
@ExtensionMethod

</>复制代码

  1. 作用

拓展方法,向现有类型“添加”方法,而无需创建新的派生类型。有点像kotlin的扩展函数。

</>复制代码

  1. 例子

</>复制代码

  1. @ExtensionMethod({Arrays.class, Extensions.class})
  2. public class Example {
  3. public static void main(String[] args) {
  4. int[] intArray = {5, 3, 8, 2};
  5. intArray.sort();
  6. int num = 1;
  7. num = num.increase();
  8. Arrays.stream(intArray).forEach(System.out::println);
  9. System.out.println("num = " + num);
  10. }
  11. }
  12. class Extensions {
  13. public static int increase(int num) {
  14. return ++num;
  15. }
  16. }

生成后:

</>复制代码

  1. public class Example {
  2. public Example() {
  3. }
  4. public static void main(String[] args) {
  5. int[] intArray = new int[]{5, 3, 8, 2};
  6. Arrays.sort(intArray);
  7. int num = 1;
  8. int num = Extensions.increase(num);
  9. IntStream var10000 = Arrays.stream(intArray);
  10. PrintStream var10001 = System.out;
  11. System.out.getClass();
  12. var10000.forEach(var10001::println);
  13. System.out.println("num = " + num);
  14. }
  15. }

输出:

</>复制代码

  1. 2
  2. 3
  3. 5
  4. 8
  5. num = 2
@FieldDefaults

</>复制代码

  1. 作用

定义类、字段的修饰符

</>复制代码

  1. 参数

AccessLevel:访问权限修饰符

makeFinal:是否加final

@FieldNameConstants

</>复制代码

  1. 作用

默认生成一个常量,名称为大写字段名,值为字段名

</>复制代码

  1. 参数

prefix:前缀

suffix:后缀

</>复制代码

  1. 例子

</>复制代码

  1. public class Example {
  2. @FieldNameConstants(prefix = "PREFIX_", suffix = "_SUFFIX")
  3. private String foo;
  4. }

生成后:

</>复制代码

  1. public class Example {
  2. public static final String PREFIX_FOO_SUFFIX = "foo";
  3. private String foo;
  4. public Example() {
  5. }
  6. }
@Helper

</>复制代码

  1. 作用

方法内部的类方法暴露给方法使用

</>复制代码

  1. 测试时,maven编译不通过。
@NonFinal

</>复制代码

  1. 作用

设置不为Final,@FieldDefaults和@Value也有这功能

@PackagePrivate

</>复制代码

  1. 作用

设置为private,@FieldDefaults和@Value也有这功能

@SuperBuilder @Tolerate @UtilityClass @Wither

</>复制代码

  1. 作用

生成withXXX方法,返回类实例

</>复制代码

  1. 例子

</>复制代码

  1. @RequiredArgsConstructor
  2. public class Example {
  3. private @Wither final int foo;
  4. }

生成后:

</>复制代码

  1. public class Example {
  2. private final int foo;
  3. public Example(int foo) {
  4. this.foo = foo;
  5. }
  6. public Example withFoo(int foo) {
  7. return this.foo == foo ? this : new Example(foo);
  8. }
  9. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/76816.html

相关文章

  • Java 编译器 javac 笔记:javac API、注解处理 API 与 Lombok 原理

    摘要:对语法树的扫描,同样提供了扫描器。词法分析过程如下图所示语法分析,即根据语法由序列生成抽象语法树,对应实现类为。生成的抽象语法树如下图所示的实现原理依赖开发的典型的第三方库有,代码自动生成的和,代码检查的和,编译阶段完成依赖注入的等。 原文:http://nullwy.me/2017/04/java...如果觉得我的文章对你有用,请随意赞赏 javac 是 Java 代码的编译器 [...

    lookSomeone 评论0 收藏0
  • 使用 lombok 简化 Java 代码

    摘要:使用可以大大减少代码行数,提高开发效率。提供了日志工具无参构造器提供方法提供方法方法有参构造器,参数按属性定义顺序传入提供了空指针检测,会抛出异常 lombok 是一个第三方工具,提供了一些注解功能,可以帮助我们消除冗余、臃肿的 Java 代码,比如 POJO 的 getter/setter 方法、构造方法、hashcode 方法等。lombok 在编译时根据注解生成具体的代码,在虚拟...

    CloudwiseAPM 评论0 收藏0
  • 第二十九章:基于SpringBoot平台使用Lombok来优雅的编码

    摘要:还提供了全部参数的构造函数的自动生成,该注解的作用域也是只有在实体类上,因为只有实体类才会存在构造函数。当然除了全部参数的构造函数,还提供了没有参数的构造函数,使用方式与一致。 Lombok对于Java偷懒开发者来说应该是比较中意的,恰恰笔者就是一个喜欢在小细节上偷懒来提高开发效率的人。所以在技术框架的海洋里寻找了很久才在GitHub开源平台上找到,而在这之前国外很多程序猿一直使用该框...

    fanux 评论0 收藏0
  • Lombok安装及Spring Boot集成Lombok

    摘要:注意,其是在编译源码过程中,帮你自动生成的。就是说,将极大减少你的代码总量。注解和类似,区别在于它会把所有成员变量默认定义为修饰,并且不会生成方法。不同的日志注解总结如下上面是注解,下面是编译后的代码参考资料下的安装以及使用简介注解介绍 Lombok有什么用 在我们实体Bean中有大量的Getter/Setter方法以及toString, hashCode等可能不会用到,但是某些时候仍...

    dkzwm 评论0 收藏0
  • Lombok 看这篇就够了

    摘要:注解在类上为类提供一个全参的构造方法,加了这个注解后,类中不提供默认构造方法了。这个注解用在类上,使用类中所有带有注解的或者带有修饰的成员变量生成对应的构造方法。 转载请注明原创地址:http://www.54tianzhisheng.cn/2018/01/07/lombok/ showImg(http://ohfk1r827.bkt.clouddn.com/blog/180107/7...

    LeanCloud 评论0 收藏0

发表评论

0条评论

shery

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<