17

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?

Community
  • 1
  • 1
Joergi
  • 1,527
  • 3
  • 39
  • 82

1 Answers1

42

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.

Community
  • 1
  • 1
Ashutosh Jindal
  • 18,501
  • 4
  • 62
  • 91
  • 1
    a short question again: it works perfect for autocompletion - but it's still not working by pressing `CTRL + o` – Joergi Aug 27 '12 at 08:55
  • 2
    `Ctrl + o` is to open a resource in the workspace. Did you mean `Ctrl + Shift + o` to organize imports ? – Ashutosh Jindal Aug 27 '12 at 11:10
  • sorry, yes i mean `CTRL + SHIFT + O` - it's not importing the static classes, it will only import by autocompletion – Joergi Aug 28 '12 at 09:40
  • 1
    @Joerg, I could not find a way to include static imports when doing an Organize Imports (Ctrl + Shift + O). However the fast code plugin was very helpful while adding static imports : http://fast-code.sourceforge.net/documentation.htm (Search for `Create Import`). – Ashutosh Jindal Aug 28 '12 at 17:59