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.
Related
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
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.
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".
I use eclipse as my scala ide. And I know that in java I can use short cut by content assistent to import classes. So I do not need type in the whole class name. Just need to type the first several characters.
But in scala, I can not import classes automatically. Do I need to do some set up or it is just because scala plugin do not support this.
Ctrl + 1
on symbol works for me :)
There is Scala IDE for Eclipse however it is not very mature these days, however improving day by day. As far as I remember it can import automatically, or you can use Eclipse global fix import hotkey ctrl+shift+o.
I have a very big class with lots of methods, is it possible to build an interface from this class using Resharper?
Yes.
My shortcut is Ctrl + Shift + R to bring up the refactoring options. Doing this on the class name allows you to" Extract Interface..."
Optionally, you can choose from the menu > ReSharper > Refactor > Extract Interface...
Uh, maybe I'm missing something here (I've never used resharper) but you can extract an interface from a class using the standard VS IDE refactoring tools (at least you can in 2008). Right click the class, select 'Refactor' and then 'Extract Interface'. This will bring up a dialog box where you can select which properties to include.