views:

66

answers:

2

Is there a way/tool to auto convert Java source code from using raw types to using generic types?

I have some legacy code with 677 references to raw types:

ArrayList   47
Vector      420
Hashtable   61
Enumeration 64
Class       7
Iterator    78
TOTAL       677

Now I could manually look through the code to infer the generic types and replace, but that is going to take a long time.

+2  A: 

Eclipse does have an 'infer generic types' refactoring tool (I don't know if it will process multiple classes in a single hit, but probably).

Replaces raw type occurrences of generic types by parameterized types after identifying all places where this replacement is possible. Available: Projects, packages, and types 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, uncheck this box will cause Eclipse to still provide a wildcard parameter, replacing the reference with ArrayList.

Kevin Day
Found you can right-click a project and choose this under the 'Refactor' menu.
Sam
+1  A: 

IntelliJ also has Refactor | Generify. It worked pretty decently five years ago, so I assume it's no worse now!

Kevin Bourrillion