ChatGPT解决这个技术问题 Extra ChatGPT

IntelliJ: Never use wildcard imports

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?

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.


G
Guildenstern

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.

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 + S on Windows and Linux) > Editor > Code Style > Java > Imports tab set 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.


In case it saves someone else the confusion: In my enthusiasm for never collapsing imports, I put an unreasonably long string of 9's in the 'class count to use...' field, and this caused IntelliJ 12 to silently ignore the value. '9999' works fine.
Is there really no way to tell Intellij 15 never to use wildcard imports? I mean without specifying a class count??!
Why is adding a class count so oppressive? Why not ask JetBrains for an enhancement if this is so odious?
Looks like three nines should be enough. If you need more than a thousand imports I'd say your class should be refactored.
Can't believe programmes programmed a program for programmers the way this is programmed. This confusing dialog is still not fixed in 2016.3, is there a bug for it, or somewhere we can vote?
M
Michael

File\Settings... (Ctrl+Alt+S) Project Settings > Editor > Code Style > Java > Imports tab Set Class count to use import with '*' to 999 Set Names count to use static import with '*' to 999

https://i.stack.imgur.com/J4xwT.png

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


M
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:

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

You're not the only dum-dum out there.. /:( :)
I had the same issue with Groovy.
K
Kanke

https://i.stack.imgur.com/Da7X3.png

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


A
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.


C
CoolMind

In Android Studio with Kotlin.

File > Settings,

Editor > Code Style > Kotlin, then "Imports" tab, select "Use single name import".

Also remove import java.util.*.

https://i.stack.imgur.com/eBT1d.png


Thanks, @CoolMind. This is the only thing that worked for me - I couldn't just uncheck import java.util.* (macOS Big Sur / Android Studio Arctic Fox 2020.3.1 Patch 1).
u
user84

This applies for "Intellij Idea- 2020.1.2" on window

Navigate to "IntelliJ IDEA->File->Settings->Editor->Code Style->java".

https://i.stack.imgur.com/94GRg.png


N
Neeraj Jain

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

Navigate to "IntelliJ IDEA->Preferences->Editor->Code Style->Kotlin". The "Packages to use Import with '' section on the screen will list "import java.util."

https://i.stack.imgur.com/BXCYt.png

Click anywhere in that box and clear that entry. Hit Apply and OK.

https://i.stack.imgur.com/xwDkO.png


H
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 "*"


S
Solubris

Adding the following to the .editorconfig file saves having to apply the settings every time the project is reimported from scratch:

[*.java]
ij_java_names_count_to_use_import_on_demand = 999
ij_java_class_count_to_use_import_on_demand = 999

@wakedeer not that I'm aware of, this was the original question asked and consensus so far seems to be to use 999.
999 is enough, but the max is 2147483647. This is from autogenerated kotlin rule I found.
u
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


A
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.


IntelliJ considers collapsing imports with * to be an optimization, by default.
I disagree with "you don't want to change preferences" but the keyboard shortcut worked for me!
If you change the settings like this and then Ctrl + Alt + O (on Linux) or Ctrl + Option + O (on Mac), then the IDE will remove the Wildcard imports and add necessary single name imports.
S
Sandro

On a related note: If your java.util packages are still not resolved to single imports today then it might be, that you are looking at a Kotlin file while trying to change the settings for Java like I did. :-) There are the same settings for Kotlin that fixes that.