Turn off Wildcard imports?

Answered

Is there a way to turn off wildcard import like "import javax.persistence.*;" ?

Intellij 12.1.4 usually imports exactly the class I need, for example:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Table;



But at some point it replaces all the individual import statements with:


import javax.persistence.*;


Is this behavior controlled by a setting anywhere?
4
12 comments

File > Settings > Project Settings > Code Style > Java > Imports > General > Class count to use import with '*'

In The settings dialog you can also search/filter - see field in the top left.

8
Avatar
Permanently deleted user

Thanks, that's exactly what I needed!

0

In IDEA Ultimate 2020.2 the setting is under File > Settings > Editor > Java > General > Class count to use import with '*'

5

In IDEA Ultimate 2021.1.3, the setting is under

File > Settings > Editor > Code Style > Java > Imports > Class count to use import with '*' 

and

Names count to use static import with'*'

fields (as stated in https://www.jetbrains.com/help/idea/creating-and-optimizing-imports.html#disable-wildcard-imports).

 

1

For the notice, there's also 2 hardcoded exceptions (awt & swing) that'll get through the configuration mentioned above. They're basically at the same path as the wildcard count definition, but just below on the same tab, in the " Packages to use import with '*' " section. Remove them and it'll be fine.

On osx, the configuration tab would be at:

Intellij IDEA > Preferences > Editor > Code Style > Java > Imports
2

I put 99 in both of these and still adds * all the time, very annoying.

0

Dominic Tobias Which IDEA version are you using? Does it happens in all Java packages?  Can you share a sample file of the problem?

0

I am using 2024.1 version community edition and get the same problem  as Dominic.

 

When I optimize imports, it goes from:

To:

And these are my settings:

0

Vanesa Pazos  Did disable all downloaded 3rd-party plugin helps?  If it is still there, please attach the logs via the menu Help > Collect logs and Diagnostic Data if possible, please upload it to https://uploads.jetbrains.com/ and paste the upload id here, the uploaded file is only visible to JetBrains employees.

A a minimal sample project would also be helpful.
 

0

I opened a ticket: https://youtrack.jetbrains.com/issue/IDEA-352926/Organize-imports-doesnt-respect-class-count-to-use-wildcard-in-Java

Disabling the plugins helped a bit: when I optimize the imports it doesn't happen anymore that separated imports collapse into * ones. But it still happens that the imports added by Intellij are using the wildcard. 

I had "Add unambiguous imports on the fly" on in Auto import section, and now I unmarked it:

Created a new clean project and in the main class added the creation of an array list. I tried to import manually:

And the result has the wildcard:

I included both the test project and the logs: Upload id: 2024_05_09_23xpQJyeM9RXwxhurijpKV (files: idea-logs-20240509-09505117936355280639079772.zip, wildcardimporttest.zip)

I'll include this same info in the ticket.

Thanks in advance

0

Vanesa Pazos  Thanks for the issue report, I'll take a look at the issue and assign it to our dev.

1

Hello, this problem can be fixed by enable the below settings enabled plus 3rd party plugin disabled: 

Settings | Editor | Code Style | Java | Imports | Use single class import

Use single class import: import only a particular class from a package during code generation or import optimization. Otherwise, a statement that imports an entire package is inserted.

0

Please sign in to leave a comment.