Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add favorite methods to static import in IntelliJ?

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;.

IntelliJ editor

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.

like image 826
matt b Avatar asked Feb 24 '11 15:02

matt b


People also ask

How do I auto optimize imports in IntelliJ?

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.

How do I arrange imports in IntelliJ?

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.

How do I import a static method?

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.


1 Answers

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.

enter image description here

like image 125
Pakka Pakka Avatar answered Oct 12 '22 10:10

Pakka Pakka