Sunday, February 12, 2012

Excellent tip for importing static methods in eclipse

One of the great features of Java 1.5 is Static Imports. In order to configure Eclipse to search for static imports in a particular class, you have to perform the following steps:


1. Navigate to Preferences by clicking on the Window -> Preferences Menu Item

2. Navigate to Java -> Editor -> Content Assist -> Favorites using the menu tree (or search for Favorites using the search bar at the top)

3. Click the New Type button
4. Type in the name of the Class that has static methods that you would like to be used when using Eclipse's Content Assist / Code Completion (eg Assert)
5. Click on the Browse button which will bring up the Open Type Dialog using what you entered previously as the search criteria
6. Find the class that you would like to add, and then click Okay on the Open Type Dialog
7. Then Click Okay on the New Type Favorite Dialog.

Now when you are editing Java code, instead of typing Assert.assertEquals, you only need to type assertEquals, with Ctrl-Space, and the Assert Type will be searched for in order to resolve the static import.

No comments: