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方法
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,374评论 19 139
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 7,593评论 0 3
  • 文/蓝天 爱你 不仅仅爱你的忧点和美丽 每个人都有缺点 虽然热恋时 你的臭脾气没露出来 但我已做好了准备 爱你 就...
    翟德生阅读 3,038评论 5 9
  • 当年万里觅封侯。匹马戍梁州。关河梦断何处,尘暗旧貂裘。胡未灭,鬓先秋。泪空流。此生谁料,心在天山,身老沧洲。 一个...
    尘里微光阅读 3,089评论 0 0