10

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.

Community
  • 1
  • 1
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • If you use the content assist or do a refactoring, the import statement is automatically added. In addition, you can do _Organize imports_ on save. But this would require avoiding ambiguities by using _Type Filters_ or/and by restricting visibility of specific types in a specific library. – howlger Sep 03 '18 at 06:54
  • @howlger I see in Content Assist (Neon) that it's only for single proposal – Ori Marko Sep 03 '18 at 07:03
  • If you write the code statement by statement using the content assist or e. g. doing TDD using _Quick Fixes_ (Ctrl+1), no import statements will be missing. How did it come to the situation that import statements are missing? – howlger Sep 03 '18 at 07:24
  • 1
    @howlger it's not missing, I just want automatically import `java.util.List` using include, instead excluding the other dozen options – Ori Marko Sep 03 '18 at 07:28
  • Do you actually have dozens of type definitions (classes, interfaces or enums) named `List`? – howlger Sep 03 '18 at 07:42
  • @howlger Yes, see screenshot in question – Ori Marko Sep 03 '18 at 07:49
  • I see. The following five type filters should serve the purpose: `a*.List`, `c*.List`, `o*.List`, `java.awt.*` and `javax.*.List`. However, I would recommend modularizing the code e. g. by using several interdependent projects or using OSGi. – howlger Sep 03 '18 at 08:11
  • The IDE learns and recommends to you the frequently used on the top. Apart from that, you have to manually select the appropriate one yourself. – Mukul Bansal Oct 15 '19 at 12:39
  • 1
    @MukulBansal it's quite annoying re-selecting List every class – Ori Marko Oct 15 '19 at 12:40
  • 1
    @user7294900 Have you seen `eclipse Project —> Properties->Java Editor —> Save Actions` tab? Try there `Organize imports checkbox`. – Kamil Witkowski Oct 16 '19 at 13:01
  • @yami Thanks, it help to find it first in list, can I auto import it? – Ori Marko Oct 16 '19 at 13:07
  • @user7294900 I don't use Eclipse, but as i understand it - whenever you save a file this action occurs. Please let me know if you are happy with that as an answer. – Kamil Witkowski Oct 16 '19 at 13:10
  • @yami it's not a complete answer, but it helps in the right direction and maybe it can't be done unless there is a new enhancement – Ori Marko Oct 16 '19 at 13:15

1 Answers1

5

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.
Pawan Maurya
  • 387
  • 2
  • 11