lombok @Data 注解到底干了个啥

在java bean定义的时候,需要给每个字段提供set和get属性。
lombok这个插件所做的事情就是在编译期间替我们干了这件事。
下面提供两个文件的比对:
原始文件:
@Data
@NoArgsConstructor
public class AdvertiserQueryVO {
private String advertiserName;
private String jdId;
private String status;
private String chargeType;
}
编译后的class,反编译出来的文件:
public class AdvertiserQueryVO {
private String advertiserName;
private String jdId;
private String status;
private String chargeType;

public boolean equals(Object o) {
    if (o == this) return true;
    if (!(o instanceof AdvertiserQueryVO)) return false;
    AdvertiserQueryVO other = (AdvertiserQueryVO) o;
    if (!other.canEqual(this)) return false;
    Object this$advertiserName = getAdvertiserName();
    Object other$advertiserName = other.getAdvertiserName();
    if (this$advertiserName == null ? other$advertiserName != null : !this$advertiserName.equals(other$advertiserName))
        return false;
    Object this$jdId = getJdId();
    Object other$jdId = other.getJdId();
    if (this$jdId == null ? other$jdId != null : !this$jdId.equals(other$jdId)) return false;
    Object this$status = getStatus();
    Object other$status = other.getStatus();
    if (this$status == null ? other$status != null : !this$status.equals(other$status)) return false;
    Object this$chargeType = getChargeType();
    Object other$chargeType = other.getChargeType();
    return this$chargeType == null ? other$chargeType == null : this$chargeType.equals(other$chargeType);
}

public boolean canEqual(Object other) {
    return other instanceof AdvertiserQueryVO;
}

public int hashCode() {
    int PRIME = 59;
    int result = 1;
    Object $advertiserName = getAdvertiserName();
    result = result * 59 + ($advertiserName == null ? 0 : $advertiserName.hashCode());
    Object $jdId = getJdId();
    result = result * 59 + ($jdId == null ? 0 : $jdId.hashCode());
    Object $status = getStatus();
    result = result * 59 + ($status == null ? 0 : $status.hashCode());
    Object $chargeType = getChargeType();
    result = result * 59 + ($chargeType == null ? 0 : $chargeType.hashCode());
    return result;
}

public String toString() {
    return "AdvertiserQueryVO(advertiserName=" + getAdvertiserName() + ", jdId=" + getJdId() + ", status=" + getStatus() + ", chargeType=" + getChargeType() + ")";
}


public String getAdvertiserName() {
    return advertiserName;
}

public void setAdvertiserName(String advertiserName) {
    this.advertiserName = advertiserName;
}

public String getJdId() {
    return jdId;
}

public void setJdId(String jdId) {
    this.jdId = jdId;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getChargeType() {
    return chargeType;
}

public void setChargeType(String chargeType) {
    this.chargeType = chargeType;
}

public AdvertiserQueryVO() {
}

}
通过对两个文件的比对,我们发现,加了@Data注解的类,编译后会自动给我们加上下列方法:

  • 所有属性的get和set方法
  • toString 方法
  • hashCode方法
  • equals方法

你可能感兴趣的:(lombok @Data 注解到底干了个啥)