IntelliJ Static Import Completion

JavaIntellij Idea

Java Problem Overview


In Eclipse I was able to register a set of classes for static import auto-completion, e.g. Math., Assert., EasyMock.*

With this enabled I was able to hit ctrl-space assertEquals, pow, createMock etc. would appear as valid code completions.

I'm struggling to find this in IntelliJ but am sure it must exist. Can anyone help?

Thanks!

Java Solutions


Solution 1 - Java

Alt + Enter will do the trick. You have to have the whole expression at first, and the hit Alt + Enter on the method you want to statically import.

statically import IntelliJ

Solution 2 - Java

Version >= 11:

Settings -> Code Style -> Java -> Imports (tab).

Version < 11

Settings -> Code Style -> Imports

In the middle of the pane is the "Packages to Use Import with '*'" table. You can add an entry here of a fully-qualified class name, and tick the static box; now all static methods in this class will be available for auto-completion.

(I'm not sure how the static import works with specifying a package, as I've never tried it, but I don't see why it wouldn't. Specifying a super-package and ticking the "with subpackages" option could be even more interesting.)

Solution 3 - Java

If you're using IntelliJ 10, try the following:

assEq<Ctrl-Alt-Space>

Accepting a suggestion from the resulting popup list will, by default, insert a ClassName.methodName() reference (which you can convert to a static import using an Alt-Enter intention).

You can also insert a statically imported method from the completion list by choosing "Right" in the completion menu, and selecting "Import Statically":

enter image description here

Note that once you've statically imported a single method from a class (Assert.assertSame), other static methods from that class (like Assert.assertEquals) will be included in the "regular" code completion (Ctrl-Space).

Solution 4 - Java

For Intellij 12 just hit <ctrl + spacebar(twice)>. Then to import the method statically hit <alt + return>. Otherwise just hitting enter will insert the fully qualified name of the method.

Update: Sometimes just using <alt + return> works too.

Solution 5 - Java

In OS X you need to do a option + return.

Solution 6 - Java

Just do the reference once, then put the caret on the class name (ie Math), press alt+enter and choose "add on demand static import for 'java.lang.Math'. Intellij will add

import static java.lang.Math.*;

to the top of the file.

Solution 7 - Java

Now its possible to add live templates with static imports: > You have to check static import in Options

@org.junit.Test
public void should$EXPR$when$CONDITION$() {
    org.junit.Assert.assertThat(null, org.hamcrest.CoreMatchers.is(org.hamcrest.CoreMatchers.nullValue())); 
}

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionScruffersView Question on Stackoverflow
Solution 1 - JavanucatusView Answer on Stackoverflow
Solution 2 - JavaAndrzej DoyleView Answer on Stackoverflow
Solution 3 - JavaPakka PakkaView Answer on Stackoverflow
Solution 4 - JavaloyalBrownView Answer on Stackoverflow
Solution 5 - JavaJohn DewellView Answer on Stackoverflow
Solution 6 - JavaAndreas WederbrandView Answer on Stackoverflow
Solution 7 - JavaJorge TovarView Answer on Stackoverflow