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:/ ...
随机推荐
- 匈牙利算法实战codevs1022覆盖
1022 覆盖 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 查看运行结果 题目描述 Description 有一个N×M的单位方格中 ...
- canvas 画一条折线
设置画布对象 canvas id="myCanvas" ref="canvas" //获取Canvas对象(画布) var canvas = document. ...
- react 中刷新,路由传参数丢失不存在了?
你可能在Link to没写state {{pathname:'/report',state:{storageClear:this.state.storageClear}}}
- Mac OS X终端的常用操作命令(UNIX指令)
用了十多年windows,终于换了个高配Mac,俗话说 无论前端还是后端最终还是走向了linux,无论是换了多少台PC最终都会走向Mac.不学习命令行用什么Mac? 干就完了~ pwd 显示现在的文件 ...
- ubuntu安装WPS替代office
安装 1.下载地址:http://community.wps.cn/download/(去WPS官网下载) 下载第一个即可 2.执行安装命令: sudo dpkg -i wps-office_10.1 ...
- Redis 小调研
一. 概况: Redis是一款开源的.网络化的.基于内存的.可进行数据持久化的Key-Value存储系统.它的数据模型建立在外层,类似于其它结构化存储系统,是通过Key映射Value的方式来建立字典以 ...
- zero udp
Description UDP transport can only be used with the ZMQ_RADIO and ZMQ_DISH socket types.
- JAVAWEB之文件的上传和下载
一.文件的上传: Enctype的属性介绍: 基于表单文件上传的界面简介: 文件上传时服务器端获取不到请求信息的原因及获取请求信息的几种方式: 输入流方式的实现: 实用工具包的实现:要导入fileup ...
- bootstrap img自适应
img 添加class名img-responsive适配屏幕
- WPF 模仿 UltraEdit 文件查看器系列 开篇和导读
WPF 模仿 UltraEdit 文件查看器系列 开篇和导读 运行环境:Win10 x64, NetFrameWork 4.8, 作者:乌龙哈里,日期:2019-05-10 学 .Net FrameW ...