Search code examples
javaeclipseimportautocompleteeclipse-plugin

Eclipse - auto import java.util classes on ambiguous imports


Is there a way Eclipse will auto import classes from java package as java.util.List without the need to choose it explicitly in every class?

(even without Ctrl + Shift + O)

When I write List and eclipse auto import java.util.List instead of suggesting irrelevant List as org.apache.xmlbeans.impl.xb.xsdschema.ListDocument.List

I know I can exclude by Type Filters, but I just want specific objects as List to be automatically imported.


Solution

  • Automatically organise import statements whenever you save

    • Go to Window > Preferences > Java > Editor > Save Actions.
    • Select Perform the selected actions on save (off by default).
    • Ensure that Organize imports is selected (on by default).

    Let Eclipse collapse imports in the same package a wildcard (.*) or always expand them

    • Go to Window > Preferences > Java > Code Style > Organize Imports.
    • Change the value of Number of imports need for .* to 0.
    • (Optional) Change the value of Number of static imports needed for .* to 0.

    Exclude unwanted packages using Type Filters

    • Go to Window > Preferences > Java > Appearance > Type Filters.
    • Click Add… to add a package/class.
    • Enter java.awt.List (or java.awt.* if you don’t intend using any AWT classes).

    some these other features

    • Folding: By default, Eclipse folds all import statements into one line so your class takes up less space on the screen. You can change this by going to Window > Preferences > Java > Editor > Folding and deselecting Imports.

    • Sorting: If you’re really particular about the order of packages, you can go to Window > Preferences > Java > Code Style > Organize Imports and define the order of the packages as you want Eclipse to order them. It’s not really worth the effort though so I’d skip it.

    • Compress/abbreviate package names in the Package Explorer (not in your class but in the view): You can display packages compressed (eg. o~.e~.swt) or abbreviated (eg. org.eclipse.swt.custom becomes {SWT}.custom). Go to Window > Preferences > Java > Appearance and define the settings there. The dialog has decent examples of how to do this.