41

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.

Lii
  • 11,553
  • 8
  • 64
  • 88
HappyEngineer
  • 4,017
  • 9
  • 46
  • 60

5 Answers5

58
  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.

Jasper
  • 2,166
  • 4
  • 30
  • 50
meriton
  • 68,356
  • 14
  • 108
  • 175
  • 1
    holy crap, I've been wondering how to do this for years, thank you so much! seems so obvious in retrospect – matt b Jan 21 '10 at 00:12
  • 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 Jan 21 '10 at 00:14
  • 1
    As @StephenDenne pointed out, when you Ctrl + hover over the name of a method of an interface, you can select either "Open Declaration" or "Open Implementation." If you choose the latter, it will jump straight to the only implementing class or (if there are multiple) open the Type Hierarchy so that you can choose which implementation class you want to jump into. – hotshot309 Nov 19 '12 at 21:20
29

Go to Window > Preferences > General > Editors > Text Editors > Hyperlinking, and uncheck Open Declaration. Preferences screenshot

From now on, when you hold Ctrl while hovering over a method name, the following popup will be displayed. Just click on the method name and the implementation will be opened (or a Types implementing XXX box, if more than one implementation exists).

Popup box

You will still be able to use the Open Declaration feature by pressing F3.

Morgan Courbet
  • 772
  • 1
  • 8
  • 18
19

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

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Stephen Denne
  • 36,219
  • 10
  • 45
  • 60
  • 1
    Excellent tip. I would like to add that you can easily bind this to a key under Preferences -> General -> Keys -> "Open Implementation". This mimics the implementors plugin which is now be obsolete IMHO. – rwojtech Apr 04 '11 at 07:45
  • 1
    This feature is called Hyperlinking. – Circuit in the wall Aug 17 '11 at 12:04
15

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
  • 2,407
  • 5
  • 20
  • 18
  • 1
    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 Jan 21 '10 at 20:11
  • 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 Jan 22 '10 at 01:09
  • @Micke Will that plugin works on latest eclipse versions like `Luna` and `Mars` – Kasun Siyambalapitiya May 17 '16 at 17:06
  • this plugin does not suport newer eclipse. Look for better solution from @morgan-courbet below. – andrej Jan 30 '19 at 17:27
  • in a more complicated classes and interfaces hierarchy it displays just the top most implementation, not the one I expect – MarianP Jul 01 '22 at 12:03
0

In Eclipse IDE for C/C++ Developers Version: Oxygen.3a Release (4.7.3a) I saw Ctrl+Tab (Toggle Source/Header) which worked for me (although not directly but its a workaround).

Toggle Source/Header

didinino
  • 93
  • 1
  • 8