如果最終子類從建構式呼叫超類可覆寫方法,Spotbugs 會報告錯誤(有關詳細資訊,請參閱下面的注釋)。
這是預期的還是有問題的?
例如:
public class SuperClass {
private final int id;
public SuperClass(int id) {
this.id = id;
}
public int getId() {
return id;
}
}
public final class SubClass extends SuperClass {
private final String name;
public SubClass(int id, String code) {
super(id);
this.name = getId() code; // Spotbugs repot this line as a bug
}
public final String getName() {
return name;
}
}
報告:
[ERROR] Low: Overridable method getId is called from constructor new SubClass(int, String).
[SubClass] At SubClass.java:[line ?] MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR
筆記:
由于添加了Spotbugs 4.5.0.0 錯誤檢測器FindOverridableMethodCall(請參閱詳細資訊):
MC:從建構式呼叫可覆寫的方法
(MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR)在建構式中呼叫可覆寫的方法可能會導致使用未初始化的資料。它也可能泄漏部分構造物件的參考。只能從建構式呼叫靜態、最終或私有方法。
uj5u.com熱心網友回復:
這似乎是一個錯誤,最近有一個與此案例相關的未解決問題:
最終課程中 MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR 的誤報
還有一個pull request來修復這個bug。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/369637.html
下一篇:在C 中抽象容器
