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:/ ...
随机推荐
- BBS论坛 home主页与个人站点主页
五.Home主页与个人站点主页 home首页代码: # views.py视图函数部分代码def home(request): # 将数据库中的所有文章展示到主页 article_list = mode ...
- cdn 的配置及原理
CDN概况 CDN的全称是Content Delivery Network,即内容分发网络. CND加速主要是加速静态资源,如网站上面上传的图片.媒体,以及引入的一些Js.css等文件. CND加速需 ...
- lambda和DynamoDB连接
在DynamoDB当作创建一个新表,然后在项目当中创建,把string value的值填写一下. 在IAM创建角色附加 AWSLambdaDynamoDBExecutionRole权限 在lambda ...
- Loop Sql
-- Numeric FOR loop -- set serveroutput on -->> do not use in TOAD -- DECLARE k ; BEGIN .. LOO ...
- C#常用设计模式
1.单例模式 单例模式恐怕是很多开发人员最先接触到的模式之一,可以认为就是一个全局变量.它的初始化过程无非就是一开始就new 一个instance,或者惰性初始化等需要用到的时候new 一个insta ...
- 解析Spring第四天(Spring中的事物、Spring框架来管理模板类)
JDBC模板技术: Spring框架中提供了很多持久层的模板类来简化编程,使用模板类编写程序会变的简单 template 模板 都是Spring框架提供XxxTemplate 提供了JDBC模板,Sp ...
- 矩阵乘法分配律+bitset优化——hdu4920
因为是模3,所以把原矩阵拆成两个01矩阵,然后按分配律拆开分别进行矩阵乘法,行列用bitset来存进行优化即可 注意 int bitset<int>::count() 函数可以统计bits ...
- paper 16 : 计算机视觉领域博客资源
这是收录的图像视觉领域的博客资源的第一部分,包含:中国内地.香港.台湾 这些名人大家一般都熟悉,本文仅收录了包含较多资料的个人博客,并且有不少更新,还有些名人由于分享的paper.code或者数据集不 ...
- NX二次开发-UFUN更改视图比例大小UF_DRAW_set_view_scale
#include <uf.h> #include <uf_draw.h> #include <uf_drf.h> #include <uf_obj.h> ...
- NX二次开发-UFUN和NXOpen结合开发中Tag_t对象与TaggedObject对象转换方法
本文通过举四个例子来告诉大家在NX二次开发过程中会经常用到UFUN和NXOpen结合去开发,在UFUN中我们得到的是Tag_t对象,在NXOpen中得到的是TaggedObject对象,这两个是需要进 ...