Configure those annoying tooltips in Eclipse to only popup on request

Whenever you hover over any piece of code in Eclipse, it pops up a tooltip that displays more information about the item, such as its declaration, variable values or Javadoc information, as in the example below.

Although useful at times, this becomes extremely annoying after a while, especially when you’re using your mouse to browse some code. Popup after popup of unwanted information keeps obscuring your view of the code, leading to some lengthy expletives and big productivity loss. It’s useful information, but not every time all the time, almost like your car’s GPS giving you directions to 10 different places at once while you’re still parked in the driveway.

Luckily there is a way to alleviate the problem and all it takes is changing some preferences in Eclipse. We don’t want to completely disable tooltips (they can be useful), so I’ll show you how to tell Eclipse to bring up the tooltips only when you request them.

Continue reading

Generate static imports in Eclipse on autocomplete for JUnit Assert, StringUtils and others

Eclipse normally adds import statements for you when you autocomplete a type, press Ctrl+Shift+O or organise imports when you save. However, by default it doesn’t do the same for static imports, something you’ll miss when using classes like JUnit’s Assert and Apache Commons’ StringUtils. This means having to add import statements manually, which takes unnecessary time.

So, in the code below, I want to be able to autocomplete assertEquals (line 7) and have Eclipse add a static import for org.junit.Assert.assertEquals (line 1).

import static org.junit.Assert.assertEquals;
public class MyTest {
   public void testSomething() throws Exception {
      assertEquals(1, 1);

Now, the good news is that there is a way to automatically add static imports when you use autocomplete. It’s done via a preference that tells Eclipse which static classes you want to include for Content Assist (autocomplete). I show you how to do this below.

The bad news is that there doesn’t seem to be a way to tell the Organise Imports command to add static imports. However, a working autocomplete should already cater for 90% of your coding needs, so the bad news isn’t that bad.

Continue reading

Always run/debug the last launched class instead of the selected one in Eclipse

I previously mentioned shortcuts to run/debug a class as a Java app/JUnit test. But have you ever tried to rerun the last application you launched in Eclipse using F11 or the toolbar, only to find out it’s running the currently active class or selection in Package Explorer? Well, that’s because Eclipse’s default is to run the selected resource or active editor if it’s launchable (eg. if it has a main method or is a JUnit test).

Luckily, there is a preference you can change to force Eclipse to always run the last application you launched. So now you can avoid the confusion and rest assured that it’ll do what you want it to do.

Continue reading

Instantly show a class/file in the Package/Project Explorer in Eclipse

While working on a class or XML file, you’ll often find that you want to work with the actual .class or .xml file in the Package Explorer, Project Explorer or Navigator views. You may want to copy, move or delete the file or do stuff that are only accessible in these views (eg. browse all files in the same folder).

Eclipse has a feature called Show In, that’s almost like a Quick Open. It instantly navigates to the current editor’s file in a requested view and positions you directly on the file in that view, no matter how deep in the hierarchy the file is. It’s also keyboard friendly, which means no more reaching for the mouse.

This feature is perfect for those (like me) who don’t like the Link with Editor functionality on certain views. I don’t like the Package Explorer to Link with Editor as I like to know things are the way I left them (see the rant at the end of the post).

Continue reading