« Enterprise Java customers are getting the Microsoft treatment | Main | Bringing agility to desktop apps development »

Jan 22, 2008

Eclipse tip: quickly navigate between method implementations

I know, this feature is not new. It's with us from around version 3.2 of Eclipse. However, I was not familiar with it and I like it very much, hence, I'm sharing.  I like it so much, I think it will make my "Top 10 Eclipse Tricks" (which is a list I should make). This one is for Java developers using Eclipse. I'm sure other IDEs have parallel tools.

A few months ago I wrote a post about Java interfaces. I dwelled over the fact that using interfaces makes it harder to trace the code with static tools. When a class invokes a method through an interface, it can be very difficult (or even impossible) to determine which implementing method is invoked.

Here's the common symptom: you're reading your code and you see a method being invoked, you hit F3 (stands for Navigate->Open Declaration) or use the favorite Ctrl/Command key shortcuts that turns the method names to links. In the normal case, you would now navigate to the method declaration. If the method was invoked through an interface you will get the declaration in the interface, not an actual implementation. Now comes the time-consuming process of looking for the implementing classes of the given interface (use F4 to open the type hierarchy for the interface) and fishing for the method implementation.

Here's the solution: instead of hitting F3, hit Ctrl-T (Command-T on Mac). This is equivalent to choosing Navigate -> Quick Type Hierarchy. What happens next is pure magic:

  1. Eclipse searches for all the implementors of the interface.
  2. Among those implementors, it will search for those who implement the selected method
  3. It will pop-up a small tool-tip like box that shows the relevant implementors hierarchy. In this hierarchy, the interfaces and classes that don't implement the method are grayed out or not shown.
  4. You can now select a class. It even provide a quick-type selection: start typing the name and it will focus on the matching class.
  5. You navigate to the method implementation in the selected class.

Here's how it looks like.

Eclipse_quick_type_hierarchy

The "Quick Type Hierarchy" feature works in other contexts as well. Here's some examples:

  • When you're on a class or interface - it will show the hierarchy.
  • If you're on field, it will show where the field is defined in the hierarchy

Check it out, it's a very useful feature.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83548421753ef00e54feaf95a8833

Listed below are links to weblogs that reference Eclipse tip: quickly navigate between method implementations:

Comments

hi,
using ctrl+t calculates the entire tree of implementations (including the transitive impls). this can take quite a while for huge class hierarchies.
thus, i think you might be interested in the eclipse Implementors[0] plugin. it adds the shortcut alt+F3 which takes you directly to the implementation (in case there is only one), or presents a dialog with multiple implementations.

version 0.0.16 works for eclipse 3.2+. i'm currently using it on 3.4M4 and it works like a charm. huge time saver.

cheers,
Gerolf
[0] http://eclipse-tools.sourceforge.net/implementors/

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

About nWire

  • nwire logo
    nWire is an Eclipse™ Plug-in which expedites Java development by assisting the developer in navigating through the code and better understanding it.

    Learn more at nwiresoftware.com
My Photo

My Other Accounts

Delicious Digg Facebook Flickr FriendFeed Google Talk Last.fm LinkedIn Reddit Skype StumbleUpon Technorati Yahoo!

AddThis Social Bookmark Button
Blog powered by TypePad
Member since 05/2007