首页  

lombok常用注解     所属分类 lombok 浏览量 486
@Data   注解在类上 所有属性的 get set  方法  以及 equals  hashCode toString 方法
自动为所有字段添加 @ToString , @EqualsAndHashCode, @Getter 方法,
为非final字段添加 @Setter,和 @RequiredArgsConstructor

@Value  作用与 @Data 类似 ,成员变量 private final 修饰,且不会生成set方法

@Setter 注解在属性上 set 方法
@Getter 注解在属性上 get 方法


@NoArgsConstructor  注解在类上 无参构造方法
@AllArgsConstructor 注解在类上 全参的构造方法
@RequiredArgsConstructor  包含 特定参数 的构造器 ,final修饰的成员变量

@Builder   使用构造者模式
@Data 和 @Builder 一起使用,流式设值
 
@Builder 只能生成当前类的字段构建方法。  @SuperBuilder 可使用父类字段
@Synchronized  加同步锁
@SneakyThrows  等同于try/catch 捕获异常
@NonNull        参数为null 抛 空指针异常

@EqualsAndHashCode
生成 equals(Object other)和 hashcode方法,包括所有非静态变量 和 非 transient变量
equals 和 hashcode 要一起实现
@EqualsAndHashCode(exclude="xxx")

@ToString

@Log
@Log4j  注解在类上  生成 属性名为log 的 log4j 日志对象
@Slf4j

@Cleanup   资源清理 调用 close方法

https://www.sohu.com/a/378470481_355142


看源码是 ,无法跳转到 lombok生成的代码 
idea 可以 到 target classes 下面 看 相应的 class文件 
会自动反编译
Source code recreated from a .class file by IntelliJ IDEA
(powered by FernFlower decompiler)


import lombok.*; @Data @Builder @NoArgsConstructor @AllArgsConstructor // @ToString public class Data001 { private String str1; private String str2; private String str3; } 生成的代码
public class Data001 { private String str1; private String str2; private String str3; public static Data001.Data001Builder builder() { return new Data001.Data001Builder(); } public String getStr1() { return this.str1; } public String getStr2() { return this.str2; } public String getStr3() { return this.str3; } public void setStr1(String str1) { this.str1 = str1; } public void setStr2(String str2) { this.str2 = str2; } public void setStr3(String str3) { this.str3 = str3; } public boolean equals(Object o) { if (o == this) { return true; } else if (!(o instanceof Data001)) { return false; } else { Data001 other = (Data001)o; if (!other.canEqual(this)) { return false; } else { label47: { Object this$str1 = this.getStr1(); Object other$str1 = other.getStr1(); if (this$str1 == null) { if (other$str1 == null) { break label47; } } else if (this$str1.equals(other$str1)) { break label47; } return false; } Object this$str2 = this.getStr2(); Object other$str2 = other.getStr2(); if (this$str2 == null) { if (other$str2 != null) { return false; } } else if (!this$str2.equals(other$str2)) { return false; } Object this$str3 = this.getStr3(); Object other$str3 = other.getStr3(); if (this$str3 == null) { if (other$str3 != null) { return false; } } else if (!this$str3.equals(other$str3)) { return false; } return true; } } } protected boolean canEqual(Object other) { return other instanceof Data001; } public int hashCode() { int PRIME = true; int result = 1; Object $str1 = this.getStr1(); int result = result * 59 + ($str1 == null ? 43 : $str1.hashCode()); Object $str2 = this.getStr2(); result = result * 59 + ($str2 == null ? 43 : $str2.hashCode()); Object $str3 = this.getStr3(); result = result * 59 + ($str3 == null ? 43 : $str3.hashCode()); return result; } public String toString() { return "Data001(str1=" + this.getStr1() + ", str2=" + this.getStr2() + ", str3=" + this.getStr3() + ")"; } public Data001() { } public Data001(String str1, String str2, String str3) { this.str1 = str1; this.str2 = str2; this.str3 = str3; } public static class Data001Builder { private String str1; private String str2; private String str3; Data001Builder() { } public Data001.Data001Builder str1(String str1) { this.str1 = str1; return this; } public Data001.Data001Builder str2(String str2) { this.str2 = str2; return this; } public Data001.Data001Builder str3(String str3) { this.str3 = str3; return this; } public Data001 build() { return new Data001(this.str1, this.str2, this.str3); } public String toString() { return "Data001.Data001Builder(str1=" + this.str1 + ", str2=" + this.str2 + ", str3=" + this.str3 + ")"; } } }

https://gitee.com/dyyx/hellocode/tree/master/demo/lombokdemo
Lombok的一些坑 eclipse安装lombok

上一篇     下一篇
Java Builder 模式 实例

隐私计算技术点和术语

反编译工具 JD-GUI 启动报错

mysql insert 性能优化

联邦学习简介

idea设置源码目录