Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Eclipse the Organize Import (ctrl+shift+o) command's handling of static imports be modified?

I use the Eclipse keyboard shortcut Ctrl + Shift + O all the time (Organize Imports).

However, it has one particularly annoying behavior:

If I have any static imports like:

import static java.lang.Math.*; 

Organize Imports will "helpfully" replace it with static imports of only the fields and methods I am actually using. For example:

import static java.lang.Math.PI; import static java.lang.Math.cos; 

Is there any way to remove just this behavior from Organize Imports?

like image 405
ulmangt Avatar asked Mar 22 '12 21:03

ulmangt


People also ask

How do I organize imports in Eclipse?

Automatically organise import statements whenever you save Here are the steps to organising imports 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).

What does Ctrl Shift O do in Eclipse?

In Eclipse, you press CTRL + SHIFT + O “Organize Imports” to import packages automatically. For IntelliJ IDEA, if you press CTRL + ALT + O “Optimize Imports”, it just removes some unused imports, never imports any package.

What is Ctrl G in Eclipse?

CTRL + G Find **declarations** in workspace. CTRL + Shift + G Find **references** in workspace. If you want to change those,open Windows->Preferences->General->Keys. Now you can use the filter to find your shortcut and change its binding. Follow this answer to receive notifications.


1 Answers

Sure: Window -> Preferences -> Java -> Code Style -> Organize Imports

Adjust the value for "Number of static imports needed for .*" to 1, and it will always use .*. Of course, that's not quite the same as saying, "Just leave static imports alone" but hopefully it's what you want.

Basically, this number says "Use all of the methods or fields of this import if I'm importing this many items" So if the number is 2, it will add the asterisk only if you use two or more imports. That will fix the problem you said in the response.

like image 103
Jon Skeet Avatar answered Oct 03 '22 00:10

Jon Skeet