Prevent eclipse from putting static imports on top - eclipse

Is there anyway I can stop eclipse from moving static imports to the top? We have code that doesn't have static imports on top and every time i hit organize imports it causes a long diff in the code.

You can define the preferred sort order in the "Organize Imports" preferences. This tab also allows you to specify where you want specific static imports.
If you want Eclipse to not change the order of imports at all, then you must never use "Organize Imports." Use "Remove Unused Imports" instead.

You can add both normal and static imports with *, and then choose the order.

Related

How to keep import * for specific package when organizing imports in Eclipse

For some imports, I may want to keep a * import even I am currently using only 1-2 classes/methods. For example, I may want to import org.mockito.Mockito.* even I am currently using only a few of them, because when my test grows, I will need to use more static method under Mockito. However Organize Imports function in Eclipse always break down my import to import individual methods (which is the preferred behavior in most case).
Is there a way to let me control such behavior in package basis? i.e. I want to keep importing * for some packages (or disabling auto-break-down feature), but for other packages, I would want to use Eclipse's default behavior (deciding * base on number of classes/methods used)
You can't change the policy for individual imported packages or classes, but you can set the overall threshold lower so that Organize Imports won't convert the wildcard import to individual ones. Go to Preferences > Java > Code Style > Organize Imports and then edit the Number of static imports needed... value to something small, like 2.
With it set to a small number, you can manually add a wildcard import and as long as the class uses at least the threshold number of methods, Eclipse will leave it alone.
This doesn't appear to be possible at present, but as it's clearly a useful idea I've added a feature request to eclipse bugzilla for it: https://bugs.eclipse.org/bugs/show_bug.cgi?id=506826

Auto Import With Eclipse Snippets

I started using snippets recently. The most common one I use inserts this at the cursor:
private Logger log = LoggerFactory.getLogger(getClass());
However, I still have to do a CTRL-SHIFT-O to import Logger and LoggerFactory.
Out of pure laziness I ask this: Is there a way to do an automatic import when I insert this snippet?
I don't think a Snippet can automatically insert an import statement (or do anything other than insert text at the current cursor location). But you can set up an automatic Save Action to do the organize imports whenever you save. Open the project properties and navigate to Java Editor > Save Actions.
I usually set up a few different things in the Additional Actions including Organize Imports, removed trailing whitespace, insert missing annotations, etc.

Eclipse optimize imports to include static members and methods

Long user of eclipse and Java. One issue that i've found with Eclipse, is it seems that there is no easy way to import static members and methods.
Namely, the jUnit fail() method from org.junit.Assert
I create several classes a day, and manually add
import static org.junit.Assert.fail;
to the import statements. This is quite annoying. I absolute LOVE using Ctrl+Shift+O to organize my imports, but it still doesn't find static members and methods.
Also, the import does not show up in eclipse.
Funny thing is, is that i've seen it work previously, but i cant recall the variables.
So to my question:
Does anybody know what I need to do to ensure that this static import is always recognized and can be found using Ctrl+Shift+O?
Thanks #qqilihq.
Note:
The answer that was accepted does not work with the Organize Imports keyboard shortcut that I preferred in eclipse, but does work for the "hover over" suggestion.
You can use Ctrl + Shift + M, for example you want to import verify method from Mockito class then
Mockito.verify() // select verify and press Ctrl + Shift + M
This will import verify static method from Mockito class.
Did you have a look at Preferences > Java > Editor > Content Assist > Favorites? You can define candidates for static imports there. Result:
For less used classes you can lower the value of Preferences > Java > Code Style > Organize Imports > Number of static imports needed for .* but beware that you may get .* for classes that contain generically named methods such as getInstance. This in turn may lead to confusion and/or naming conflicts.
You can add the classes that you statically import from Preferences > Java > Editor > Content Assist > Favorites page in Eclipse. Then, Ctrl+Space shortcut lists all the static members of your favourite classes in the content assist menu.

Eclipse/IntelliJ: static imports of members

I'm refactoring something affecting many bits of my code (huge), and I ended up in needing to organise the import of my project so it can add a static import of a member in a class..
After having spent a full day trying to figure out how to do that, I can't find the way. It's not working neither in IntelliJ or in Eclipse.
In Eclipse I have added it to the Favorites settings, just like it is explained here:
Eclipse Optimize Imports to Include Static Imports
But, it only works on the Control + space, not on Control+Shift+O (to rearrange imports)
In Eclipse 4.2 at least it draws the line red and propose me the suggestion to add the import, but it still doing nothing on the "Organise Import" trigger.
In IntelliJ it is the same, adding the package and the class to the setting, fixes the Control+Alt+Space, but it doesn't deal with the "Optimise Import" feature.
I can't believe this. Has anybody had to deal with this?
You don't want to perform import static on just everything; if the class contains class members that are too general, then an import static will only make your code unreadable. Besides that, there may be name clashes (e.g. two classes having a static getInstance() method would be quite common.
With IntelliJ 13.1.3 you can hit Alt-ENTER, then choose to do a static import (older versions may require one or more CTR-Space before the Alt-ENTER. If you do this on a static member then only the static member is statically imported. If you do it on a class you can do to statically import everything (e.g. import static some.package.SomeClass.*). In the latter case it will remove the class in front of the static imports, unless there is a name clash.
Beware, even if there is no name clash, doing things like SomeInterface someInstance = getInstance() - where getInstance() was statically imported - will get your colleagues very nervous.
Please re-read the question you referred to. You missed the answer mentioning Ctrl + Shift + M. There is a difference between the commands "Organize Imports" and "Add import".

Is there a way to tell Eclipse how to resolve ambiguous java names?

My eclipse is configured so that when I save the java source file, it automatically inserts the missing import declarations. Except when the reference is ambiguous. Fpr instance, the most annoying ambiguity is with List<T> - both java.util and java.awt declare it. Here eclipse demands manual resolution.
I was wondering if it was possible to configure somewhere that whenever List<T> is used then java.util should be imported. Or alternatively, since I am not using java.awt, I could just remove it from the list of possible suggestions.
Any ideas?
Thanks.
This sounds like a possible duplicate of Exclude packages from Eclipse's organize imports.
Basically, you want to change your Type Filters preference to exclude java.awt.* packages. Keep in mind that doing so will make things harder/confusing if you ever try to write AWT/Swing code.
One thing that pops into my mind is altering the class template in the preferences to include the line:
import java.util.List
Unless you are going to use the AWT version of a List, or care about unused import statements, that should solve this annoying issue ..
Manually invoke Organize Imports and it will ask you when an ambiguity is found.