简介
说明
本文介绍Lombok。
官网
Project Lombok
https://github.com/rzwitserloot/lombok
Lombok简介
Automatic Resource Management, automatic generation of getters, setters, equals, hashCode and toString, and more!
在项目使用了Lombok的情况下,安装lombok插件可以保证开发时的代码提示和代码检查的正常。
Lombok能以简单的注解形式来简化java代码,提高开发人员的开发效率。Lombok能通过注解的方式,在编译时自动为属性生成构造器、getter/setter、equals、hashcode、toString方法。在源码中没有getter和setter方法,但是在编译生成的字节码文件中有getter和setter方法。
当修改/增加属性时,往往需要修改toString(),getter/setter等,容易遗漏。Lombok可以自动生成。
Lombok实现原理
自从Java 6起,javac就支持“JSR 269 Pluggable Annotation Processing API”规范,只要程序实现了该API,就能在javac运行的时候得到调用。
Lombok就是一个实现了"JSR 269 API"的程序。在使用javac的过程中,它产生作用的具体流程如下:
1. javac对源代码进行分析,生成一棵抽象语法树(AST)
2. javac编译过程中调用实现了JSR 269的Lombok程序
3. 此时Lombok就对第一步骤得到的AST进行处理,找到Lombok注解所在类对应的语法树 (AST),然后修改该语法树(AST),增加Lombok注解定义的相应树节点
4. javac使用修改后的抽象语法树(AST)生成字节码文件
插件安装
1.引入依赖
maven:
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<scope>provided</scope>
</dependency>
grable:
其他网址:lombok官网(gradle部分)
法1:非插件法
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.12'
annotationProcessor 'org.projectlombok:lombok:1.18.12'
testCompileOnly 'org.projectlombok:lombok:1.18.12'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'
}
法2:插件法
plugins {
id("io.freefair.lombok") version "3.1.4"
}
2.IDE支持(本处以Idea为例)
File=>Settings=> Plugins=> 搜索“Lombok”并安装
File=>Settings=> Build,Execution,Deployment=> Compiler=> AnnocationProcessors=> 选中模块
=> Enable annocation processor
用法简介
其他网址
Lombok的基本使用 - 简书
使用Lombok的注解,可以添加相应方法,自己有自定义的方法时同样有效。例如:
package com.example;
public class User{
private Long id;
private String name;
private Integer age;
public User name(String name) {
this.name = name;
return this;
}
}
编译之后生成的文件
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.example;
public class User {
private Long id;
private String name;
private Integer age;
public User name(String name) {
this.name = name;
return this;
}
public User() {
}
public Long getId() {
return this.id;
}
public String getName() {
return this.name;
}
public Integer getAge() {
return this.age;
}
public void setId(Long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof User)) {
return false;
} else {
User other = (User)o;
if (!other.canEqual(this)) {
return false;
} else {
label47: {
Object this$id = this.getId();
Object other$id = other.getId();
if (this$id == null) {
if (other$id == null) {
break label47;
}
} else if (this$id.equals(other$id)) {
break label47;
}
return false;
}
Object this$name = this.getName();
Object other$name = other.getName();
if (this$name == null) {
if (other$name != null) {
return false;
}
} else if (!this$name.equals(other$name)) {
return false;
}
Object this$age = this.getAge();
Object 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 User;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $id = this.getId();
int result = result * 59 + ($id == null ? 43 : $id.hashCode());
Object $name = this.getName();
result = result * 59 + ($name == null ? 43 : $name.hashCode());
Object $age = this.getAge();
result = result * 59 + ($age == null ? 43 : $age.hashCode());
return result;
}
public String toString() {
return "User(id=" + this.getId() + ", name=" + this.getName() + ", age=" + this.getAge() + ")";
}
}
@Getter/@Setter:
作用类上,生成所有成员变量的getter/setter方法;作用于成员变量上,生成该成员变量的getter/setter方法。可以设定访问权限及是否懒加载等。例:@Getter(AccessLevel.PROTECTED)也可写为@Getter(value=AccessLevel.PROTECTED)
@ToString
作用于类,覆盖默认的toString()方法,可以通过of属性限定显示某些字段,通过exclude属性排除某些字段。
@EqualsAndHashCode
作用于类,覆盖默认的equals和hashCode
@NonNull
主要作用于成员变量和参数中,标识不能为空,否则抛出空指针异常。
@NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor
作用于类上,用于生成构造函数。
有staticName、access等属性。staticName属性一旦设定,将采用静态方法的方式生成实例,access属性可以限定访问权限。用到单例模式,这时需要将构造器私有化,例如:@NoArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor:生成无参构造器;
@RequiredArgsConstructor:生成包含final和@NonNull注解的成员变量的构造器;
@AllArgsConstructor:生成全参构造器
@Data
作用于类上,是以下注解的集合:@ToString @EqualsAndHashCode @Getter @Setter @RequiredArgsConstructor
@Builder
作用于类上,将类转变为建造者模式
@Log
作用于类上,生成日志变量。针对不同的日志实现产品,有不同的注解
@Cleanup
自动关闭资源,针对实现了java.io.Closeable接口的对象有效
@SneakyThrows
可以对受检异常进行捕捉并抛出
基础
@Getter/@Setter
为字段生成Getter和Setter方法,可以注解到字段或者类上(注解在类上会为类中的所有字段生成Getter和Setter方法),默认是public类型的,如果需要的话可以修改方法的访问级别。
public class User {
private Long id;
(AccessLevel.PROTECTED)
private String phone;
private String password;
}
编译后的代码:
public class User {
private Long id;
private String phone;
private String password;
public User() {
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
protected String getPhone() {
return this.phone;
}
}
结果解释:
id字段生成了Getter&Setter,访问修饰符是public(默认的)
phone只生成了Getter方法,因为只使用了@Getter而没有使用@Setter, 并且访问修饰符是protected
password 上并没有注解,所以什么都不生成
注意:Lombok中的注解一般都会包含一个无参构造函数注解@NoArgsConstructor(用于生成无参构造函数的) ,所以还会额外生成一个无参构造函数
@Getter @Setter 注解在类上,表示为类中的所有字段生成Getter&Setter方法。
public class User {
private Long id;
private String phone;
private String password;
}
public class User {
private Long id;
private String phone;
private String password;
public User() {
}
public Long getId() {
return this.id;
}
public String getPhone() {
return this.phone;
}
public String getPassword() {
return this.password;
}
public void setId(Long id) {
this.id = id;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setPassword(String password) {
this.password = password;
}
}
@Data
@Data 包含了 @ToString、@EqualsAndHashCode、@Getter / @Setter和@RequiredArgsConstructor的功能。
public class User {
private Long id;
private String phone;
private Integer status;
}
public class User {
private Long id;
private String phone;
private Integer status;
public User() {
}
public Long getId() {
return this.id;
}
public String getPhone() {
return this.phone;
}
public Integer getStatus() {
return this.status;
}
public void setId(Long id) {
this.id = id;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setStatus(Integer status) {
this.status = status;
}
public boolean equals(Object o) {
if(o == this) {
return true;
} else if(!(o instanceof User)) {
return false;
} else {
User other = (User)o;
if(!other.canEqual(this)) {
return false;
} else {
label47: {
Long this$id = this.getId();
Long other$id = other.getId();
if(this$id == null) {
if(other$id == null) {
break label47;
}
} else if(this$id.equals(other$id)) {
break label47;
}
return false;
}
String this$phone = this.getPhone();
String other$phone = other.getPhone();
if(this$phone == null) {
if(other$phone != null) {
return false;
}
} else if(!this$phone.equals(other$phone)) {
return false;
}
Integer this$status = this.getStatus();
Integer other$status = other.getStatus();
if(this$status == null) {
if(other$status != null) {
return false;
}
} else if(!this$status.equals(other$status)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof User;
}
public int hashCode() {
boolean PRIME = true;
byte result = 1;
Long $id = this.getId();
int result1 = result * 59 + ($id == null?43:$id.hashCode());
String $phone = this.getPhone();
result1 = result1 * 59 + ($phone == null?43:$phone.hashCode());
Integer $status = this.getStatus();
result1 = result1 * 59 + ($status == null?43:$status.hashCode());
return result1;
}
public String toString() {
return "User(id=" + this.getId() + ", phone=" + this.getPhone() + ", status=" + this.getStatus() + ")";
}
}
@Getter(lazy = true)
@Getter(lazy = true)
标注字段为懒加载字段,懒加载字段在创建对象时不会进行初始化,而是在第一次访问的时候才会初始化,后面再次访问也不会重复初始化。
public class User {
private final List<String> cityList = getCityFromCache();
private List<String> getCityFromCache() {
System.out.println("get city from cache ...");
return new ArrayList<>();
}
}
public static void main(String[] args) {
User user = new User(); // 初始化对象时会执行getCityFromCache()方法
}
public class User {
(lazy = true)
private final List<String> cityList = getCityFromCache();
private List<String> getCityFromCache() {
System.out.println("get city from cache ...");
return new ArrayList<>();
}
}
public class User {
private final AtomicReference<Object> cityList = new AtomicReference();
public User() {
}
private List<String> getCityFromCache() {
System.out.println("get city from cache ...");
return new ArrayList();
}
public List<String> getCityList() {
Object value = this.cityList.get();
if(value == null) {
AtomicReference var2 = this.cityList;
synchronized(this.cityList) {
value = this.cityList.get();
if(value == null) {
List actualValue = this.getCityFromCache();
value = actualValue == null?this.cityList:actualValue;
this.cityList.set(value);
}
}
}
return (List)((List)(value == this.cityList?null:value));
}
}
@Getter(lazy = true):为懒加载字段生成一个Getter方法
public static void main(String[] args) {
User user = new User(); // 创建对象时不会初始化懒加载的字段
List<String> cityList = user.getCityList(); // 只有第一次访问属性时才会去初始化
cityList = user.getCityList(); // 第二次就不会再次初始化了
}
@Value
@Value 将字段都变成不可变类型:使用final修饰, 同时还包含@ToString、@EqualsAndHashCode、@AllArgsConstructor 、@Getter(注意只有Getter没有Setter)
public class User {
private Long id;
private String username;
}
public final class User {
private final Long id;
private final String username;
public User(Long id, String username) {
this.id = id;
this.username = username;
}
public Long getId() {
return this.id;
}
public String getUsername() {
return this.username;
}
public boolean equals(Object o) {
if(o == this) {
return true;
} else if(!(o instanceof User)) {
return false;
} else {
User other = (User)o;
Long this$id = this.getId();
Long other$id = other.getId();
if(this$id == null) {
if(other$id != null) {
return false;
}
} else if(!this$id.equals(other$id)) {
return false;
}
String this$username = this.getUsername();
String other$username = other.getUsername();
if(this$username == null) {
if(other$username != null) {
return false;
}
} else if(!this$username.equals(other$username)) {
return false;
}
return true;
}
}
public int hashCode() {
boolean PRIME = true;
byte result = 1;
Long $id = this.getId();
int result1 = result * 59 + ($id == null?43:$id.hashCode());
String $username = this.getUsername();
result1 = result1 * 59 + ($username == null?43:$username.hashCode());
return result1;
}
public String toString() {
return "User(id=" + this.getId() + ", username=" + this.getUsername() + ")";
}
}
@NonNull
为字段赋值时(即调用字段的setter方法时),如果传的参数为null,则会抛出空异常NullPointerException,生成setter方法时会对参数是否为空检查。
public class User {
private Long id;
private String phone;
}
public class User {
private Long id;
private String phone;
public User() {
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getPhone() {
return this.phone;
}
public void setPhone( String phone) {
if(phone == null) {
throw new NullPointerException("phone");
} else {
this.phone = phone;
}
}
}
构造方法
@NoArgsConstructor
生成一个无参构造方法。 对于具有约束的字段(例如@NonNull字段),不会生成检查或分配,因此,正确初始化这些字段之前,这些约束无效。
注解 | 说明 |
@NoArgsConstructor(force = true) | 强制生成。当类中有final字段没有被初始化时,编译器会报错,此时可用force = true属性,会为没有初始化的final字段设置默认值 0 / false / null, 这样编译器就不会报错。 |
@NoArgsConstructor(access = AccessLevel.PRIVATE) | 将构造器权限设为private。有时使用单例模式,需要将构造器私有化。 |
@NoArgsConstructor(staticName = "xxx") | 生成静态的构造方法,原来的实例构造方法将会被私有( |
@NoArgsConstructor
public class User {
private String username;
private String password;
}
// 编译后:
public class User {
private String username;
private String password;
public User() { }
}
@NoArgsConstructor(access = AccessLevel.PRIVATE)
access = AccessLevel.PRIVATE)
public class User {
private String username;
private String password;
}
// 编译后:
public class User {
private String username;
private String password;
private User() {
}
}
(
@NoArgsConstructor(force = true)
force = true)
public class User {
private Long id;
private String phone;
private final Integer age;
}
(
public class User {
private Long id;
private String phone;
private final Integer age = null;
public User() {
}
}
@NoArgsConstructor(staticName = "UserHa")
staticName = "UserHa")
public class User {
private String username;
private String password;
}
// 编译后:
public class User {
private String username;
private String password;
private User() { }
public static User UserHa() {
return new User();
}
}
(
@RequiredArgsConstructor
注解 | 说明 |
@RequiredArgsConstructor | 生成静态的构造方法(public类型) |
@RequiredArgsConstructor(staticName = "xxx") | 生成静态的构造方法,原来的实例构造方法将会被私有( |
为需要的字段生成构造方法(可能带参数也可能不带参数)。需要的字段包括:以final修饰的未经初始化的字段、以@NonNull注解的未经初始化的字段。
public class User {
private Long id;
private String phone;
private Integer status = 0;
private final Integer age;
private final String country = "china";
}
public class User {
private Long id;
private String phone;
private Integer status = Integer.valueOf(0);
private final Integer age;
private final String country = "china";
public User( String phone, Integer age) {
if(phone == null) {
throw new NullPointerException("phone");
} else {
this.phone = phone;
this.age = age;
}
}
}
@AllArgsConstructor
注解 | 说明 |
@AllArgsConstructor(access = AccessLevel.PRIVATE) | 将构造器权限设为private。 |
@AllArgsConstructor(staticName = "xxx") | 生成静态的构造方法,原来的实例构造方法将会被私有( |
生成一个全参数的构造方法,默认不提供无参构造。这里的全参不包括已初始化的final字段
public class User {
private Long id;
private String phone;
private Integer status = 0;
private final Integer age;
private final String country = "china";
}
public class User {
private Long id;
private String phone;
private Integer status = Integer.valueOf(0);
private final Integer age;
private final String country = "china";
public User(Long id, String phone, Integer status, Integer age) {
if(phone == null) {
throw new NullPointerException("phone");
} else {
this.id = id;
this.phone = phone;
this.status = status;
this.age = age;
}
}
}
@Builder
@Builder:生成内部类和全字段的构造器。
public class User {
private Long id;
private String phone;
}
public class User {
private Long id;
private String phone;
User(Long id, String phone) {
this.id = id;
this.phone = phone;
}
public static User.UserBuilder builder() {
return new User.UserBuilder();
}
public static class UserBuilder {
private Long id;
private String phone;
UserBuilder() {
}
public User.UserBuilder id(Long id) {
this.id = id;
return this;
}
public User.UserBuilder phone(String phone) {
this.phone = phone;
return this;
}
public User build() {
return new User(this.id, this.phone);
}
public String toString() {
return "User.UserBuilder(id=" + this.id + ", phone=" + this.phone + ")";
}
}
}
覆写Object类的方法
@ToString
生成toString()方法,默认情况下它会按顺序(以逗号分隔)打印你的类名称以及每个字段。可以这样设置不包含哪些字段,可以指定一个也可以指定多个@ToString(exclude = “id”) / @ToString(exclude = {“id”,“name”})
如果继承的有父类的话,可以设置callSuper 让其调用父类的toString()方法,例如:@ToString(callSuper = true)
exclude = {"password", "salt"})
public class User {
private Long id;
private String phone;
private String password;
private String salt;
}
(
public class User {
private Long id;
private String phone;
private String password;
private String salt;
public User() {
}
public String toString() {
return "User(id=" + this.id + ", phone=" + this.phone + ")";
}
}
exclude = {"password", "salt"}, callSuper = true)
public class User {
private Long id;
private String phone;
private String password;
private String salt;
}
(
public class User {
private Long id;
private String phone;
private String password;
private String salt;
public User() {
}
public String toString() {
return "User(super=" + super.toString() + ", id=" + this.id + ", phone=" + this.phone + ")";
}
}
@EqualsAndHashCode
生成hashCode()和equals()方法,默认情况下,它将使用所有非静态,非transient字段。但可以通过在可选的exclude参数中来排除更多字段。或者,通过在of参数中命名它们来准确指定希望使用哪些字段。
// exclude 排除字段
@EqualsAndHashCode(exclude = {“password”, “salt”})
// of 指定要包含的字段
@EqualsAndHashCode(of = {“id”, “phone”, “password”})
public class User implements Serializable{
private static final long serialVersionUID = 6569081236403751407L;
private Long id;
private String phone;
private transient int status;
}
public class User implements Serializable {
private static final long serialVersionUID = 6569081236403751407L;
private Long id;
private String phone;
private transient int status;
public User() {
}
public boolean equals(Object o) {
if(o == this) {
return true;
} else if(!(o instanceof User)) {
return false;
} else {
User other = (User)o;
if(!other.canEqual(this)) {
return false;
} else {
Long this$id = this.id;
Long other$id = other.id;
if(this$id == null) {
if(other$id != null) {
return false;
}
} else if(!this$id.equals(other$id)) {
return false;
}
String this$phone = this.phone;
String other$phone = other.phone;
if(this$phone == null) {
if(other$phone != null) {
return false;
}
} else if(!this$phone.equals(other$phone)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof User;
}
public int hashCode() {
boolean PRIME = true;
byte result = 1;
Long $id = this.id;
int result1 = result * 59 + ($id == null?43:$id.hashCode());
String $phone = this.phone;
result1 = result1 * 59 + ($phone == null?43:$phone.hashCode());
return result1;
}
}
生成了 equals 、hashCode 和 canEqual 无参构造函数 四个方法。
常用
日志
生成log对象,用于记录日志,可以通过topic属性来设置getLogger(String name)方法的参数 例如 @Log4j(topic = “com.xxx.entity.User”),默认是类的全限定名,即 类名.class,log支持以下几种:
注解 | 对应包 |
@Log | java.util.logging.Logger |
@Log4j | org.apache.log4j.Logger |
@Log4j2 | org.apache.logging.log4j.Logger |
@Slf4j | org.slf4j.Logger |
@XSlf4j | org.slf4j.ext.XLogger |
@CommonsLog | org.apache.commons.logging.Log |
@JBossLog | org.jboss.logging.Logger |
private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());
private static final Logger log = org.apache.log4j.Logger.Logger.getLogger(UserService.class);
private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);
private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class);
public class UserService {
public void addUser(){
log.info("add user");
}
}
import java.util.logging.Logger;
public class UserService {
private static final Logger log = Logger.getLogger(UserService.class.getName());
public UserService() {
}
}
@Accessors
@Accessors:用于配置getter和setter方法的生成结果
chain
设置为true,则setter方法返回当前对象。
(chain = true)
public class User {
private Long id;
private String name;
// 生成的setter方法如下,方法体略
public User setId(Long id) {}
public User setName(String name) {}
}
fluent
fluent的中文含义是流畅的,设置为true,则getter和setter方法的方法名都是基础属性名,且setter方法返回当前对象。
(fluent = true)
public class User {
private Long id;
private String name;
// 生成的getter和setter方法如下,方法体略
public Long id() {}
public User id(Long id) {}
public String name() {}
public User name(String name) {}
}
prefix
用于生成getter和setter方法的字段名会忽视指定前缀(遵守驼峰命名)。
(prefix = "p")
class User {
private Long pId;
private String pName;
// 生成的getter和setter方法如下,方法体略
public Long getId() {}
public void setId(Long id) {}
public String getName() {}
public void setName(String name) {}
}
其他
@SneakyThrows
使用try catch 来捕获异常, 默认捕获的是Throwable异常,也可以设置要捕获的异常
public class User {
public void sleep(){
Thread.sleep(1000);
}
(InterruptedException.class)
public void sleep2() {
Thread.sleep(1000);
}
}
public class User {
public User() {
}
public void sleep() {
try {
Thread.sleep(1000L);
} catch (Throwable var2) {
throw var2;
}
}
public void sleep2() {
try {
Thread.sleep(1000L);
} catch (InterruptedException var2) {
throw var2;
}
}
public static void main(String[] args) {
}
}
@Synchronized
给方法加上同步锁
public class User {
private final Object readLock = new Object();
public static void foo(){
System.out.println();
}
public void bar(){
System.out.println();
}
("readLock")
public void test() {
System.out.println();
}
}
public class User {
private static final Object $LOCK = new Object[0];
private final Object $lock = new Object[0];
private final Object readLock = new Object();
public User() {
}
public static void foo() {
Object var0 = $LOCK;
synchronized($LOCK) {
System.out.println();
}
}
public void bar() {
Object var1 = this.$lock;
synchronized(this.$lock) {
System.out.println();
}
}
public void test() {
Object var1 = this.readLock;
synchronized(this.readLock) {
System.out.println();
}
}
}
@Cleanup
主要用来修饰 IO 流相关类, 会在 finally 代码块中对该资源进行 close();
public class CleanupExample {
public static void main(String[] args) throws IOException {
InputStream in = new FileInputStream(args[0]);
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);
}
}
}
public class CleanupExample {
public static void main(String[] args) throws IOException {
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();
}
}
}
}
@Wither
提供了给final字段赋值的一种方法
public class User {
private final String country;
public User(String country) {
this.country = country;
}
}
public class User {
private final String country;
public User(String country) {
this.country = country;
}
public User withCountry(String country) {
return this.country == country?this:new User(country);
}
}
@Delegate
为List类型的字段生成一大堆常用的方法,其实这些方法都是List中的方法
注意:一个类中只能使用一个@Delegate注解,因为使用多个会生成多个size()方法,从而会编译报错。
public class User {
private List<String> address;
}
public class User {
private List<String> address;
public User() {
}
public int size() {
return this.address.size();
}
public boolean isEmpty() {
return this.address.isEmpty();
}
public boolean contains(Object arg0) {
return this.address.contains(arg0);
}
public Iterator<String> iterator() {
return this.address.iterator();
}
public Object[] toArray() {
return this.address.toArray();
}
public <T> T[] toArray(T[] arg0) {
return this.address.toArray(arg0);
}
public boolean add(String arg0) {
return this.address.add(arg0);
}
public boolean remove(Object arg0) {
return this.address.remove(arg0);
}
public boolean containsAll(Collection<?> arg0) {
return this.address.containsAll(arg0);
}
public boolean addAll(Collection<? extends String> arg0) {
return this.address.addAll(arg0);
}
public boolean addAll(int arg0, Collection<? extends String> arg1) {
return this.address.addAll(arg0, arg1);
}
public boolean removeAll(Collection<?> arg0) {
return this.address.removeAll(arg0);
}
public boolean retainAll(Collection<?> arg0) {
return this.address.retainAll(arg0);
}
public void replaceAll(UnaryOperator<String> arg0) {
this.address.replaceAll(arg0);
}
public void sort(Comparator<? super String> arg0) {
this.address.sort(arg0);
}
public void clear() {
this.address.clear();
}
public String get(int arg0) {
return (String)this.address.get(arg0);
}
public String set(int arg0, String arg1) {
return (String)this.address.set(arg0, arg1);
}
public void add(int arg0, String arg1) {
this.address.add(arg0, arg1);
}
public String remove(int arg0) {
return (String)this.address.remove(arg0);
}
public int indexOf(Object arg0) {
return this.address.indexOf(arg0);
}
public int lastIndexOf(Object arg0) {
return this.address.lastIndexOf(arg0);
}
public ListIterator<String> listIterator() {
return this.address.listIterator();
}
public ListIterator<String> listIterator(int arg0) {
return this.address.listIterator(arg0);
}
public List<String> subList(int arg0, int arg1) {
return this.address.subList(arg0, arg1);
}
public Spliterator<String> spliterator() {
return this.address.spliterator();
}
}
lombok.config
lombok.config配置文件是通过一些设置来控制代码生成的规则或者称之为习惯,配置文件的位置应放在src/mian/java,不要放置在src/main/resources。
注意配置文件和要使用注解的类要在同一套代码中,要么同时在src/main/java 要么同时在 src/test/java中
lombok.config
#lombok 默认对boolean类型字段生成的get方法使用is前缀, 通过此配置则使用get前缀,默认: false
lombok.getter.noIsPrefix=true
#默认的set方法返回void设置为true返回调用对象本身,这样方便使用链式来继续调用方法,默认: false
lombok.accessors.chain=true
#如果设置为true get和set方法将不带get,set前缀, 直接以字段名为方法名, 默认: false
lombok.accessors.fluent=true
#设置log类注解返回的字段名称,默认: log
lombok.log.fieldName=logger
public class User {
private String username;
private boolean vip;
private boolean isOldUser;
}
public class User {
private static final Logger logger = Logger.getLogger(User.class);
private String username;
private boolean vip;
private boolean isOldUser;
public User() {
}
public String username() {
return this.username;
}
public boolean vip() {
return this.vip;
}
public boolean isOldUser() {
return this.isOldUser;
}
public User username(String username) {
this.username = username;
return this;
}
public User vip(boolean vip) {
this.vip = vip;
return this;
}
public User isOldUser(boolean isOldUser) {
this.isOldUser = isOldUser;
return this;
}
}