Reputation: 458
I’d like to configure Eclipse with some “favorite” static-method collections, but without using static imports.
For example, if I add com.google.common.collect.Lists
the list in Window » Preferences » Java » Editor » Content Assist » Favorites, I can type “newA
”, press Ctrl-Space, and pick the Lists.newArrayList
method from that list. But:
1) If “static imports” is enabled in Preferences » Java » Editor » Content Assist, the result is that Eclipse adds a static import for com.google.common.collect.Lists.newArrayList
and auto-completes my code to newArrayList(...)
.
2) If “static imports” is disabled, then:
2.a) If the class with the static method is not imported, it adds an import for the class, and auto-completes to Lists.newArrayList(...)
, which is exactly what I want. However,
2.b) If the class with the static method is already imported, then it just auto-completes to newArrayList(...)
, which of course doesn’t compile.
My question is: Is there a way to configure Eclipse so that (2.b) behaves like (2.a) all the time? (I.e. so that it auto-completes favorite static methods with their class name prefix even when the class is already imported.)
Upvotes: 2
Views: 427
Reputation: 15528
Not exactly to the point, but you can define a template and when you select it, you get both the import and the code. Go to Window -> Preferences -> Java -> Editor -> Templates
, add a new one with name newA
and the pattern ${staticImport:importStatic('com.google.common.collect.Lists')}Lists.newArrayList(${cursor})
In your method body, type newA
and hit CTRL+Space
and you should see the following:
Upvotes: 1