Java Eclipse 优化导入以包含静态导入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/288861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:36:28  来源:igfitidea点击:

Eclipse Optimize Imports to Include Static Imports

javaeclipsekeyboard-shortcuts

提问by Paul Croarkin

Is there anyway to get Eclipse to automatically look for static imports? For example, now that I've finally upgraded to Junit 4, I'd like to be able to write:

有没有办法让 Eclipse 自动查找静态导入?例如,现在我终于升级到 Junit 4,我希望能够编写:

assertEquals(expectedValue, actualValue);

hit Ctrl+ Shift+ Oand have Eclipse add:

命中Ctrl+ Shift+ O,并有添加Eclipse:

import static org.junit.Assert.assertEquals;

Maybe I'm asking too much.

可能是我要求太高了。

采纳答案by Joey Gibson

I'm using Eclipse Europa, which also has the Favorite preference section:

我正在使用 Eclipse Europa,它也有“收藏夹”首选项部分:

Window > Preferences > Java > Editor > Content Assist > Favorites

窗口 > 首选项 > Java > 编辑器 > 内容辅助 > 收藏夹

In mine, I have the following entries (when adding, use "New Type" and omit the .*):

在我的中,我有以下条目(添加时,使用“新类型”并省略.*):

org.hamcrest.Matchers.*
org.hamcrest.CoreMatchers.*
org.junit.*
org.junit.Assert.*
org.junit.Assume.*
org.junit.matchers.JUnitMatchers.*

All but the third of those are static imports. By having those as favorites, if I type "assertT" and hit Ctrl+Space, Eclipse offers up assertThatas a suggestion, and if I pick it, it will add the proper static import to the file.

除了第三个之外,所有这些都是静态导入。通过将它们作为收藏夹,如果我输入“ assertT”并点击Ctrl+Space,Eclipse 会提供assertThat一个建议,如果我选择它,它会将正确的静态导入添加到文件中。

回答by Paul Croarkin

Not exactly what I wanted, but I found a workaround. In Eclipse 3.4 (Ganymede), go to

不完全是我想要的,但我找到了一个解决方法。在 Eclipse 3.4 (Ganymede) 中,转到

Window->Preferences->Java->Editor->Content Assist

窗口->首选项->Java->编辑器->内容辅助

and check the checkbox for Use static imports (only 1.5 or higher).

并选中Use static import (only 1.5 or later)复选框。

This will not bring in the import on an Optimize Imports, but if you do a Quick Fix (CTRL+ 1) on the line it will give you the option to add the static import which is good enough.

这不会引入 Optimize Imports 的导入,但是如果您在线上执行 Quick Fix ( CTRL+ 1),它将为您提供添加足够好的静态导入的选项。

回答by Dave L.

If you highlight the method Assert.assertEquals(val1, val2)and hit Ctrl+ Shift+ M(Add Import), it will add it as a static import, at least in Eclipse 3.4.

如果突出显示该方法Assert.assertEquals(val1, val2)并点击Ctrl+ Shift+ M(添加导入),它会将其添加为静态导入,至少在 Eclipse 3.4 中是这样。

回答by Bill Michell

Eclipse 3.4 has a Favourites section under Window->Preferences->Java->Editor->Content Assist

Eclipse 3.4 在 Window->Preferences->Java->Editor->Content Assist 下有一个收藏夹部分

If you use org.junit.Asserta lot, you might find some value to adding it there.

如果您使用org.junit.Assert很多,您可能会发现将其添加到那里的一些价值。

回答by Sumit Singh

From Content assist for static imports

来自静态导入的内容辅助

To get content assist proposals for static members configure your list of favorite static members on the Opens the Favorites preference page Java > Editor > Content Assist > Favoritespreference page.
For example, if you have added java.util.Arrays.*or org.junit.Assert.*to this list, then all static methods of this type matching the completion prefix will be added to the proposals list.

要获取静态成员的内容辅助建议,请在打开收藏夹首选项页面Java > Editor > Content Assist > Favorites首选项页面上配置您喜爱的静态成员列表。
例如,如果您已将java.util.Arrays.*或添加org.junit.Assert.*到此列表,则与完成前缀匹配的此类型的所有静态方法都将添加到建议列表中。

Open Window ? Preferences ? Java ? Editor ? Content Assist ? Favorites

打开 Window ? Preferences ? Java ? Editor ? Content Assist ? Favorites

enter image description here

在此处输入图片说明

回答by Neeraj

For SpringFramework Tests, I would recommend to add the below as well

对于 SpringFramework 测试,我建议也添加以下内容

org.springframework.test.web.servlet.request.MockMvcRequestBuilders
org.springframework.test.web.servlet.request.MockMvcResponseBuilders
org.springframework.test.web.servlet.result.MockMvcResultHandlers
org.springframework.test.web.servlet.result.MockMvcResultMatchers
org.springframework.test.web.servlet.setup.MockMvcBuilders
org.mockito.Mockito

When you add above as new Type it automatically add .* to the package.

当您将上面添加为新类型时,它会自动将 .* 添加到包中。

回答by Ankit Soni

In Eclipse 4.9, you can static import existing invocations using a quick fix.

在 Eclipse 4.9 中,您可以使用快速修复静态导入现有调用。

A new quick fix has been implemented that allows the user to convert static field accesses and static methods to use a static import. It's also possible to replace all occurrences at the same time.

已实施新的快速修复,允许用户将静态字段访问和静态方法转换为使用静态导入。也可以同时替换所有事件。

More details here

更多细节在这里

回答by teknopaul

Select the constant, type

选择常量,输入

Ctrl + 1  (quick fix)

Select "Convert to static import." from the drop down.

选择“转换为静态导入”。从下拉。

"Quick fix" has options even though it is not an error.

“快速修复”有选项,即使它不是错误。

回答by Michael Hegner

Shortcut for static import: CTRL + SHIFT + M

静态导入的快捷键:CTRL + SHIFT + M