views:

3653

answers:

5

Is there anyway to get Eclipse to automatically look for static imports? For example, now that I've finally upgraded to Junit 4, I'd like to be able to write:

assertEquals(expectedValue, actualValue);

hit ctrl-SHIFT-O and have Eclipse add:

import static org.junit.Assert.assertEquals;

Maybe I'm asking too much.

+5  A: 

Not exactly what I wanted, but I found a workaround. In Eclipse 3.4 (Ganymede), go to Window->Preferences->Java->Editor->Content Assist and check the checkbox for "Use static imports (only 1.5 or higher)".

This will not bring in the import on an Optimize Imports, but if you do a Quick Fix (crtl-1) on the line it will give you the option to add the static import which is good enough.

Paul Croarkin
+6  A: 

If you highlight the method Assert.assertEquals(val1, val2) and hit Ctrl-Shirt-M (Add Import), it will add it as a static import, at least in Eclipse 3.4.

Dave L.
+10  A: 

Eclipse 3.4 has a Favourites section under Window->Preferences->Java->Editor->Content Assist

If you use org.junit.Assert a lot, you might find some value to adding it there.

Bill Michell
+1 for providing path to preferences screen.
Grundlefleck
+26  A: 

I'm using Eclipse Europa, which also has the Favorites preference section. In mine, I have the following entries

org.hamcrest.Matchers.*
org.junit.*
org.junit.Assert.*
org.junit.Assume.*
org.junit.matchers.JUnitMatchers.*

All but the second of those are static imports. By having those as favorites, if I type "assertT" and hit Ctrl-Space, Eclipse offers up assertThat as a suggestion, and if I pick it, it will add the proper static import to the file.

Joey Gibson
+1 what a handy thing :-)
Grundlefleck
+1  A: 

With http://fast-code.sourceforge.net/ plugin you could static import a class. Then all the static member will come as code assist. You might want to take a look at that.

fastcodejava