Eclipse has some problems with auto import static classes

Joergi picture Joergi · Aug 6, 2012 · Viewed 8k times · Source

I have some problems with eclipse.

if I use something like anyMap() in my source code, and press then CTRL + SHIFT + O no update in the import list will be done.
If I write something like: import static org.mockito.Matchers.* into my imports, the anyMap() is then known.
If press then CTRL + SHIFT + O because i had to import some other classes, the import static org.mockito.Matchers.* will be replaced by import static org.mockito.Matchers.anyMap

If I want to use then anyList(), i have to write the import import static org.mockito.Matchers.* by hand again.

I know this topic Can Eclipse the Organize Import (ctrl+shift+o) command's handling of static imports be modified? and the solution works fine, but is there also a possibility so eclipse automatically knows the static classes i wanna use?

Answer

Ashutosh Jindal picture Ashutosh Jindal · Aug 6, 2012

Yes this is possible .. see this question :

Eclipse Optimize Imports to Include Static Imports

Check the Favorite preference section:
Window » Preferences » Java » Editor » Content Assist » Favorites

Click 'New Type' and add org.mockito.Matchers like so :

Add a New Type

Once you do that, you should have the corresponding entry there like so :

Entry Appears

After that, if you type anyM in the code and do a Ctrl+Space you will see that the static method is one of the content assist proposals. If you choose that, the corresponding import is automatically added :

Proposal

If you type something that matches nothing but the static, the import will automatically be added. For example if you typed anyMap and then pressed Ctrl + Space the import will directly be added.