Inferring type parameters for generic type references
Eclipse can attempt to infer type parameters for all generic type references in a
class, package, or project. This is especially useful when migrating from Java 1.4 code
to Java 5.0 code, allowing you to automatically make use of the generic classes in
Java's collections API.
- Open a class in the Java editor, or in a Java view, select a class, package, or project.
- Do one of the following:
- From the menu bar, select Refactor > Infer Generic Type Arguments or
- From the pop-up menu, select Refactor > Infer Generic Type Arguments
You will be given a dialog with two configurable options:
- Assume clone() returns an instance of the receiver type
Well-behaved classes generally respect this rule, but if you know that your
code violates it, uncheck the box.
- Leave unconstrained type arguments raw (rather than inferring <?>).
If there are no constraints on the elements of e.g.
ArrayList a
, unchecking this
box will cause Eclipse to still provide a wildcard parameter, replacing the reference with
ArrayList<?> a
.
Press OK or Preview to continue with the operation.
Note: It may in some cases be impossible to assign
consistent type parameters in a selection, or require deeper analysis
than Eclipse can perform.
Refactoring actions
Refactoring dialogs
Java preferences