lombok使用手册
lombok使用手册:
1、安装插件
1.1、idel:

elipse:下载地址https://projectlombok.org/download
运行jar文件

1.2、使用lombok还需要引用相关jar包
参考网址:https://projectlombok.org/features/all
2、注解说明
2.1、@Data
@Data
All together now: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, and @Setter on all non-final fields, and @RequiredArgsConstructor!
Lombok代码:
@Data
public class Lombok {
private String name;
private String age;
}
编译后代码:
public class Lombok {
private String name;
private String age;
public Lombok() {
}
public String getName() {
return this.name;
}
public String getAge() {
return this.age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(String age) {
this.age = age;
}
public boolean equals(Object o) {
if(o == this) {
return true;
} else if(!(o instanceof Lombok)) {
return false;
} else {
Lombok other = (Lombok)o;
if(!other.canEqual(this)) {
return false;
} else {
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;
}
String this$age = this.getAge();
String other$age = other.getAge();
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 Lombok;
}
public int hashCode() {
boolean PRIME = true;
byte result = 1;
String $name = this.getName();
int result1 = result * 59 + ($name == null?43:$name.hashCode());
String $age = this.getAge();
result1 = result1 * 59 + ($age == null?43:$age.hashCode());
return result1;
}
public String toString() {
return "Lombok(name=" + this.getName() + ", age=" + this.getAge() + ")";
}
}
2.2、@val
编译前代码:
val example = new ArrayList<String>();
example.add("Hello, World!");
val foo = example.get(0);
return foo.toLowerCase(); val map = new HashMap<Integer, String>();
map.put(0, "zero");
map.put(5, "five");
for (val entry : map.entrySet()) {
System.out.printf("%d: %s\n", entry.getKey(), entry.getValue());
}
编译后代码:
final ArrayList<String> example = new ArrayList<String>();
example.add("Hello, World!");
final String foo = example.get(0);
return foo.toLowerCase(); final HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(0, "zero");
map.put(5, "five");
for (final Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.printf("%d: %s\n", entry.getKey(), entry.getValue());
}
2.3、@var 用法参考@val
2.4、@NonNull
编译前代码:
public NonNullExample(@NonNull Person person) {
this.name = person.getName();
}
编译后代码:
public NonNullExample(@NonNull Person person) {
if (person == null) {
throw new NullPointerException("person is marked @NonNull but is null");
}
this.name = person.getName();
}
2.5、@Cleanup
编译前代码:
@Cleanup InputStream in = new FileInputStream(args[0]);
@Cleanup OutputStream out = new FileOutputStream(args[1]);
byte[] b = new byte[10000];
while (true) {
int r = in.read(b);
if (r == -1) break;
out.write(b, 0, r);
}
编译后代码:
InputStream in = new FileInputStream(args[0]);
try {
OutputStream out = new FileOutputStream(args[1]);
try {
byte[] b = new byte[10000];
while (true) {
int r = in.read(b);
if (r == -1) break;
out.write(b, 0, r);
}
} finally {
if (out != null) {
out.close();
}
}
} finally {
if (in != null) {
in.close();
}
}
2.6、@Getter、@Setter
编译前代码:
@Getter @Setter private int age = 10;
@Setter private String name;
编译后代码:
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
protected void setName(String name) {
this.name = name;
}
2.7、@ToString
编译前代码:
@ToString
public class Lombok {
@Getter @Setter String name;
@Getter @Setter private String age;
@ToString.Exclude String id;
}
编译后代码:
public class Lombok {
String name;
private String age;
String id;
public Lombok() {
}
public String toString() {
return "Lombok(name=" + this.getName() + ", age=" + this.getAge() + ")";
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return this.age;
}
public void setAge(String age) {
this.age = age;
}
}
2.8、@EqualsAndHashCode
编译前代码:
@EqualsAndHashCode
public class Lombok {
@Getter @Setter String name;
@Getter @Setter private String age;
@EqualsAndHashCode.Exclude String id;
}
编译后代码:
public class Lombok {
String name;
private String age;
String id;
public Lombok() {
}
public boolean equals(Object o) {
if(o == this) {
return true;
} else if(!(o instanceof Lombok)) {
return false;
} else {
Lombok other = (Lombok)o;
if(!other.canEqual(this)) {
return false;
} else {
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;
}
String this$age = this.getAge();
String other$age = other.getAge();
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 Lombok;
}
public int hashCode() {
boolean PRIME = true;
byte result = 1;
String $name = this.getName();
int result1 = result * 59 + ($name == null?43:$name.hashCode());
String $age = this.getAge();
result1 = result1 * 59 + ($age == null?43:$age.hashCode());
return result1;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return this.age;
}
public void setAge(String age) {
this.age = age;
}
}
2.9、@NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor构造器
编译前代码:
@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() {
}
}
}
2.10、@Value用法参考@Data默认设置为final,会被其他注解显示生效
2.11、@Builder
编译前代码:
@Builder
public class BuilderExample {
@Builder.Default private long created = System.currentTimeMillis();
private String name;
private int age;
@Singular
private Set<String> occupations;
}
编译后代码:
public class BuilderExample {
private long created;
private String name;
private int age;
private Set<String> occupations;
private static long $default$created() {
return System.currentTimeMillis();
}
BuilderExample(long created, String name, int age, Set<String> occupations) {
this.created = created;
this.name = name;
this.age = age;
this.occupations = occupations;
}
public static BuilderExample.BuilderExampleBuilder builder() {
return new BuilderExample.BuilderExampleBuilder();
}
public static class BuilderExampleBuilder {
private boolean created$set;
private long created$value;
private String name;
private int age;
private ArrayList<String> occupations;
BuilderExampleBuilder() {
}
public BuilderExample.BuilderExampleBuilder created(long created) {
this.created$value = created;
this.created$set = true;
return this;
}
public BuilderExample.BuilderExampleBuilder name(String name) {
this.name = name;
return this;
}
public BuilderExample.BuilderExampleBuilder age(int age) {
this.age = age;
return this;
}
public BuilderExample.BuilderExampleBuilder occupation(String occupation) {
if(this.occupations == null) {
this.occupations = new ArrayList();
}
this.occupations.add(occupation);
return this;
}
public BuilderExample.BuilderExampleBuilder occupations(Collection<? extends String> occupations) {
if(this.occupations == null) {
this.occupations = new ArrayList();
}
this.occupations.addAll(occupations);
return this;
}
public BuilderExample.BuilderExampleBuilder clearOccupations() {
if(this.occupations != null) {
this.occupations.clear();
}
return this;
}
public BuilderExample build() {
Set occupations;
switch(this.occupations == null?0:this.occupations.size()) {
case 0:
occupations = Collections.emptySet();
break;
case 1:
occupations = Collections.singleton(this.occupations.get(0));
break;
default:
LinkedHashSet occupations1 = new LinkedHashSet(this.occupations.size() < 1073741824?1 + this.occupations.size() + (this.occupations.size() - 3) / 3:2147483647);
occupations1.addAll(this.occupations);
occupations = Collections.unmodifiableSet(occupations1);
}
long created$value = this.created$value;
if(!this.created$set) {
created$value = System.currentTimeMillis();
}
return new BuilderExample(created$value, this.name, this.age, occupations);
}
public String toString() {
return "BuilderExample.BuilderExampleBuilder(created$value=" + this.created$value + ", name=" + this.name + ", age=" + this.age + ", occupations=" + this.occupations + ")";
}
}
}
2.12、@SneakyThrows
编译前代码:
@SneakyThrows(UnsupportedEncodingException.class)
public String utf8ToString(byte[] bytes) {
return new String(bytes, "UTF-8");
} @SneakyThrows
public void run() {
throw new Throwable();
}
编译后代码:
public String utf8ToString(byte[] bytes) {
try {
return new String(bytes, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw Lombok.sneakyThrow(e);
}
}
public void run() {
try {
throw new Throwable();
} catch (Throwable t) {
throw Lombok.sneakyThrow(t);
}
}
2.13、@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");
}
}
}
2.14、@With 必须要包含全参数构造器
编译前代码:
@AllArgsConstructor
public class Lombok {
@With private Integer x;
@With private Integer y;
@With private Integer z;
}
编译后代码:
public class Lombok {
private Integer x;
private Integer y;
private Integer z;
public Lombok(Integer x, Integer y, Integer z) {
this.x = x;
this.y = y;
this.z = z;
}
public Lombok withX(Integer x) {
return this.x == x?this:new Lombok(x, this.y, this.z);
}
public Lombok withY(Integer y) {
return this.y == y?this:new Lombok(this.x, y, this.z);
}
public Lombok withZ(Integer z) {
return this.z == z?this:new Lombok(this.x, this.y, z);
}
}
2.15、@Getter(lazy=true)缓存
编译前代码:
public class Lombok {
@Getter(lazy=true) private final int[] cached = expensive();
private int[] expensive() {
int[] result = new int[1000000];
for (int i = 0; i < result.length; i++) {
result[i] = i;
}
return result;
}
}
编译后代码:
public class Lombok {
private final AtomicReference<Object> cached = new AtomicReference();
public Lombok() {
}
private int[] expensive() {
int[] result = new int[1000000];
for(int i = 0; i < result.length; result[i] = i++) {
;
}
return result;
}
public int[] getCached() {
Object value = this.cached.get();
if(value == null) {
AtomicReference var2 = this.cached;
synchronized(this.cached) {
value = this.cached.get();
if(value == null) {
int[] actualValue = this.expensive();
value = actualValue == null?this.cached:actualValue;
this.cached.set(value);
}
}
}
return (int[])((int[])(value == this.cached?null:value));
}
}
2.16、日志@Log (and friends)
@CommonsLog
Creates private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
@Flogger
Creates private static final com.google.common.flogger.FluentLogger log = com.google.common.flogger.FluentLogger.forEnclosingClass();
@JBossLog
Creates private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class);
@Log
Creates private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());
@Log4j
Creates private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class);
@Log4j2
Creates private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
@Slf4j
Creates private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
@XSlf4j
Creates private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);
@CustomLog
Creates private static final com.foo.your.Logger log = com.foo.your.LoggerFactory.createYourLogger(LogExample.class);
@Log
public class Lombok {
@Getter(lazy=true) private final int[] cached = expensive(); private int[] expensive() {
int[] result = new int[1000000];
for (int i = 0; i < result.length; i++) {
result[i] = i;
}
log.severe(result.toString());
return result;
}
}
2.17、额外的注解见链接https://projectlombok.org/features/all
lombok使用手册的更多相关文章
- Java8特性大全(最新版)
一.序言 Java8 是一个里程碑式的版本,凭借如下新特性,让人对其赞不绝口. Lambda 表达式给代码构建带来了全新的风格和能力: Steam API 丰富了集合操作,拓展了集合的能力: 新日期时 ...
- Mybatis-Plus3.0入门手册
Mybatis-Plus3.0入门手册 ref: https://blog.csdn.net/moshowgame/article/details/81008485 Mybatis-Plus简介 ...
- 阿里巴巴Java开发手册(详尽版)-个人未注意到的知识点(转)
转自 https://blog.csdn.net/u013039395/article/details/86528164 一.编程规约 (一) 命名风格 [强制]代码中的命名只可用英文方式 [强制]类 ...
- [No000019A]IDEA 设置手册
[No000019A]idea设置手册.rar IDEA 设置手册 IDEA 设置手册 plugin lgnore files and folesrs 代码管控 程序框架 部署方式 useless 3 ...
- 为什么阿里Java手册推荐慎用 Object 的 clone 方法来拷贝对象
图片若无法显示,可至掘金查看https://juejin.im/post/5d425230f265da039519d248 前言 在阿里Java开发手册中,有这么一条建议:慎用 Object 的 cl ...
- 记一次lombok踩坑记
引言 今天中午正在带着耳机遨游在代码的世界里,被运营在群里@了,气冲冲的反问我最近有删生产的用户数据的吗?我肯定客气的回答道没有呀?生产的数据我怎么能随随便便可以删除,这可是公司的红线,再说了我也没有 ...
- FREERTOS 手册阅读笔记
郑重声明,版权所有! 转载需说明. FREERTOS堆栈大小的单位是word,不是byte. 根据处理器架构优化系统的任务优先级不能超过32,If the architecture optimized ...
- JS魔法堂:不完全国际化&本地化手册 之 理論篇
前言 最近加入到新项目组负责前端技术预研和选型,其中涉及到一个熟悉又陌生的需求--国际化&本地化.熟悉的是之前的项目也玩过,陌生的是之前的实现仅仅停留在"有"的阶段而已. ...
- 转职成为TypeScript程序员的参考手册
写在前面 作者并没有任何可以作为背书的履历来证明自己写作这份手册的分量. 其内容大都来自于TypeScript官方资料或者搜索引擎获得,期间掺杂少量作者的私见,并会标明. 大部分内容来自于http:/ ...
随机推荐
- Flask数据库的基本操作
Flask操作数据库基本操作 常用的SQLAlchemy字段类型 类型名 python中类型 说明 Integer int 普通整数,一般是32位 SmallInteger int 取值范围小的整 ...
- js 正则替换的使用方法
function compress(source) { const keys = {}; ⇽--- 存储目标key source.replace( /([^=&]+)=([^&]*)/ ...
- No package docker-io available
新手centos6.8安装docker时从遇到No package docker-io available开始的各种不小心的坑... 新安装了CentOS6.8,准备安装docker,执行命令 yum ...
- ARM TK1 安装kinect驱动
首先安装usb库 $ git clone https://github.com/libusb/libusb.git 编译libusb需要的工具 $ sudo apt-get install autoc ...
- ARM 汇编 内存访问指令
一. 单个寄存器操作读写内存 内存访问指令格式:<opcode><cond> Rd, [Rn] Rn 中保存的是一个内存的地址值 1. 内存写指令 [ str,strb,st ...
- JFinal教程
自学JFinal总结 前言:每次搭建ssm框架时,就像搬家一样,非常繁杂,并且还容易出错.正好了解到JFinal极简,无需配置即可使用,在这里记录下学习的过程. 感谢:非常感谢此网站发布的教程,非常详 ...
- zabbix active模式以及自定义key not Supported的解决
zabbix active模式 active模式适用场景 zabbix server端无法直连agent端,比如agent为内网机器,仅有内网ip,没有公网ip,但是内网机器能够访问server端 a ...
- [原创] delphi KeyUp、KeyPress、Keydown区别和用法,如何不按键盘调用事件
KeyPress (Sender: TObject; var Key: Char); 当用户按下键盘上的字符键(字母,数字) 会触发该事件,功能键则不会(F1-F12,Ctrl,Alt,Shift ...
- 【优化】碎片OPTIMIZE
来看看手册中关于 OPTIMIZE 的描述: OPTIMIZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, tbl_name] ... 如果您已经删除 ...
- 零距离接触阿里云时序时空数据库TSDB
概述 最近,Amazon新推出了完全托管的时间序列数据库Timestream,可见,各大厂商对未来时间序列数据库的重视与日俱增.阿里云TSDB是阿里巴巴集团数据库事业部研发的一款高性能分布式时序时空数 ...