前言
以前 写一个pojo的时候,我们可能需要为他设置getter 和setter方法,使用logger在程序中打印日志的时候我们可能需要new 一个logger的对象出来,使用tostring的时候我们可能需要复写一个类的tostring 方法。但是拥有了lombok我们就可以,不需要写这些那么繁琐的步骤了。
下载lombok的jar
下载的地址是:https://projectlombok.org/download.html
安装lombok
打开cmd 窗口,打开lombok 的目录,然后执行:
Java -jar lombok.jar
然后它会弹出个窗口,选择你的eclipse,点击安装就可以了,然后重启eclipse就可以使用了。
使用说明
@Getter and @Setter
可以很直观的从名字看出这个两个是分别用来生成Getter和Setter方法的
public class User{
@Getter @Setter private boolean employed = true;
@Setter(AccessLevel.PROTECTED) private String name;
}
相当于
public class User {
private boolean employed = true;
private String name;
public User() {
}
public boolean isEmployed() {
return this.employed;
}
public void setEmployed(boolean employed) {
this.employed = employed;
}
protected void setName(String name) {
this.name = name;
}
}
@NonNull
提供一个参数的非空判断
@Getter @Setter @NonNull
private List<String> members;
等同于
@NonNull
private List<String> members;
@NonNull
public List<String> getMembers() {
return this.members;
}
public void setMembers(@NonNull List<String> members) {
if(members == null) {
throw new NullPointerException("members");
} else {
this.members = members;
}
}
@ToString
@ToString(exclude="name")
public class User {
@Getter
@Setter
private boolean employed = true;
@Setter(AccessLevel.PROTECTED)
private String name;
@Getter
@Setter
@NonNull
private List<String> members;
}
等同于
public String toString() {
return "User(employed=" + this.isEmployed() + ", members=" + this.getMembers() + ")";
}
@EqualsAndHashCode
@EqualsAndHashCode(exclude={"name"})
public class User {
@Getter
@Setter
private boolean employed = true;
@Setter(AccessLevel.PROTECTED)
private String name;
@Getter
@Setter
@NonNull
private List<String> members;
}
等同于
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 if(this.isEmployed() != other.isEmployed()) {
return false;
} else {
List this$members = this.getMembers();
List other$members = other.getMembers();
if(this$members == null) {
if(other$members != null) {
return false;
}
} else if(!this$members.equals(other$members)) {
return false;
}
return true;
}
}
}
public boolean canEqual(Object other) {
return other instanceof User;
}
public int hashCode() {
boolean PRIME = true;
byte result = 1;
int result1 = result * 59 + (this.isEmployed()?79:97);
List $members = this.getMembers();
result1 = result1 * 59 + ($members == null?0:$members.hashCode());
return result1;
}
@Data
这个注解相当于同时使用@ToString, @EqualsAndHashCode, @Getter和@Setter@Cleanup
他可以帮我们在需要释放的资源位置自动加上释放代码public void testCleanUp() {
try {
@Cleanup ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(new byte[] {'Y','e','s'});
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
public void testCleanUp() {
try {
ByteArrayOutputStream e = new ByteArrayOutputStream();
try {
e.write(new byte[]{(byte)89, (byte)101, (byte)115});
System.out.println(e.toString());
} finally {
if(Collections.singletonList(e).get(0) != null) {
e.close();
}
}
} catch (IOException var6) {
var6.printStackTrace();
}
}
@Synchronized
可以帮我们在方法上添加同步代码块
public class TestSync {
private DateFormat format = new SimpleDateFormat("MM-dd-YYYY");
@Synchronized
public String synchronizedFormat(Date date) {
return format.format(date);
}
}
等同于
public class TestSync {
private final Object $lock = new Object[0];
private DateFormat format = new SimpleDateFormat("MM-dd-YYYY");
public TestSync() {
}
public String synchronizedFormat(Date date) {
Object var2 = this.$lock;
synchronized(this.$lock) {
return this.format.format(date);
}
}
}
总结
可以看到代码量差不多缩减了三倍,但是他也有一些缺点,比如:
大大降低了源代码文件的可读性和完整性,降低了阅读源代码的舒适度。