lombok其实可以算是一个小型的插件,它的使用要有两个条件(我使用的是idea工具):
1、安装lombok插件
2、引入lombok的jar包
安装:
ctrl+alt+s先打开setting界面,然后搜索plugins:
点击Plugins,然后点击Browse Repositories...,在弹出框中输入:lombok
然后点击右边安装即可。
注意有的时候在线安装会不成功能,那么可以使用本地安装,这个时候在返回到原先的Plugins的界面,然后点击
install plugins from disk..找到你下载的安装包,然后就可以安装了(https://plugins.jetbrains.com/pluginManager/?action=download&id=Lombook%20Plugin&build=IU-182.4892.20&uuid=d3b6bec1-6e7b-4d32-adc0-bb0f8d965c3b)。
这个是我下载的 lombok-plugin-0.23-2018.2.zip。如果还是下载不到的话,就来这里下载:
https://pan.baidu.com/s/1EuSkOR6jEomnWSBkcEwBcw 提取码:lm2y
引入jar包
如果你是使用maven的话,就直接引入配置:
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
如果只是简单的java功能,则可以下载改jar,创建lib,引入,add libararies 添加到项目中也可以使用。
使用
@Getter为该类的属性加上getter的方法(跟alt + Insert 选中getter方法和所有属性生成的一样)。
有个属性lazy,不过要求作用于属性上,同时要求属性是private final 同时要有初始化的值,例如:
@Getter(lazy = true)
private final int cons = 10;
实际生成代码:
public int getCons() {
Object value = this.cons.get();
if (value == null) {
AtomicReference var2 = this.cons;
synchronized(this.cons) {
value = this.cons.get();
if (value == null) {
int actualValue = true;
value = 10;
this.cons.set(value);
}
}
}
return (Integer)value;
}
@Setter为该类的属性加上setter的方法。
get、set两个方法都可以指定accessLevel=AccessLevel.PROTECTED代表生成访问权限为 protected的方法。
@AllArgsConstructor会生成所有的参数的构造器,参数的顺序就是属性定义的顺序。
@RequiredArgsConstructor指定参数的构造器,对变量中加了@NonNull的属性,会加入 到构造器中,注意无法生成重载的构造器,如果将所有的参数都加上@NonNull属性且 同时已经有了全参的构造器,会报错。
@NoArgsConstructor会生成无参数构造器。
上述三个构造器的生成方式都有属性staticName,如果指定某个值为my的话,则对应的 构造器会变成private,同时生成一个调用该构造器的public static方法,属性access 用于 设置构造器的访问权限,类型为访问权限的枚举类型
@Data会生成很多getter、setter、toString、hashCode等方法。
@Data
public class DataPerson {
private int age;
private String name;
}
实际生成代码:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package cn.itouchtv.test;
public class DataPerson {
private int age;
private String name;
public DataPerson() {
}
public int getAge() {
return this.age;
}
public String getName() {
return this.name;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof DataPerson)) {
return false;
} else {
DataPerson other = (DataPerson)o;
if (!other.canEqual(this)) {
return false;
} else if (this.getAge() != other.getAge()) {
return false;
} else {
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;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof DataPerson;
}
public int hashCode() {
int PRIME = true;
int result = 1;
int result = result * 59 + this.getAge();
Object $name = this.getName();
result = result * 59 + ($name == null ? 43 : $name.hashCode());
return result;
}
public String toString() {
return "DataPerson(age=" + this.getAge() + ", name=" + this.getName() + ")";
}
}
@Value同样会生成getter、toString、hashCode等方法,跟@Data一样,但是没有setter方法,同时这个注 解会默认给属性加上final修饰符号,所以没有setter方法。
import lombok.Value;
@Value
public class DataPerson {
private int age;
private String name;
}
实际生成代码:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package cn.itouchtv.test;
public final class DataPerson {
private final int age;
private final String name;
public DataPerson(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return this.age;
}
public String getName() {
return this.name;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof DataPerson)) {
return false;
} else {
DataPerson other = (DataPerson)o;
if (this.getAge() != other.getAge()) {
return false;
} else {
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;
}
return true;
}
}
}
public int hashCode() {
int PRIME = true;
int result = 1;
int result = result * 59 + this.getAge();
Object $name = this.getName();
result = result * 59 + ($name == null ? 43 : $name.hashCode());
return result;
}
public String toString() {
return "DataPerson(age=" + this.getAge() + ", name=" + this.getName() + ")";
}
}
@Slf4j注释在类上,可以直接使用log来调用log.info等方法
加入这个注解,会在编译时加入:
private static final Logger log = LoggerFactory.getLogger(Person.class)这个代码。
@Log注解和@Slf4j类似,但是引入的是java.util.logging日志
@Synchronized:对象同步
给某个方法加上该注解:例如
@Synchronized
public static String getName1(Person person){
return person.getName();
}
实际编译时会生成:
public static String getName1(Person person) {
Object var1 = $LOCK;
synchronized($LOCK) {
return person.getName();
}
}
@SneakyThrows:抛出异常
@SneakyThrows
public void operateStream(){
InputStream inputStream = new FileInputStream(new File("cn/itouchtv/file.xml"));
}
生成以下代码:
public void operateStream() {
try {
InputStream inputStream = new FileInputStream(new File("cn/itouchtv/file.xml"));
} catch (Throwable var2) {
throw var2;
}
}
同时可以指定需要抛出的异常, @SneakyThrows(Exception.class),此时就会用 Exception来catch异常
@Cleanup 主动关闭流
try {
@Cleanup
InputStream inputStream = new FileInputStream(new File("cn/itouchtv/file.xml"));
} catch (Exception e) {
e.printStackTrace();
}
会生成代码如下:
try {
InputStream inputStream = new FileInputStream(new File("cn/itouchtv/file.xml"));
if (Collections.singletonList(inputStream).get(0) != null) {
inputStream.close();
}
} catch (Exception var2) {
var2.printStackTrace();
}
注意如果外层的try catch异常 var2 无法捕捉close的异常,将会报错。
@NonNull 非空判断,如果为null,会判处异常,提供具体的异常信息(好像不能定制抛出的异常信息,感觉不太好,还不如我自己判断)
public static String getName1(@NonNull Person person) {
if (person == null) {
throw new NullPointerException("person is marked @NonNull but is null");
} else {
return person.getName();
}
@ToString 生成tostring方法。
最终来一个除了@Data和@Value注解之外的例子:
package cn.itouchtv.test;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
@ToString
@Getter
@Setter
@Slf4j
@RequiredArgsConstructor
@NoArgsConstructor
@AllArgsConstructor
public class DataPerson {
@Getter(lazy = true)
private final String idNumber = "121231231231";
@NonNull
private int age;
@NonNull
private String name;
private String address;
public String getMyName(@NonNull DataPerson person){
return person.getName();
}
@SneakyThrows
public void throwException(){
System.out.println("抛出异常。。。");
}
@Synchronized
public void synchronizeMethod(){
System.out.println("同步。。。");
}
public void tryCatchMethod(){
try {
@Cleanup
InputStream inputStream = new FileInputStream(new File("cn/itouchtv/file.xml"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
实际生成代码:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package cn.itouchtv.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicReference;
import lombok.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DataPerson {
private static final Logger log = LoggerFactory.getLogger(DataPerson.class);
private final Object $lock = new Object[0];
private final AtomicReference<Object> idNumber = new AtomicReference();
@NonNull
private int age;
@NonNull
private String name;
private String address;
public String getMyName(@NonNull DataPerson person) {
if (person == null) {
throw new NullPointerException("person is marked @NonNull but is null");
} else {
return person.getName();
}
}
public void throwException() {
try {
System.out.println("抛出异常。。。");
} catch (Throwable var2) {
throw var2;
}
}
public void synchronizeMethod() {
Object var1 = this.$lock;
synchronized(this.$lock) {
System.out.println("同步。。。");
}
}
public void tryCatchMethod() {
try {
InputStream inputStream = new FileInputStream(new File("cn/itouchtv/file.xml"));
if (Collections.singletonList(inputStream).get(0) != null) {
inputStream.close();
}
} catch (Exception var2) {
var2.printStackTrace();
}
}
public String toString() {
return "DataPerson(idNumber=" + this.getIdNumber() + ", age=" + this.getAge() + ", name=" + this.getName() + ", address=" + this.getAddress() + ")";
}
@NonNull
public int getAge() {
return this.age;
}
@NonNull
public String getName() {
return this.name;
}
public String getAddress() {
return this.address;
}
public void setAge(@NonNull int age) {
this.age = age;
}
public void setName(@NonNull String name) {
if (name == null) {
throw new NullPointerException("name is marked @NonNull but is null");
} else {
this.name = name;
}
}
public void setAddress(String address) {
this.address = address;
}
public DataPerson(@NonNull int age, @NonNull String name) {
if (name == null) {
throw new NullPointerException("name is marked @NonNull but is null");
} else {
this.age = age;
this.name = name;
}
}
public DataPerson() {
}
public DataPerson(@NonNull int age, @NonNull String name, String address) {
if (name == null) {
throw new NullPointerException("name is marked @NonNull but is null");
} else {
this.age = age;
this.name = name;
this.address = address;
}
}
public String getIdNumber() {
Object value = this.idNumber.get();
if (value == null) {
AtomicReference var2 = this.idNumber;
synchronized(this.idNumber) {
value = this.idNumber.get();
if (value == null) {
String actualValue = "121231231231";
value = "121231231231" == null ? this.idNumber : "121231231231";
this.idNumber.set(value);
}
}
}
return (String)((String)(value == this.idNumber ? null : value));
}
}