Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autocomplete for generic types in Eclipse

"Refer to objects by their interfaces" is a good practise, as mentioned in Effective Java. So for example i prefer

List<String> al = new ArrayList<String>();

over

ArrayList<String> al = new ArrayList<String>();

in my code. One annoying thing is that if i type ArrayList<String> al = new and then hit Ctrl+Space in Eclipse i get ArrayList<String>() as propostal. But if i type List al = new and then hit Ctrl+Space i will get only propostal to define anonymous inner class, but not propostals such as new ArrayList<String>(), what is 99% the case, or for example new Vector<String>().

Question: Is there any way to get the subclasses as propostals for generic types?

like image 200
AvrDragon Avatar asked Jul 03 '12 14:07

AvrDragon


3 Answers

I would suggest simply writing the expression first and then hitting Ctrl+2, L. Then you can name the variable, followed by Enter, Down, Enter. Done.

like image 187
Ben Schulz Avatar answered Oct 22 '22 00:10

Ben Schulz


Depending on your personal style of writing such code, this is an alternative inspired by the answer of @Ben Schulz. If you typically write the assignment first like

al = new ArrayList<String>();

then you can use Ctrl+1 to start the "Create local variable" quick fix. Hitting Tab will now directly open the type selection where you select the List type.

like image 20
Bananeweizen Avatar answered Oct 22 '22 01:10

Bananeweizen


JDK 1.7 doesn't need to specify the generic type at the right of the equal. Preference -> Java -> Compiler to 1.6

like image 2
user2724466 Avatar answered Oct 22 '22 01:10

user2724466