Eclipse Optimize Imports to Include Static Imports

Eclipse Optimize Imports to Include Static Imports

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.

Eclipse optimize imports to include static members and methods

Did you have a look at Preferences > Java > Editor > Content Assist > Favorites? You can define candidates for static imports there. Result:

Sample Image

For less used classes you can lower the value of Preferences > Java > Code Style > Organize Imports > Number of static imports needed for .* but beware that you may get .* for classes that contain generically named methods such as getInstance. This in turn may lead to confusion and/or naming conflicts.

Is it possible to change the order of static imports followed by imports in eclipse?

You can configure the order of imports in the Preferences in the 'Java > Code Style > Organize Imports' page. This will allow you to specify the order of static and normal imports (and a lot more if you want).



Related Topics



Leave a reply



Submit