Excluding Classes from Auto-Import

IntelliJ IDEA auto-import popup is a very handy feature that lets you stop worrying about import statements in your Java class files. However, sometimes it can show more choices than you need (e.g. when some of the class names in your project match the names of internal JDK or unrelated library classes).

To make auto-import easier to use and to avoid accidental imports of wrong classes, you can open the Settings dialog, then go to Editor | Auto-Import and add their package prefixes to the exclusion list. This will also hide the specified classes from class name completion lists.

This entry was posted in Tips & Tricks and tagged . Bookmark the permalink.

6 Responses to Excluding Classes from Auto-Import

  1. Joseph says:

    This is nice but I think it’s not smart enough implemented (at least not for IntelliJ’s used level :) ):
    - such a setting should be project/module dependent in first place.
    (when switching from one project to the other it is ugly to constantly edit those settings)
    - even more, IntelliJ could be smarter here: e.g. the facets could know what classes they usually don’t require, i.e. in a module when a certain facet is active, than it could automatically exclude from completion.

    Best example I can think of is to exclude the java.awt.* and javax.swing.* classes when having the webmodule active, since many webframeworks also have “TextArea” or “TextFields” , and intelliJ is proposing the AWT/Swing classes in this case.
    (Also in web modules users work allot with the List interface, and IntelliJ is constantly proposing java.awt.List :) ).
    It would be also pretty easy to make a Facet/framework incompatibility/improbability matrix, to make IntelliJ behave smarter.

  2. Pat Niemeyer says:

    Yay! java.awt.List is dead, long live java.util.List!

  3. Stephen Friedrich says:

    I have agree with Joseph.
    Me myself I know not to import stuff from com.sun.*
    However I’d like to forbid that in the project settings.
    For example some colleagues often manage to import @NotNull from a strange com.sun.* package rather than using the idea annotation.

  4. eyrieowl says:

    This must be the location in the more recent versions. Still on IDEA 7.0.4, and the similar setting is under IDE Settings -> Code Completion. Useful tip, though.

  5. Shadow says:

    How do you turn this irritating thing off?!?!?

    It is seriously pissing me the f*&k off!!!

  6. Shadow says:

    Oh thank god, I figured it out!

    Settings -> Editor -> Code Completion -> Preselect the first suggestion: -> Never

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>