Java Eclipse 可以修改 Organize Import (ctrl+shift+o) 命令对静态导入的处理吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9831047/
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-16 08:18:47  来源:igfitidea点击:

Can Eclipse the Organize Import (ctrl+shift+o) command's handling of static imports be modified?

javaeclipsekeyboard-shortcuts

提问by ulmangt

I use the Eclipse keyboard shortcut Ctrl+ Shift+ Oall the time (Organize Imports).

我一直使用 Eclipse 键盘快捷键Ctrl+ Shift+ O(组织导入)。

However, it has one particularly annoying behavior:

但是,它有一个特别烦人的行为:

If I have any static imports like:

如果我有任何静态导入,例如:

import static java.lang.Math.*;

Organize Imports will "helpfully" replace it with static imports of only the fields and methods I am actually using. For example:

组织导入将“有帮助地”将其替换为仅我实际使用的字段和方法的静态导入。例如:

import static java.lang.Math.PI;
import static java.lang.Math.cos;

Is there any way to remove just this behavior from Organize Imports?

有什么方法可以从 Organize Imports 中删除这种行为?

采纳答案by Jon Skeet

Sure: Window -> Preferences -> Java -> Code Style -> Organize Imports

当然:Window -> Preferences -> Java -> Code Style -> Organize Imports

Adjust the value for "Number of static imports needed for .*"to 1, and it will always use .*. Of course, that's not quitethe same as saying, "Just leave static imports alone" but hopefully it's what you want.

将 的值调整为"Number of static imports needed for .*"1,它将始终使用 .*。当然,这不是相同的话说,“见好就收静态导入独”,但希望这是你想要的。

Basically, this number says "Use all of the methods or fields of this import if I'm importing this many items" So if the number is 2, it will add the asterisk only if you use two or more imports. That will fix the problem you said in the response.

基本上,这个数字表示“如果我要导入这么多项目,请使用此导入的所有方法或字段”因此,如果数字为 2,则仅当您使用两个或更多导入时才会添加星号。这将解决您在回复中所说的问题。