Eclipse, Static imports quickly and GitHub gist.

Every one has a collection of favorite Eclipse templates.  I find the simple ones are often the most useful.  The examples below create you a static import and finish the method your typing.  They even put the cursor in the right place if you need arguments in the method.  I am going to collect more of them using the incredibly handy GitHub gist: My Static Import Templates For Eclipse.  GitHub gists are great for these kinds of programmer reminders.  In addition edits get version control.

Typical Eclipse template for Google Collections

${:importStatic(com.google.common.collect.Lists.newArrayList)}newArrayList();
${cursor}
${:importStatic(com.google.common.collect.Sets.newHashSet)}newHashSet();
${cursor}

To use them, just open Eclipse Preferences: Java Editor: Templates.  Add new templates, and give them a name similar to the method.  I.e newArrayList.  Then your code completion hot key will run the template completing the text your typing in.  i.e you type newArrayList [code complete key] and it adds the static, and the rest of the method, placing your cursor at the end of the line.  Neat!

Note: See the comment from Yuri below, the above templates are no longer required for static imports. Its possible to configure the Content Assist to bring in a whole type, or member using Prefs:Java:Editor:Content Assist:Favorites. This is better as it scans all possible available methods in the type.

The code you end up with is tidy too:

List myList = newArrayList();

There is another thing I can never remember the syntax for! :-)   The markup to add the syntax highlight for this blog. Available as a gist: http://gist.github.com/234709

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

2 thoughts on “Eclipse, Static imports quickly and GitHub gist.

  1. You can do this without templates using Preferences / Java /Editor/ Content Assist / Favorites