I want to refactor my code from non-typed objects to typed objects.
For example, here is my current code:
public class TestClass {
ArrayList list = new ArrayList();
public ArrayList testMethodA() {
list.add("string1");
list.add("string2");
list.add("string3");
list.add("string4");
list.add("string5");
return list;
}
}
I want to turn this into a typed List, so it becomes
public class TestClass {
List<String> list = new ArrayList<>();
public ArrayList<String> testMethodA() {
list.add("string1");
list.add("string2");
list.add("string3");
list.add("string4");
list.add("string5");
return list;
}
}
Is there a way to change the return type from ArrayList
to ArrayList<Object>
based on the type of obj1
, obj2
, ...]? For example, if what's being added to the list are of type String
, then is it possible to change the return value from ArrayList
to ArrayList<String>
dynamically?
I'm using Eclipse, so if there is a plugin (or script or something) that can help me do this it would be much appreciated.
If there is no 'automatic' way of doing this it would mean 5-6 days of manual fixing, which I would rather avoid.
In the java editor: Right-click > Refactor > Infer Generic Type Arguments...
Have a look at the documentation of Eclipse's "Infer Generic Type Arguments" refactoring: http://help.eclipse.org/kepler/index.jsp?topic=/org.eclipse.jdt.doc.user/reference/ref-menu-refactor.htm
Also see this previous stackoverflow question: How to use "Infer Generic Type Arguments..." in Eclipse