Francois Marot
Francois Marot

Reputation: 1195

How to organize import in Eclipse but not *change* star imports

I'd like to configure the organize import in Eclipse with one caveat: I do NOT want Eclipse to CHANGE the imports related to star imports (like import javax.xml.parsers.*).

I would like Eclipse not to expand (replace the star import with each specific ones) if there is already a star import, but also not automatically add the star when there is not.

In fact, I would like the organize import to just organize the imports (ie reorder), not change them. Any idea ?

I like it when IDE do not mess with developer's will.

Upvotes: 9

Views: 4435

Answers (1)

howlger
howlger

Reputation: 34137

The Organize Imports command can only be executed as a whole (see source code) and the Sort Members command ignores import statements.

As workaround to reorder/sort the import statements alphabetical use the AnyEdit Tools plug-in:

  1. Select the lines of import statements to reorder/sort
  2. Right-click selection and choose Sort > Case-Insensitive A-Z

For .* import statements only, set the number of imports needed for .* to 0 (see Eclipse help - Organize Imports Preferences):

  1. Open Window > Preferences: Java > Organize Imports
  2. Set Number of imports needed for .* (e.g. 'org.eclipse.*') to 0 (instead of 99)
  3. Click Apply and Save

For adding import statements manually or via content assist only, disable Organize Imports on save (see Eclipse help - Clean Up Preferences):

  1. Open Window > Preferences: Java > Code Style > Clean Up (or for project specific settings: right-click project, choose Properties: Code Style > Clean Up)
  2. Click Edit...
  3. In the Code Organize tab uncheck the Organize imports checkbox
  4. In the Unnecessary Code tab uncheck the Remove unused imports checkbox
  5. Click OK and Apply and Save to close both dialogs

Upvotes: 6

Related Questions