2

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 “<code>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.)

4

1 回答 1

1

不完全是,但您可以定义一个模板,当您选择它时,您会同时获得导入和代码。转到Window -> Preferences -> Java -> Editor -> Templates,添加一个新的名称newA和模式${staticImport:importStatic('com.google.common.collect.Lists')}Lists.newArrayList(${cursor})

在您的方法主体中,键入newA并点击CTRL+Space,您应该会看到以下内容: 在此处输入图像描述

于 2014-04-08T13:50:43.187 回答