IDE tip: Go To Implementation

One of the best parts of using an IDE instead of an editor is "Go To Declaration". When you hold the Control key (or the Command key on Macs), identifiers under the mouse turn into hyperlinks and when you click on the hyperlink you jump to the declaration point of say the method call.
You can also just hover over the hyperlink, and a tooltip will pop up and show you the signature of the identifier - the fully qualified type name, method arguments, and so on.

But what if the thing you are pointing at is an interface? Or an abstract class? Good Java programming style dictates that you should prefer interfaces over implementation types, so your code will typically only know about the interface or abstract type, and when you jump to the declaration, you go to the boring interface method definition, rather the interesting bits in the implementation!

This is what the Go To Implementation feature is for. And I have a confession to make: I didn't start using this until recently. At the recent IDE tips roundup session, somebody asked whether NetBeans supported Go To Implementation. By instinct I went to Google and looked for a NetBeans plugin to do it, and found one. Unfortunately, it was old and only worked with older versions of NetBeans. Uh oh. Then on a whim I decided to look in the Navigate context menu in NetBeans - and lo and behold, it's right there!

And you don't need to use a context menu to access this feature. The trick is to use a second modifier key to the normal ctrl-click on the identifier! In addition to the control key (or command on Macs), also hold the Alt key (Option on Macs).

Here's how it works. First, let's say you want to go to the actionPerformed implementation. You ctrl-click the identifier:

...and that takes you into the abstract method declaration. (In this specific case, the method is in a read-only file inside a .jar file, which is why NetBeans marks the whole file in gray)

That's not what we want. Go back to the previous editor location (Ctrl-left.) This time, hold both the Control and Alt keys (or Command and Option on Macs) when you click. When you do that you get this dialog:

This dialog has focus and you can use the arrow keys to navigate and press enter -- or just click with the mouse. As soon as you pick a specific override of this method, you jump to it:

That's all there is to it. It's doubly embarrassing that I didn't remember this feature, since I should know the implementation of it very well. You see, I shamelessly lifted most of the implementation of it for the language-infrastructure work I did to support Go To Declaration in Ruby, JavaScript etc:

P.S. I've updated the wiki page with a clarification that it's built in now.

P.S.2: Until recent builds, Go To Implementation in NetBeans only worked for interfaces, not methods extending abstract classes. So use 6.9.


command-t in eclipse :)

I just discovered this recently also, and it's enormously useful.

Posted by Jeremy on April 28, 2010 at 02:37 PM PDT #

Yes, though Command-T shows the type hierarchy which will include not only stuff in your own projects but all the other types in the classpath as well; so in the above example if you try to go to one of your actionPerformed() methods you'll see (in the Cmd-T dialog) hundreds of matches from Swing code etc. Of course if you're trying to navigate into platform code that's what you want... but I usually use goto-declaration and goto-implementation to jump around my own code. Maybe this is configurable somewhere?

Posted by Tor Norbye on April 28, 2010 at 03:32 PM PDT #

Also notice the green circle/square with 'I' on the left side with line numbers - it provides the same functionality - just click on it with mouse.

Posted by Jiri Pejchal on April 28, 2010 at 04:54 PM PDT #

Go back on Windows is Alt-Left not Ctrl-Left.

P.S. A you still working on Python module ;)

Posted by Pet on April 28, 2010 at 05:13 PM PDT #

In Eclipse right click the method and select Open Interface/Open Implementation. I think you can configure shortcut too. There is one thing that bugs me though, the Select Implementations dialog is so narrow, I have to scroll it every time.
BTW, it also works on class hierarchy. It is essential go up/go down command.

Posted by jan_bar on April 28, 2010 at 09:11 PM PDT #

@Tor, re: "Command-T shows the type hierarchy which will include not only stuff in your own projects but all the other types in the classpath as well"

Ah, I hadn't been aware of that difference. I actually use this mostly for navigating through code in 3rd party jars, and typically interfaces with not so many implementations.

Posted by Jeremy on April 29, 2010 at 12:55 AM PDT #

What version of NetBeans are you using that has Go To Implementation? I've been asking for a solid implementation of that for years now because I use it constantly in IntelliJ IDEA and every time they add one, they almost instantly remove it again. I just installed 6.8 (full J2EE edition) and don't see that under the Navigate menu nor as an available plug-in.

Posted by Scott Wells on April 30, 2010 at 05:02 AM PDT #

Netbeans 6.9 beta has this feature.

Posted by Jiri Pejchal on April 30, 2010 at 05:12 AM PDT #

I like this feature, its really helpful :)

Posted by Muhammad Ghazali on April 30, 2010 at 12:00 PM PDT #

Only works on 6.9 but works beautifully =)

Posted by Paulo "JCranky" Siqueira on May 21, 2010 at 02:24 AM PDT #

Post a Comment:
Comments are closed for this entry.

Tor Norbye


« July 2016