views:

755

answers:

3

I often have to debug java code which was written so that there is an interface and exactly one implementation of that interface.

For instance there would be an interface Foo with exactly one implementation called FooImpl. In the following code if I ctrl-click on doThings it'll jump to Foo.java when I actually want to go to FooImpl.java to see the implementation.

public void doStuff(Foo foo) {
    foo.doThings();
}   

When I end up at the interface I have to use ctrl-shift-r to open up FooImpl. It'd be really nice if I could do something lick ctrl-alt-click on doThings and end up inside FooImpl.java. If there are multiple implementations in the workspace then perhaps it would just pop up a box telling me what they are.

Is there a plugin or existing function in eclipse that does this? I know that I could go to Foo.java and then get the hierarchy and go to the implementation, but that's more clicks than are necessary when there is exactly one implementation of an interface.

+8  A: 
  1. move the cursor to the method call
  2. press Ctrl-T
  3. select your desired implementation
  4. hit enter

This also works if there are several implementors.

meriton
holy crap, I've been wondering how to do this for years, thank you so much! seems so obvious in retrospect
matt b
That works great! It'd be even better if it just jumped directly to the implementation if their is only one implementation, but it's a lot better than what I was doing.
HappyEngineer
+1  A: 

The Implementors plugin does pretty much exactly what you ask for. If there is only one implementation it will open it directly, otherwise it will let you choose.

Micke
The solutions provided by meritron and Stephen both work great, but your solution gives me exactly what I want! I can hit a key and jump to the implementation! It defaults apparently to mapping to alt-f3, but I changed it to be ctrl-alt-t so that it works alongside the solution provided by meritron. Thanks!
HappyEngineer
You're welcome! The default keyboard shortcut to open a declaration is F3, so I guess that's why the plugin authors chose Alt+F3 for their similar functionality, but naturally you can choose whatever suits you best.
Micke
+5  A: 

In Eclipse 3.5, when you hover over doThings while holding down the control key, a pop up box gives you two options to click on:

  • Open Declaration
  • Open Implementation

There's a screenshot as the second section of the Eclipse 3.5 New & Noteworthy page for JDT:

alt text

Stephen Denne