If you’re using Eclipse and working with a class with a lot of static members, you might be affected by the problem of
lack of content assist and automatic import for such members. Let’s say you’re using a Hamcrest matchers; you
need a is()
, notNullValue()
, hasEntry()
and so on (there are plenty of those). You don’t want to type
Matchers.hasEntry()
every time you use it in your code. Hey, that’s not why the fluent API was invented in the first place!
So, instead you do a static import
which is available since Java 5:
import static org.hamcrest.Matchers.*;
Ok, you needed to type it by yourself, but still, now you can use just hasEntry()
and everything works smoothly!
But then, after some time, you do the Organize import
magic (ctrl + shift + o
) which automatically replaces your
wildcard import static with concrete import static members, so you end up with:
import static org.hamcrest.Matchers.hasEntry;
Later on, if you want to use another matcher, e.g. notNullValue()
you need once again type the import statement by
yourself. That’s ugly, time waste manufacture which we should get rid off!
Luckily, there is an Eclipse setting which allows you to add some specific static members or whole types into the
regular content assist (ctrl + space
) without writing any import statement by yourself.
To set this, just go to: Window -> Preferences
and then under Java -> Editor -> Content Assist -> Favorites
add a
Type (all static members within the given type will be added) or Member (just the particular static will be added):
From now on, all defined members and types will be available when you hit the ctrl + space
. Organize imports will
never again be your enemy! ;-)