Errors and Warnings
以下是線上說明的完整清單,針對我們團隊中一些設定項目定義,我會說明它所避開的陷阱實例。
Code style
Potential programming problems
多數時候,此區的 warning 檢核,可以彌補我們不足的單元測試案例。
Unlikely argument type for equals() : Info -> Warning
When enabled, the compiler will issue an error or warning when 'java.lang.Object#equals(Object)' is used with an argument type, that seems to be not related to the receiver's type, or correspondingly when the arguments of 'java.util.Objects#equals(Object, Object)' have types that seem to be not related to each other.
舉例:在原本程式中,this.format 是 String,所以沒有問題。
if ("twy/MM/dd".equals(this.format)) {
return YYYMMDDUtils.insertSeparatorForce(valueStr, '/');
} else {
return valueStr;
}
但後來若為API的擴充性,把 format 改為 Functional Interface,這時「Unlikely argument type for equals」就可以提醒我們此處有問題(如果沒有相對單元測試案例的話)。
private DateFormat format = () -> "twy/MM/dd";
// ....
boolean fooxxx (...) {
if ("twy/MM/dd".equals(this.format.getFormat())) {
return YYYMMDDUtils.insertSeparatorForce(valueStr, '/');
} else {
return valueStr;
}
}
PS: 此項檢查也對JDK8 的 Objects.equals(formatText, this.format); 有效;但對其它 3rd party 的函式庫如 ObjectUtils.equals(formatText, this.format); 無效。