28

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.
enter image description here

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.

ddavison
  • 28,221
  • 15
  • 85
  • 110
  • Possible duplicate of [Eclipse Optimize Imports to Include Static Imports](https://stackoverflow.com/questions/288861/eclipse-optimize-imports-to-include-static-imports) – Pshemo Sep 13 '17 at 00:33
  • This question was asked 5 years previous to that question there. I've updated the title though, to better show the intent. – ddavison Jan 17 '22 at 16:46

3 Answers3

34

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.

Sachin Gorade
  • 1,427
  • 12
  • 32
28

Did you have a look at Preferences > Java > Editor > Content Assist > Favorites? You can define candidates for static imports there. Result:

enter image description here

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.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
qqilihq
  • 10,794
  • 7
  • 48
  • 89
  • that definitely worked! and judging by the description of that particular functionality in eclipse, looks like it's specifically designed for my issue. Thanks! – ddavison Oct 17 '13 at 19:57
  • @sircapsalot 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 e.g. contain a `getInstance` method. qqilihq, should I include this in the answer or should I create a separate one? – Maarten Bodewes Oct 16 '14 at 12:19
  • 3
    I'm actually using IntelliJ IDEA now. much smarter editor. I don't have to do these favorites anymore :) – ddavison Oct 16 '14 at 13:30
  • @sircapsalot Yeah, smarter editor, less smart parsing/compiling. I guess you cannot have your cake and eat it. I'm "stuck" with Eclipse due to a specific plugin I require... – Maarten Bodewes Oct 16 '14 at 14:17
  • For those who can't get it to work, make sur you add the correct line in the config. In the context of this question, it is "org.junit.Assert.*" and not "org.junit.Assert.fail" – pmartin8 Apr 24 '20 at 12:22
2

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.

ovunccetin
  • 8,443
  • 5
  • 42
  • 53