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:/ ...
随机推荐
- 代码控制PrivateBinPath和ConfigurationFile的位置
原文:代码控制PrivateBinPath和ConfigurationFile的位置 .Net的WinForm程序有的时候让人很烦的是,在执行目录下总是一大堆的DLL,配置文件,最少则是个以下,多的时 ...
- what codes does sudo command do in Linux?
sometime, to make your change of configuration file be effective to web application, we have to rest ...
- Sql Server的内存策略
最近碰到有人问我在使用sql server的时候,内存突然升高,但是没有log日志进行详细的调查,有没有什么解决办法. 在此我经过一番查询,发现了2种能够对内存进行一定优化限制的方法. 在数据库上点击 ...
- mysql的下载
怎样从Mysql官网下载mysql.tar.gz版本的安装包 原创 2016年10月20日 21:06:41 10854 今天学习在Linux上部署项目,用到了Mysql,因此想要下载适用 ...
- Effective C++之条款1:视C++为一个语言联邦
C++中的sub-languages有如下四种: C Object-Oriented C++: (classes ,encapsulation(封装),inheritance(继承),polymo ...
- zabbix active模式以及自定义key not Supported的解决
zabbix active模式 active模式适用场景 zabbix server端无法直连agent端,比如agent为内网机器,仅有内网ip,没有公网ip,但是内网机器能够访问server端 a ...
- WPS Office for Mac如何修改Word文档文字排列?WPS office修改Word文档文字排列方向教程
Word文档如何改变文字的排列方向?最新版WPS Office for Mac修复了文字排版相关的细节问题,可以更快捷的进行Word编辑,WPS Office在苹果电脑中如何修改Word文档文字排列方 ...
- __new__构造方法
""" 对象的创建过程:new创建 返回 模拟实例对象的创建过程. 为啥是静态方法? 先有new后来init.因为init是需要实例对象来调用的,需要一个实例对象和sel ...
- R语言 运算符
R语言运算符 运算符是一个符号,通知编译器执行特定的数学或逻辑操作. R语言具有丰富的内置运算符,并提供以下类型的运算符. 运算符的类型 R语言中拥有如下几种运算符类型: 算术运算符 关系运算符 逻辑 ...
- 【安装】Mac rabbitMQ
安装 brew install rabbitmq 目录 cd /usr/local/Cellar/rabbitmq/3.7.4/sbin 插件 sudo ./rabbitmq-plugins ena ...