Lombok 的使用
Lombok作为一个奇淫巧技,感觉知道的人还是挺少的
Lombok的一个作用就是不需要写getter和setter方法了,日后修改字段名,也不用修改getter和setter方法,lombok会屏蔽掉这一点
@Data 的使用
@Data 是在class类定义上添加的注解
@Data
public class Persion{
private String name;
private Date birthDay;
private String habbit;
}
代码在IDEA里面表现的代码结构为
class 文件反编译之后的结果
package main.java.com.ditmark.lombok;
import java.util.Date;
public class Persion {
private String name;
private Date birthDay;
private String hobbit;
public Persion() {
}
public String getName() {
return this.name;
}
public Date getBirthDay() {
return this.birthDay;
}
public String getHobbit() {
return this.hobbit;
}
public void setName(String name) {
this.name = name;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
public void setHobbit(String hobbit) {
this.hobbit = hobbit;
}
public boolean equals(Object o) {
if(o == this) {
return true;
} else if(!(o instanceof Persion)) {
return false;
} else {
Persion other = (Persion)o;
if(!other.canEqual(this)) {
return false;
} else {
label47: {
Object this$name = this.getName();
Object other$name = other.getName();
if(this$name == null) {
if(other$name == null) {
break label47;
}
} else if(this$name.equals(other$name)) {
break label47;
}
return false;
}
Object this$birthDay = this.getBirthDay();
Object other$birthDay = other.getBirthDay();
if(this$birthDay == null) {
if(other$birthDay != null) {
return false;
}
} else if(!this$birthDay.equals(other$birthDay)) {
return false;
}
Object this$hobbit = this.getHobbit();
Object other$hobbit = other.getHobbit();
if(this$hobbit == null) {
if(other$hobbit != null) {
return false;
}
} else if(!this$hobbit.equals(other$hobbit)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof Persion;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $name = this.getName();
int result = result * 59 + ($name == null?43:$name.hashCode());
Object $birthDay = this.getBirthDay();
result = result * 59 + ($birthDay == null?43:$birthDay.hashCode());
Object $hobbit = this.getHobbit();
result = result * 59 + ($hobbit == null?43:$hobbit.hashCode());
return result;
}
public String toString() {
return "Persion(name=" + this.getName() + ", birthDay=" + this.getBirthDay() + ", hobbit=" + this.getHobbit() + ")";
}
}
Lombok还可以在循环的时候使用,强制循环中使用final 类型的对象,避免对象在循环过程中进行误操作.
public static void main(String args){
val list= new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
for(val str: list){
System.out.println(str);
}
val map = new HashMap<String,String>();
for(val key : map.keySet()){
System.out.println(key);
}
for(val entry : map.entrySet()){
System.out.println(entry.getKey());
}
}
class 反编译之后,并没有发现final 可怕…也许是版本的差异?使用的版本为lombok 1.16.16
public static void main(String args) {
ArrayList<String> list = new ArrayList();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
Iterator var2 = list.iterator();
while(var2.hasNext()) {
String str = (String)var2.next();
System.out.println(str);
}
HashMap<String, String> map = new HashMap();
Iterator var6 = map.keySet().iterator();
while(var6.hasNext()) {
String key = (String)var6.next();
System.out.println(key);
}
var6 = map.entrySet().iterator();
while(var6.hasNext()) {
Entry<String, String> entry = (Entry)var6.next();
System.out.println((String)entry.getKey());
}
}
Lombok在流资源的开启和关闭也可以使用,避免写一堆close catch代码