score:586

Accepted answer

i'm using eclipse europa, which also has the favorite preference section:

window > preferences > java > editor > content assist > favorites

in mine, i have the following entries (when adding, use "new type" and omit the .*):

org.hamcrest.matchers.*
org.hamcrest.corematchers.*
org.junit.*
org.junit.assert.*
org.junit.assume.*
org.junit.matchers.junitmatchers.*

all but the third 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.

score:1

in eclipse 4.9, you can static import existing invocations using a quick fix.

a new quick fix has been implemented that allows the user to convert static field accesses and static methods to use a static import. it's also possible to replace all occurrences at the same time.

more details here

score:2

select the constant, type

ctrl + 1  (quick fix)

select "convert to static import." from the drop down.

"quick fix" has options even though it is not an error.

score:5

for springframework tests, i would recommend to add the below as well

org.springframework.test.web.servlet.request.mockmvcrequestbuilders
org.springframework.test.web.servlet.request.mockmvcresponsebuilders
org.springframework.test.web.servlet.result.mockmvcresulthandlers
org.springframework.test.web.servlet.result.mockmvcresultmatchers
org.springframework.test.web.servlet.setup.mockmvcbuilders
org.mockito.mockito

when you add above as new type it automatically add .* to the package.

score:5

shortcut for static import: ctrl + shift + m

score:10

from content assist for static imports

to get content assist proposals for static members configure your list of favorite static members on the opens the favorites preference page java > editor > content assist > favorites preference page.
for example, if you have added java.util.arrays.* or org.junit.assert.* to this list, then all static methods of this type matching the completion prefix will be added to the proposals list.

open window » preferences » java » editor » content assist » favorites

enter image description here

score:22

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 (ctrl + 1) on the line it will give you the option to add the static import which is good enough.

score:31

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.

score:244

if you highlight the method assert.assertequals(val1, val2) and hit ctrl + shift + m (add import), it will add it as a static import, at least in eclipse 3.4.


Related Query

More Query from same tag