MyEclipse (基於 Eclipse JDT 編譯器) 的 Warning 是靜態程式碼分析結果,非編譯錯誤。編譯器仍會生成 .class 檔案,程式可正常執行。
| Warning | 原因 | 建議處理 |
|---|---|---|
| Unused import | 匯入未使用的套件/類別 | Ctrl+Shift+O 自動清理 |
| Unused local variable | 宣告變數未讀取 | 移除或標記 @SuppressWarnings("unused") |
| Raw type / Unchecked conversion | 泛型缺失、型別不安全 | 補齊泛型參數 List<String> |
| Serializable without serialVersionUID | 實作 Serializable 未宣告 UID | 加入 private static final long serialVersionUID = 1L; |
| Deprecated API | 使用已棄用方法 | 遷移至替代 API,或確認相容性後抑制 |
| Null pointer dereference | 靜態分析偵測潛在 NPE | 加入非空判斷或 Objects.requireNonNull |
專案右鍵 → Properties → Java Compiler → Errors/Warnings
├─ 勾選 "Enable project specific settings"
├─ "Unnecessary code" → "Unused import" : Warning
├─ "Generic types" → "Raw type references" : Warning
├─ "Deprecated API" : Warning
└─ "Null analysis" → "Potential null pointer" : Warning (或 Error 嚴格模式)
在 Maven/Gradle 建置階段加入 spotbugs、pmd、checkstyle,將關鍵 Warning 升級為 Build Failure,強制團隊在提交前修復。
Warning = 「程式跑得動,但可能藏著 Bug 或技術債」。
零 Warning 專案是成熟工程團隊的基本門檻,而非潔癖。