Eclipse - 添加 lombok 的数据和构造函数时显示 @SuppressWarnings(value={"all"})
Eclipse - Show @SuppressWarnings(value={"all"}) when lombok's Data and Constructor added
我有一个 class 和 lombok 的 @Data
和 @AllArgsConstructor(access = AccessLevel.PUBLIC)
:
@Data
@AllArgsConstructor(access = AccessLevel.PUBLIC)
public class ResponseVO implements Serializable {
private static final long serialVersionUID = 3461582576096529916L;
@JacksonXmlProperty(localName = "amount", isAttribute = true)
String amount;
}
当我使用构造函数时
new ResponseVO("22222");
将鼠标悬停在构造函数方法上时,我在工具提示中收到警告:
ResponseVO.ResponseVO(String amount)
@SuppressWarnings(value={"all"})
为什么添加这个警告?没有 @Data
它会消失
Class反编译没有任何警告:
public class ResponseVO implements Serializable {
private static final long serialVersionUID = 3461582576096529916L;
@JacksonXmlProperty(localName = "amount", isAttribute = true)
String amount;
public String getAmount() {
return this.amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof ResponseVO)) {
return false;
} else {
ResponseVO other = (ResponseVO) o;
if (!other.canEqual(this)) {
return false;
} else {
String this$amount = this.getAmount();
String other$amount = other.getAmount();
if (this$amount == null) {
if (other$amount != null) {
return false;
}
} else if (!this$amount.equals(other$amount)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof ResponseVO;
}
public int hashCode() {
boolean PRIME = true;
byte result = 1;
String $amount = this.getAmount();
int result1 = result * 59 + ($amount == null ? 43 : $amount.hashCode());
return result1;
}
public String toString() {
return "ResponseVO(amount=" + this.getAmount() + ")";
}
public ResponseVO(String amount) {
this.amount = amount;
}
}
这不是警告,它是出现在所有 类、方法等上的常规 Eclipse 工具提示。这些工具提示显示相应元素的 JavaDoc(在此为空case) 并列出元素上的所有注释。
这就是您看到 @SuppressWarnings
的原因:它由 Lombok 生成,以避免编译器在 Lombok 生成的代码上发出警告。
问题仍然是为什么 Lombok 会生成那些抑制注释。
通常,Lombok 的代码不会产生任何警告。但是,新的 Java 语言或编译器版本可能会导致新类型的警告或新的弃用。 运行 针对较新 Java 版本的未改编 Lombok 版本因此可能会产生警告。由于用户将无法修复这些警告,因此这些警告会被抑制。
此外,添加 @SuppressWarnings("all")
还会抑制非标准警告,例如来自代码 linters 或集成在像 IntelliJ IDEA 这样的 IDE 中的代码分析。
我有一个 class 和 lombok 的 @Data
和 @AllArgsConstructor(access = AccessLevel.PUBLIC)
:
@Data
@AllArgsConstructor(access = AccessLevel.PUBLIC)
public class ResponseVO implements Serializable {
private static final long serialVersionUID = 3461582576096529916L;
@JacksonXmlProperty(localName = "amount", isAttribute = true)
String amount;
}
当我使用构造函数时
new ResponseVO("22222");
将鼠标悬停在构造函数方法上时,我在工具提示中收到警告:
ResponseVO.ResponseVO(String amount)
@SuppressWarnings(value={"all"})
为什么添加这个警告?没有 @Data
它会消失
Class反编译没有任何警告:
public class ResponseVO implements Serializable {
private static final long serialVersionUID = 3461582576096529916L;
@JacksonXmlProperty(localName = "amount", isAttribute = true)
String amount;
public String getAmount() {
return this.amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof ResponseVO)) {
return false;
} else {
ResponseVO other = (ResponseVO) o;
if (!other.canEqual(this)) {
return false;
} else {
String this$amount = this.getAmount();
String other$amount = other.getAmount();
if (this$amount == null) {
if (other$amount != null) {
return false;
}
} else if (!this$amount.equals(other$amount)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof ResponseVO;
}
public int hashCode() {
boolean PRIME = true;
byte result = 1;
String $amount = this.getAmount();
int result1 = result * 59 + ($amount == null ? 43 : $amount.hashCode());
return result1;
}
public String toString() {
return "ResponseVO(amount=" + this.getAmount() + ")";
}
public ResponseVO(String amount) {
this.amount = amount;
}
}
这不是警告,它是出现在所有 类、方法等上的常规 Eclipse 工具提示。这些工具提示显示相应元素的 JavaDoc(在此为空case) 并列出元素上的所有注释。
这就是您看到 @SuppressWarnings
的原因:它由 Lombok 生成,以避免编译器在 Lombok 生成的代码上发出警告。
问题仍然是为什么 Lombok 会生成那些抑制注释。
通常,Lombok 的代码不会产生任何警告。但是,新的 Java 语言或编译器版本可能会导致新类型的警告或新的弃用。 运行 针对较新 Java 版本的未改编 Lombok 版本因此可能会产生警告。由于用户将无法修复这些警告,因此这些警告会被抑制。
此外,添加 @SuppressWarnings("all")
还会抑制非标准警告,例如来自代码 linters 或集成在像 IntelliJ IDEA 这样的 IDE 中的代码分析。