Java IntelliJ:永远不要使用通配符导入

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

IntelliJ: Never use wildcard imports

javaintellij-ideasettings

提问by digiarnie

Is there a way to tell IntelliJ never to use wildcard imports? Under 'Settings > Code Style > Imports', I can see that you can specify the 'class count' prior to IntelliJ using wildcard imports. However, if I never want to use wildcard imports can I turn this functionality off?

有没有办法告诉 IntelliJ 永远不要使用通配符导入?在“设置 > 代码样式 > 导入”下,我可以看到您可以使用通配符导入在 IntelliJ 之前指定“类计数”。但是,如果我从不想使用通配符导入,是否可以关闭此功能?

I have tried putting -1 or leaving the field blank but that just tells IntelliJ to always use wildcard imports. Obviously a not-so-nice solution would be to put a ridiculously high number so that you never encounter wildcard imports but I was hoping there was a nicer way to just turn it off.

我曾尝试将 -1 或将该字段留空,但这只是告诉 IntelliJ 始终使用通配符导入。显然,一个不太好的解决方案是设置一个高得离谱的数字,这样您就不会遇到通配符导入,但我希望有一种更好的方法来关闭它。

采纳答案by duffymo

It's obvious why you'd want to disable this: To force IntelliJ to include each and every import individually. It makes it easier for people to figure out exactly where classes you're using come from.

很明显为什么要禁用此功能:强制 IntelliJ 单独包含每个导入。它使人们更容易确定您使用的类的确切来源。

Click on the Settings "wrench" icon on the toolbar, open "Imports" under "Code Style", and check the "Use single class import" selection. You can also completely remove entries under "Packages to use import with *", or specify a threshold value that only uses the "*" when the individual classes from a package exceeds that threshold.

点击工具栏上的设置“扳手”图标,打开“代码样式”下的“导入”,勾选“使用单类导入”选项。您还可以完全删除“要使用导入的包”下的条目*,或者指定一个阈值,*当包中的各个类超过该阈值时,该阈值仅使用“ ”。

Update:in IDEA 13 "Use single class import" does not prevent wildcard imports. The solution is to go to Preferences(?+ ,on macOS / Ctrl+ Alt+ Son Windows and Linux) > Editor > Code Style > Java > Imports tabset Class count to use import with '*'and Names count to use static import with '*'to a higher value. Any value over 99 seems to work fine.

更新:在 IDEA 13 中“使用单类导入”不会阻止通配符导入。解决的办法是去Preferences?+,在MacOS / Ctrl+ Alt+S在Windows和Linux)> Editor > Code Style > Java > Imports tabClass count to use import with '*'Names count to use static import with '*'以更高的价值。任何超过 99 的值似乎都可以正常工作。

回答by Amio.io

The solution above was not working for me. I had to set 'class count to use import with '*'' to a high value, e.g. 999.

上面的解决方案对我不起作用。我必须将 'class count to use import with '*'' 设置为一个高值,例如 999。

回答by Do Nhu Vy

  1. File\Settings... (Ctrl+Alt+S)
  2. Project Settings > Editor > Code Style > Java > Imports tab
  3. Set Class count to use import with '*'to 999
  4. Set Names count to use static import with '*'to 999
  1. 文件\设置... ( Ctrl+ Alt+ S)
  2. 项目设置 > 编辑器 > 代码样式 > Java > 导入选项卡
  3. 类计数设置为使用带有“*”的导入为 999
  4. 名称计数设置为使用带有“*”的静态导入为 999

After this, your configuration should look like: enter image description here

在此之后,您的配置应如下所示: 在此处输入图片说明

(On IntelliJ IDEA 13.x, 14.x, 15.x, 2016.x, 2017.x)

(在 IntelliJ IDEA 13.x、14.x、15.x、2016.x、2017.x 上)

回答by Heungwoo

If non of above works for you, then it is worth to check if you have any packages under Preference > Editor > Code Style > Java > Imports > Packages to Use Import with "*"

如果以上都不适合您,那么值得检查在 Preference > Editor > Code Style > Java > Imports > Packages to Use Import with "*" 下是否有任何包

回答by Alex Green

If you don't want to change preferences, you can optimize imports by pressing Ctrl+Option+o on Mac or Ctrl+Alt+o on Windows/Linux and this will replace all imports with single imports in current file.

如果您不想更改首选项,您可以通过在 Mac 上按 Ctrl+Option+o 或在 Windows/Linux 上按 Ctrl+Alt+o 来优化导入,这将用当前文件中的单个导入替换所有导入。

回答by user2789973

Shortcut doing this on Mac: Press command+Shift+A (Action) and type "class count to use import with *" Press Enter. Enter a higher number there like 999

在 Mac 上执行此操作的快捷方式:按 command+Shift+A(操作)并键入“class count to use import with *”按 Enter。在那里输入更高的数字,例如 999

回答by Kanke

enter image description here

在此处输入图片说明

IntelliJ IDEA 2018.1.4 (Ultimate Edition) built on May 16, 2018

IntelliJ IDEA 2018.1.4(终极版)于2018年5月16日构建

回答by Matt Klein

Like a dum-dum I couldn't figure out why none of these answers were working for my Kotlin files for java.util.*, so if this is happening to you then:

就像傻瓜一样,我无法弄清楚为什么这些答案都不适用于我的 Kotlin 文件java.util.*,所以如果这发生在你身上,那么:

Preferences
> Editor
> Code Style
> **Kotlin**
> Imports
> Packages to Use Import with '*'
-> Remove 'java.util.*'

回答by Neeraj Jain

This applies to "IntelliJ IDEA-2019.2.4" on Mac.

这适用于 Mac 上的“IntelliJ IDEA-2019.2.4”。

  1. Navigate to "IntelliJ IDEA->Preferences->Editor->Code Style->Kotlin".
  2. The "Packages to use Import with '' section on the screen will list "import java.util."
  1. 导航到“IntelliJ IDEA->首选项->编辑器->代码样式->Kotlin”。
  2. 屏幕上的“Packages to use with ' ' 部分将列出“import java.util.

Before

前

  1. Click anywhere in that box and clear that entry.
  2. Hit Apply and OK.
  1. 单击该框中的任意位置并清除该条目。
  2. 点击应用并确定。

After

后