Lombok 详解
简介
lombok是一个编译级别的插件,它可以在项目编译的时候生成一些代码。通俗的说,lombok可以通过注解来标示生成getter settter等代码。
引入
创建gradle项目
compile group: 'org.projectlombok', name: 'lombok', version: '1.16.20'
注解
@NonNull
标记字段不可为null
@Setter
public class Person {
@NonNull
private String name;
@NonNull
private Integer age;
}
对应的字节码文件:
public class Person {
@NonNull
private String name;
@NonNull
private Integer age;
public Person() {
}
public void setName(@NonNull String name) {
if (name == null) {
throw new NullPointerException("name");
} else {
this.name = name;
}
}
public void setAge(@NonNull Integer age) {
if (age == null) {
throw new NullPointerException("age");
} else {
this.age = age;
}
}
}
@Getter/@Setter
自动生成getter和setter方法
public class Person {
@Getter
private String name;
@Setter
private Integer age;
}
对应的字节码文件:
public class Person {
private String name;
private Integer age;
public Person() {
}
public String getName() {
return this.name;
}
public void setAge(Integer age) {
this.age = age;
}
}
@Cleanup
自动关闭流代码
@Cleanup
InputStream in = new FileInputStream(args[0]);
对应的字节码文件:
InputStream in = new FileInputStream(args[0]);
if (Collections.singletonList(in).get(0) != null) {
in.close();
}
@AllArgsConstructor/@NoArgsConstructor/@RequiredArgsConstructor
自动生成全参构造函数和无参构造函数
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private String name;
private Integer age;
}
对应的字节码文件
public class Person {
private String name;
private Integer age;
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public Person() {
}
}
@Builder
自动生成建造者模式的bean
@Builder
public class Person {
private String name;
private Integer age;
}
对应的字节码文件
public class Person {
private String name;
private Integer age;
Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public static Person.PersonBuilder builder() {
return new Person.PersonBuilder();
}
public static class PersonBuilder {
private String name;
private Integer age;
PersonBuilder() {
}
public Person.PersonBuilder name(String name) {
this.name = name;
return this;
}
public Person.PersonBuilder age(Integer age) {
this.age = age;
return this;
}
public Person build() {
return new Person(this.name, this.age);
}
public String toString() {
return "Person.PersonBuilder(name=" + this.name + ", age=" + this.age + ")";
}
}
}
@EqualsAndHashCode
自动生成equals和hashcode方法
@EqualsAndHashCode
public class Person {
private String name;
private Integer age;
}
对应的字节码文件
public class Person {
private String name;
private Integer age; public Person() {
} public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Person)) {
return false;
} else {
Person other = (Person)o;
if (!other.canEqual(this)) {
return false;
} else {
Object this$name = this.name;
Object other$name = other.name;
if (this$name == null) {
if (other$name != null) {
return false;
}
} else if (!this$name.equals(other$name)) {
return false;
} Object this$age = this.age;
Object other$age = other.age;
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 Person;
} public int hashCode() {
int PRIME = true;
int result = 1;
Object $name = this.name;
int result = result * 59 + ($name == null ? 43 : $name.hashCode());
Object $age = this.age;
result = result * 59 + ($age == null ? 43 : $age.hashCode());
return result;
}
}
@ToString
自动生成toString()方法
@ToString
public class Person {
private String name;
private Integer age;
}
对应的字节码文件
public class Person {
private String name;
private Integer age;
public Person() {
}
public String toString() {
return "Person(name=" + this.name + ", age=" + this.age + ")";
}
}
@Value
自动生成全参构造函数、Getter方法、equals方法、hashCode法、toString方法
@Value
public class Person {
private String name;
private Integer age;
}
注意:@Value不会生成Setter方法
@Synchronized
自动为被标记的方法添加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");
}
}
}
@Delegate
为标记属性生成委托方法
public class DelegateExample {
public void show() {
System.out.println("show...");
}
}
@AllArgsConstructor
public class Demo {
@Delegate
private final DelegateExample delegateExample;
}
对应的字节码文件
public class DelegateExample {
public DelegateExample() {
}
public void show() {
System.out.println("show...");
}
}
public class Demo {
private final DelegateExample delegateExample;
public Demo(DelegateExample delegateExample) {
this.delegateExample = delegateExample;
}
// 委托方法
public void show() {
this.delegateExample.show();
}
}
Lombok 详解的更多相关文章
- lombok+slf4j+logback SLF4J和Logback日志框架详解
maven 包依赖 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lomb ...
- Lombok之使用详解
前言 在Java中,封装是一个非常好的机制,最常见的封装莫过于get,set方法了,无论是Intellij idea 还是Eclipse,都提供了快速生成get,set方法的快捷键,使用起来很是方便, ...
- Lombok使用详解(转)
本文转自https://blog.csdn.net/u010695794/article/details/70441432 2017年04月22日 15:17:00 阅读数:10394 Lombok使 ...
- Lombok 使用详解,简化Java编程
前言 在 Java 应用程序中存在许多重复相似的.生成之后几乎不对其做更改的代码,但是我们还不得不花费很多精力编写它们来满足 Java 的编译需求 比如,在 Java 应用程序开发中,我们几乎要为所有 ...
- Netty4.x整合SpringBoot2.x使用Protobuf3详解
前言 本篇文章主要介绍的是SpringBoot整合Netty以及使用Protobuf进行数据传输的相关内容.Protobuf会介绍下用法,至于Netty在netty 之 telnet HelloWor ...
- java对象池commons-pool-1.6详解(一)
自己的项目中用到了 对象池 commons-pool: package com.sankuai.qcs.regulation.protocol.client; import com.dianping. ...
- spring事务详解(四)测试验证
系列目录 spring事务详解(一)初探事务 spring事务详解(二)简单样例 spring事务详解(三)源码详解 spring事务详解(四)测试验证 spring事务详解(五)总结提高 一.引子 ...
- Java Annotation详解 理解和使用Annotation
系统中用到了java注解: 查了一下如何使用注解,到底注解是什么: (1)创建方法:MsgTrace Java Class==> 在Create New Class中: name:输入MsgTr ...
- SpringBoot27 JDK动态代理详解、获取指定的类类型、动态注册Bean、接口调用框架
1 JDK动态代理详解 静态代理.JDK动态代理.Cglib动态代理的简单实现方式和区别请参见我的另外一篇博文. 1.1 JDK代理的基本步骤 >通过实现InvocationHandler接口来 ...
随机推荐
- HDU_1455_dfs
http://acm.hdu.edu.cn/showproblem.php?pid=1455 int dfs(int all,int sum,int now),all代表剩余总长,sum,代表每段长, ...
- HDU_2510_打表
http://acm.hdu.edu.cn/showproblem.php?pid=2510 dfs打表. #include<iostream> #include<cstdio> ...
- 关于AR眼镜的小结
根据IDC(International Data Corporation,国际数据公司,IDC是全球著名的信息技术.电信行业和消费科技咨询.顾问和活动服务专业提供商)的报告,2018年AR和VR的产值 ...
- Golang设置https访问,以及http如何重定向到https
设置https访问: 原始代码为http监听: func main() { server := &http.Server{ Addr: ":8080", ... } go ...
- Apache httpd.conf配置文件 3(虚拟主机)
### Section 3: Virtual Hosts 第三部分 虚拟主机 注意:在使用虚拟主机前,请先检查 http.conf 的 辅助配置文件httpd-vhosts.conf 是否注释 # ...
- Linux 软件安装卸载 (源码、rpm)
Linux下软件的安装主要有两种不同的形式.第一种安装为源码安装,文件名为xxx.tar.gz压缩包为主;以第一种方式发行的软件多为以源码形式发送的.第二种方式则是另一种安装文件名为xxx.i386. ...
- SHELL下打包文件
SHELL下打包文件 在我们拿下webshell的时候,想要获取数据或者源码往往会用菜刀或者蚁剑去打包,但是这个时候往往就会出现很多问题,列如打包失败,或者是打包得不完整等等. 这个时候如果对方是wi ...
- js模拟post提交表单
function post(URL, PARAMS) { var temp = document.createElement("form"); ...
- lua学习之深入函数第二篇
深入函数 2 非全局的函数 函数是第一类值,函数可以存储到全局变量,局部变量,table 字段中 lua 函数库中的大部分函数存储到 table 字段中 Lib = {} Lib.foo = func ...
- 用msi安装MySQL时MySQL Server组件不能安装,或安装失败
我的环境: MySQL8.0.15, win10 错误描述:在安装MySQL时,如果MySQL Server组件提示不能安装,错误提示是:VS 2015没有安装或安装失败.原因 ...