Java 5 (J2SE 5.0) 引入泛型,是語言史上最大語法變更。Eclipse 3.1 首度提供完整工具支援:編譯器配置、快速修復、重構、內容助手、類型感知搜尋。


| 選項 | 觸發條件 |
|---|---|
| Unchecked generic type operation | 使用原始類型 (如 List 而非 List<String>) |
| Generic type parameter declared with a final type bound | <? extends String> (String 為 final 無意義) |
| Inexact type match for vararg arguments | varargs 類型推斷模糊 |
| Boxing and unboxing conversions | 自動裝箱/拆箱可能影響效能 |
| Missing @Override annotation | 覆寫方法缺少註解 |
| Missing @Deprecated annotation | 使用已棄用 API 未標註 |
| Annotation is used as super interface | 介面繼承 @Deprecated 等註解 |
| Not all enum constants covered on switch | switch 未涵蓋所有 enum 值 |
| Unhandled warning tokens in @SuppressWarnings | 拼錯抑制警告名稱 |
| Enable @SuppressWarnings annotations | 啟用程式化抑制警告 |
public class HR {
public HR() {
List empList = new ArrayList(5); // 原始類型
empList.add(new Employee("Homer", 200.0, 1995));
empList.add(new Employee("Lenny", 300.0, 2000));
empList.add(new Employee("Waylon", 700.0, 1965));
empList.add(new Manager("Monty", 2000.0, 1933, (Employee) empList.get(2))); // 需強制轉型
printEmployees(empList);
for (int i = 0; i < empList.size(); i++)
((Employee) empList.get(i)).applyRaise(5); // 強制轉型
printEmployees(empList);
// ... Collections.sort 等操作
}
public void printEmployees(List emps) { // 原始類型參數
for (int i = 0; i < emps.size(); i++)
System.out.println(emps.get(i));
}
}




List empList = new ArrayList(5);
empList.add(new Employee("Homer", 200.0, 1995));
empList.add(new Employee("Lenny", 300.0, 2000));
empList.add(new Employee("Waylon", 700.0, 1965));
empList.add(new Manager("Monty", 2000.0, 1933, empList.get(2))); // 無需轉型!
public void printEmployees(List emps) {
for (int i = 0; i < emps.size(); i++)
System.out.println(emps.get(i));
}
兩大改變:
<Employee>empList.get(2) 已知為 Employee)
// 遷移前
for (int i = 0; i < emps.size(); i++)
System.out.println(emps.get(i));
// 快速修復建議 → for-each
for (Employee emp : emps)
System.out.println(emp);

get() 呼叫,更簡潔安全@Override 等註解的快速修復與內容助手new ArrayList<|>() 游標位置自動建議List<Employee> 與 List<Date>List empList = ...;
List hireDates = ...;
List departments = ...;
List extends Employee> allMgrs = ...;

Search 視窗選單 → Filter Incompatible → 僅保留 Employee 相容項目


問題:方法接受 List<Employee>,但未來可能需處理 List<Person> (Employee 父類)。
解決:使用通配符上界
public List empsOverRetirementAge(List extends Person> people) {
List retirees = new ArrayList();
for (Person e : people)
if (e.getAge() > 65) retirees.add(e);
return retirees;
}
? extends Person 接受 Person 及其所有子類List 安全 (只讀取、不寫入)Eclipse 3.1 提供業界領先的泛型工具鏈:配置、警告、快速修復、重構、搜尋、內容助手全方位支援。善用工具遷移舊代碼、採用 for-each、靈活運用通配符,能同時獲得型別安全、程式碼簡潔、重構可靠三重收益。