In Eclipse it is possible to configure certain "favorite" classes which will be looked up when code completetion is invoked to see if a static import can be added for a method (this is under Preferences > Java > Editor > Content Assist > Favorites).
For example, I can begin to type assertT
, and Eclipse will ask if I want to add a static import of org.junit.Assert.assertTrue;
.
Is it possible to do the same thing in IntelliJ?
The method detailed in this question will add a *
import for the type (static import org.junit.Assert.*
), but I do not want to add star-imports. I'd prefer to import just the methods I am using.
In the Settings/Preferences dialog ( Ctrl+Alt+S ), click Editor | General | Auto Import. Enable the Optimize imports on the fly option and apply the changes.
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.
In Java, static import concept is introduced in 1.5 version. With the help of static import, we can access the static members of a class directly without class name or any object. For Example: we always use sqrt() method of Math class by using Math class i.e. Math.
Strictly speaking, this does not answer your original question.
However, IDEA 10 supports Ctrl-Alt-Space completion for static methods. For example, in a JUnit 4 test that does not import anything, type "assEq" and invoke Ctrl-Alt-space. The resulting dialog will also allow you to import this method statically.
Once you're imported a single method (like assertEquals), other static methods from the same class will be included in the regular code completion suggestions (although you can still use the Ctrl-Alt-Space shortcut, of course).
I find this feature, combined with the "automatic import blacklist", to work very well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With