Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse has some problems with auto import static classes

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?

like image 241
Joergi Avatar asked Aug 06 '12 13:08

Joergi


People also ask

How do I fix all imports in eclipse?

Go to the line of unused import, press Ctrl + 1, which is an Eclipse shortcut of a quick fix. This will show a drop-down menu to fix this error and one of them will be “remove unused imports.” It will remove that import statement from the Java file.

Does Eclipse auto import?

1. Automatically organise import statements whenever you save. Eclipse can automatically format code every time you save.

Should we use static import in Java?

So when should you use static import? Very sparingly! Only use it when you'd otherwise be tempted to declare local copies of constants, or to abuse inheritance (the Constant Interface Antipattern). In other words, use it when you require frequent access to static members from one or two classes.

How do I add a static import?

In Eclipse IDE, you can select the whole reference variable and press Ctrl+Shift+M and it will automatically import that static element using static import in Java.

How to auto import java class in Eclipse?

Click Source —> Organize Imports menu item or Shift+Ctrl+O in Eclipse, then the java class will be imported in the java source file automatically. 2. Auto Import Java Class In All Java Project Files. If you have a lot of java source file need auto import used java classes, you can config your java project to achieve it.

How do I add a static import in Eclipse?

Next you have to tell Eclipse which classes you’d like to add as static imports. Here’s how to do this: Go to Window > Preferences > Java > Editor > Content Assist > Favorites. Click New Type. This brings up a dialog where you can enter the fully qualified type.

How do I add static classes to autocomplete in Eclipse?

Now, the good news is that there is a way to automatically add static imports when you use autocomplete. It’s done via a preference that tells Eclipse which static classes you want to include for Content Assist (autocomplete). I show you how to do this below.

How do I test if an import is working in Eclipse?

Now, to test that it works you can add a JUnit test, type assertEquals, press Ctrl+Space and press Enter on the appropriate entry. Eclipse will now add the static import org.junit.Assert.assertEquals (or org.junit.Assert.*, depending on your Organize Imports preferences).


1 Answers

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.

like image 60
Ashutosh Jindal Avatar answered Oct 21 '22 05:10

Ashutosh Jindal