Instantly show a class/file in the Package/Project Explorer in Eclipse

While working on a class or XML file, you’ll often find that you want to work with the actual .class or .xml file in the Package Explorer, Project Explorer or Navigator views. You may want to copy, move or delete the file or do stuff that are only accessible in these views (eg. browse all files in the same folder).

Eclipse has a feature called Show In, that’s almost like a Quick Open. It instantly navigates to the current editor’s file in a requested view and positions you directly on the file in that view, no matter how deep in the hierarchy the file is. It’s also keyboard friendly, which means no more reaching for the mouse.

This feature is perfect for those (like me) who don’t like the Link with Editor functionality on certain views. I don’t like the Package Explorer to Link with Editor as I like to know things are the way I left them (see the rant at the end of the post).

Continue reading

Split and view the same editor side by side in Eclipse

At some point you’ll want to view the same class or file side by side. You may want to follow related code in different parts of the class or need some code constantly available as reference to change some other part of the class.

Eclipse allows you to split an editor and move it to anywhere in the editor area in the same window, including next to the original editor. Changes made in the one editor are reflected in the other. The feature isn’t very obviously named, but it is easy to use.

Continue reading

Jump to the start and end of methods, loops, blocks and XML tags

Common wisdom says that we should keep our methods small and avoid nested if statements and nested loops. But I’ve seen some huge methods (1000’s of lines) and code that included an if in a while in a for loop in an if.

The problem is finding out where the block starts and where it finishes (ie. where it’s opening and closing brackets are). Also, most frameworks require XML configuration, leading to big XML files that become hard to navigate, eg. moving between tags.

But, once again, Eclipse makes it easy to jump between the opening and closing brackets/tags using a keystroke.

Continue reading

Find all classes that override/reimplement a method

Inheritance is a great feature of OO. It can make browsing and navigating code a lot more difficult though, especially in a class hierarchy where some classes override a method along the hierarchy but others don’t.

Here’s an example. Your task is to find all the classes that implement/override the method connect().

public interface DataSource {
 public void connect();
 public void update();
 public void disconnect();
}

public abstract class DBDataSource implements DataSource {
 public void connect() {
 // ... Connect to DB
 }

 public void disconnect() {
 // ... Disconnect from DB
 }
}

public class AccountDataSource extends DBDataSource {
 public void update() {
 // ... Update account
 }
}

public class ClientDatasource extends DBDataSource {
 public void connect() {
 // ... Special code to connect to client DB
 }

 public void update() {
 }
}

In this case DBDataSource implements the method, ClientDataSource overrides it but AccountDataSource doesn’t. Finding this out manually is slow and time consuming because the classes are in different files.

There are two lesser known features of Eclipse that can you help. The first one is an unknown feature of the Type Hierarchy called Lock View and Show Members and the second one is Quick Type Hierarchy which is my favourite and which I use all the time.

Continue reading