@Data
-
功能
这是一个综合注解了,等于同时使用
@Getter
,@Setter
,@ToString
,@EqualsAndHashCode
,@RequiredArgsConstructor
-
源码
@Data public class LombokDemo { @NonNull private Integer id; private String name; }
-
编译后
package xyz.mrwood.study.lombok; public class LombokDemo { private Integer id; private String name; public LombokDemo() { } public Integer getId() { return this.id; } public String getName() { return this.name; } public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } public boolean equals(Object o) { if(o == this) { return true; } else if(!(o instanceof LombokDemo)) { return false; } else { LombokDemo other = (LombokDemo)o; if(!other.canEqual(this)) { return false; } else { Integer this$id = this.getId(); Integer other$id = other.getId(); if(this$id == null) { if(other$id != null) { return false; } } else if(!this$id.equals(other$id)) { return false; } String this$name = this.getName(); String other$name = other.getName(); if(this$name == null) { if(other$name != null) { return false; } } else if(!this$name.equals(other$name)) { return false; } return true; } } } protected boolean canEqual(Object other) { return other instanceof LombokDemo; } public int hashCode() { boolean PRIME = true; byte result = 1; Integer $id = this.getId(); int result1 = result * 59 + ($id == null?43:$id.hashCode()); String $name = this.getName(); result1 = result1 * 59 + ($name == null?43:$name.hashCode()); return result1; } public String toString() { return "LombokDemo(id=" + this.getId() + ", name=" + this.getName() + ")"; } }
@Value
-
功能
不可变类的@Date, 他会默认给属性加上final
-
源码
@Value public class LombokDemo { private Integer id; private String name; }
-
编译后
package xyz.mrwood.study.lombok; import java.beans.ConstructorProperties; public final class LombokDemo { private final Integer id; private final String name; @ConstructorProperties({"id", "name"}) public LombokDemo(Integer id, String name) { this.id = id; this.name = name; } public Integer getId() { return this.id; } public String getName() { return this.name; } public boolean equals(Object o) { if(o == this) { return true; } else if(!(o instanceof LombokDemo)) { return false; } else { LombokDemo other = (LombokDemo)o; Integer this$id = this.getId(); Integer other$id = other.getId(); if(this$id == null) { if(other$id != null) { return false; } } else if(!this$id.equals(other$id)) { return false; } String this$name = this.getName(); String other$name = other.getName(); if(this$name == null) { if(other$name != null) { return false; } } else if(!this$name.equals(other$name)) { return false; } return true; } } public int hashCode() { boolean PRIME = true; byte result = 1; Integer $id = this.getId(); int result1 = result * 59 + ($id == null?43:$id.hashCode()); String $name = this.getName(); result1 = result1 * 59 + ($name == null?43:$name.hashCode()); return result1; } public String toString() { return "LombokDemo(id=" + this.getId() + ", name=" + this.getName() + ")"; } }
@Accessors
-
功能
这个注解要搭配@Getter与@Setter使用,用来修改默认的setter与getter方法的形式
-
注意
@Accessors有三个参数可以使用
- chain 链式的形式
- fluent 流式的形式
- prefix 生成指定前缀的属性的getter与setter方法,并且生成的getter与setter方法时会去除前缀
-
源码 chain = true
@Accessors(chain = true) @Setter @Getter public class LombokDemo { private Integer id; private String name; }
-
编译后 chain = true
package xyz.mrwood.study.lombok; public class LombokDemo { private Integer id; private String name; public LombokDemo() { } public LombokDemo setId(Integer id) { this.id = id; return this; } public LombokDemo setName(String name) { this.name = name; return this; } public Integer getId() { return this.id; } public String getName() { return this.name; } }
-
源码 fluent = true
@Accessors(fluent = true) @Setter @Getter public class LombokDemo { private Integer id; private String name; }
-
编译后 fluent = true
package xyz.mrwood.study.lombok; public class LombokDemo { private Integer id; private String name; public LombokDemo() { } public LombokDemo id(Integer id) { this.id = id; return this; } public LombokDemo name(String name) { this.name = name; return this; } public Integer id() { return this.id; } public String name() { return this.name; } }
-
源码 prefix = "xxx"
@Accessors(prefix = "xxx") @Setter @Getter public class LombokDemo { private Integer xxxId; private String name; }
-
编译后 prefix = "xxx"
package xyz.mrwood.study.lombok; public class LombokDemo { private Integer xxxId; private String name; public LombokDemo() { } public void setId(Integer xxxId) { this.xxxId = xxxId; } public Integer getId() { return this.xxxId; } }
参考 https://blog.csdn.net/u011719271/article/details/53842420#sneakythrows
本文由 SAn 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为:
2018/09/02 23:26