@Data测试类:
package lombok;
@Data
public class TestData {
private String name1;
private Integer id;
}
@Data注解作用:
1)生成无参构造方法;
2)属性的set/get方法;
3)equals(), hashCode(), toString(), canEqual()方法。
编译之后的class文件:
public class TestData {
private String name1;
private Integer id;
public TestData() {
}
public String getName1() {
return this.name1;
}
public Integer getId() {
return this.id;
}
public void setName1(String name1) {
this.name1 = name1;
}
public void setId(Integer id) {
this.id = id;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof TestData)) {
return false;
} else {
TestData other = (TestData)o;
if (!other.canEqual(this)) {
return false;
} else {
Object this$name1 = this.getName1();
Object other$name1 = other.getName1();
if (this$name1 == null) {
if (other$name1 != null) {
return false;
}
} else if (!this$name1.equals(other$name1)) {
return false;
}
Object this$id = this.getId();
Object other$id = other.getId();
if (this$id == null) {
if (other$id != null) {
return false;
}
} else if (!this$id.equals(other$id)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof TestData;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $name1 = this.getName1();
int result = result * 59 + ($name1 == null ? 43 : $name1.hashCode());
Object $id = this.getId();
result = result * 59 + ($id == null ? 43 : $id.hashCode());
return result;
}
public String toString() {
return "TestData(name1=" + this.getName1() + ", id=" + this.getId() + ")";
}
}
@Value注解作用:
1)有参构造方法;
2)只添加@Value注解,没有其他限制,那么类属性会被编译成final的,因此只有get方法,而没有set方法。
@Value测试类:
@Value
public class TestValue {
String name2;
Integer id2;
}
编译之后的class文件:
public final class TestValue {
private final String name2;
private final Integer id2;
public TestValue(String name2, Integer id2) {
this.name2 = name2;
this.id2 = id2;
}
public String getName2() {
return this.name2;
}
public Integer getId2() {
return this.id2;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof TestValue)) {
return false;
} else {
TestValue other = (TestValue)o;
Object this$name2 = this.getName2();
Object other$name2 = other.getName2();
if (this$name2 == null) {
if (other$name2 != null) {
return false;
}
} else if (!this$name2.equals(other$name2)) {
return false;
}
Object this$id2 = this.getId2();
Object other$id2 = other.getId2();
if (this$id2 == null) {
if (other$id2 != null) {
return false;
}
} else if (!this$id2.equals(other$id2)) {
return false;
}
return true;
}
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $name2 = this.getName2();
int result = result * 59 + ($name2 == null ? 43 : $name2.hashCode());
Object $id2 = this.getId2();
result = result * 59 + ($id2 == null ? 43 : $id2.hashCode());
return result;
}
public String toString() {
return "TestValue(name2=" + this.getName2() + ", id2=" + this.getId2() + ")";
}
}