Tomas Kramar lives in Slovakia, where he works for a company developing software for a major local telecommunication provider. He is also a student at the Slovak University of Technology. Tomas has posted 6 posts at DZone. You can read more from them at their website. View Full User Profile

Effective Eclipse: Fix it, Quickly

03.14.2008
| 13705 views |
  • submit to reddit

You are in trouble, red lines are everywhere. There is no easy way out, so you just either start googling what went wrong, or start a copy&pasting session. If it is so smart, it can tell me that I have an error in my code, why cannot it just fix it?

Let me introduce you to a powerful ally: Quick Fix.

A small example

On the image above is a well-known situation. The method is throwing a checked exception, so the calling method must either enclose it in a try-catch block, or rethrow it. A typical solution to this problem is to manually write the try-catch block or throws clause. But there is a better way, the way of quick fix.

It can be invoked in two ways

  • clicking on the bulb, left of the line number
  • hitting CTRL + 1 - the preffered way

The result will be..

..that quick fix will propose you both fixes. In the yellow box is a preview, it looks messy, but the code will be properly formatted. If you don't like the generated code, you can change it under Window->Preferences->Java->Code Style->Code Templates. I don't like the default e.printStackTrace(), so I changed the Catch block body template to

logger.severe("Exception caught: " + ${exception_var});

Whenever a red line appears, quick fix can eliminate it. It is applicable not only to the situation above, it can fix:

  • Typos


  • Imports


  • Casting

    Starting with the simple casts...



    .. and even more intricate



    .. resulting in


  • and any other kind of problem I am unable to think up at the moment.

I found quick fix helpful even in the desperate situations like this one:

Project needs to migrate WTP metadata? Ok then, have your fun.

When there is nothing to fix, quick fix turns itself to a quick refactoring. Let's see what will it propose on our list.

See, it provides all sorts of context sensitive advices. Refactoring, renaming, annotations, everything is there, hidden under one shortcut key. A powerful shortcut key, definitely.

The quick fix feature can be even used to reduce typing. Imagine we call a method join(), which returns a Set<SortedSet<Item>> and we want to store the return value in a variable. If you did not run after seeing that behemont, you can let quick fix do the hard work.

References
Published at DZone with permission of its author, Tomas Kramar. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: