A friend and technical lead on a project I was on once called it the cave-exploring term "spelunking," but he used it to mean searching through directories in a large set of files for a particular class file. With large Java projects, it's natural that as more interfaces and classes are added, the more time it can take to navigate the code base looking for a file. Combing through a large number of these files and directories in the Package Explorer can feel like exploring the deep abysses of a cave.
If you're still spelunking through packages or directories to find your Java methods or classes, consider these tips for navigating through large projects. Challenge yourself to avoid using the Package Explorer and use the tips here, most of which only require minimal movements of the mouse and a couple clicks on the keyboard.
In versions of Eclipse prior to Galileo, one way to quickly open a file to view its contents was to right-click a method or class, then click Open Declaration in the context menu to open the Java element (method, interface, or class) in the Java editor. In addition to using the context menu, you could press the Ctrl key and hover over the element in the Java editor. As your mouse hovered over the element, it would turn into a link you could click. Clicking the link took you to the declaration in the Java editor.
New in Galileo is a drop-down list that displays the options for opening a declaration. Clicking Open Declaration opens the selected element in the Java editor (see Figure 1).
Figure 1. Opening a declaration
The only problem with using Open Declaration has been with using interfaces. If you're using interfaces, the command opens the declaration of the element — which is an interface. That might be slightly annoying if what you're really trying to do is open the implementation of the selected element to see the Java code that will actually execute when the project runs. This problem has been resolved in a new command feature in Galileo: Open Implementation.
To invoke Open Implementation, press Ctrl while hovering over a Java element in the Java editor, just as if you would have to use the hyperlinking to open a declaration. The drop-down list appears, and you can click Open Implementation.
Figure 2. Opening an implementation
Using the code shown above, the editor opens with the actual class implementation of the element.
Figure 3. Code shown in the editor
If you use Javadoc (see Resources) to document your
elements, you can use the same Ctrl+click technique to open elements you've
referenced in your Javadoc using the
@link keywords (see Figure 4). When
you click the hyperlink, Eclipse opens the specified type in the Java editor.
Figure 4. Using Ctrl+click in Javadoc comment
In Galileo, a new feature is the ability to view documentation for the
elements defined by
@link. The Javadoc preview and Javadoc view display
the elements as hyperlinks. If you click the hyperlinks, the Javadoc for the clicked-on
element appears. You can cut down on opening files in new Java editor views if all
you're going to do is read the Javadoc for the element.
Figure 5. Hyperlinks in the Javadoc views for quick reading
Viewing the Quick Outline
In large class files with many methods, pressing Ctrl+O to open the Quick Outline helps you find a method or field quickly. After pressing Ctrl+O, start typing the name of the method you want to find. As you type, the list filters itself to display only the matching elements.
Figure 6. Using the Quick Outline
When you select the element from the list, Eclipse navigates to the element in the current Java editor for you.
Viewing the Quick Type Hierarchy
If you have a class file open in the Java editor and would like to open Java types in the type hierarchy, press Ctrl+T (or click Navigate > Quick Type Hierarchy) to display the type hierarchy.
Figure 7. Using the Quick Type Hierarchy
The hierarchy displays the subtypes for the selected element. For example, if an interface is selected when you invoke the Quick Type Hierarchy, the list displays all the known classes that implement the interface.
Press Ctrl+T again, and the Quick Type Hierarchy displays the supertypes in the list (see Figure 8). The list now contains any interfaces that the selected element implements or classesit extends.
Figure 8. Using the Quick Type Hierarchy to find supertypes
Choose a type from the list in the Quick Type Hierarchy, and Eclipse opens a new Java editor with the selected type.
If you know the name of the Java class you want to view in the editor, the easiest way to find and open it is to press Ctrl+Shift+T (or click Navigate > Open Type) to show the Open Type window. When this window appears, start typing, and Eclipse shows you the list of matching types it can find.
Figure 9. Using Open Type
Aside from typing the full class names, you can use two tricks to find your results even
faster. The first is to use the
wildcards to match any or a single character, respectively. For instance, to find the
AutomobileFormatter class, type
Figure 10. Using wildcards in Open Type
The second tip is to use abbreviations for the class. The abbreviations are based on the
uppercase letters of the type names. To find
AF. That still might not narrow it down very much,
however, so type
AuFo. Select a type from the list in the
Open Type window, then click OK. Eclipse opens the selected type in
the Java editor.
Sometimes, the files you want to open aren't Java elements but, rather, properties files or XML files. Instead of using Open Types, use Open Resources by pressing Ctrl+Shift+R (or clicking Navigate > Open Resource). Similar to Open Type, you can use wildcard characters or abbreviations to quickly narrow down the list of resources. Like Open Type, click OK after you select your resource from the list, and Eclipse opens the selected resource.
Leaving bread crumbs
Although using Java editor bread crumbs is similar to using the Package Explorer, the presentation of the methods, classes, packages, and other project resources is displayed in a much more navigable fashion in the Java editor's bread crumbs.
To turn the bread crumbs on, click the button shown below.
Figure 11. Java editor bread crumbs toggle button
When turned on, the bread crumbs look like those in Figure 12. As you select different elements in the editor, the items in the bread crumbs change to show the currently selected element and its hierarchy in the project.
Figure 12. The Java editor bread crumbs
You can click the bread crumb and view other elements at the same level in the hierarchy.
Figure 13. Opening elements at the same level in the hierarchy
Eclipse offers features that help you navigate large code bases quickly and efficiently, without the need to go "spelunking" through code files and directories. Using the features in the Eclipse IDE allows you to focus your time and energy on implementing code and not getting tied up trying to find classes and methods.
- Learn more about Javadoc at the Javadoc Tool site.
- Read "An Eclipse Galileo flyby" to learn about more new Galileo features.
- Check out the "Recommended Eclipse reading list."
- Browse all the Eclipse content on developerWorks.
- Follow developerWorks on Twitter.
- New to Eclipse? Read the developerWorks article "Get started with the Eclipse Platform" to learn its origin and architecture, and how to extend Eclipse with plug-ins.
- Expand your Eclipse skills by checking out IBM developerWorks' Eclipse project resources.
- To listen to interesting interviews and discussions for software developers, check out check out developerWorks podcasts.
- Stay current with developerWorks' Technical events and webcasts.
- Watch and learn about IBM and open source technologies and product functions with the no-cost developerWorks On demand demos.
- Check out upcoming conferences, trade shows, webcasts, and other Events around the world that are of interest to IBM open source developers.
- Visit the developerWorks Open source zone for extensive how-to information, tools, and project updates to help you develop with open source technologies and use them with IBM's products.
Get products and technologies
- Eclipse Galileo: Download the latest IDE from Eclipse.
- Check out the latest Eclipse technology downloads at IBM alphaWorks.
- Download Eclipse Platform and other projects from the Eclipse Foundation.
- Download IBM product evaluation versions or explore the online trials in the IBM SOA Sandbox and get your hands on application development tools and middleware products from DB2®, Lotus®, Rational®, Tivoli®, and WebSphere®.
- Innovate your next open source development project with IBM trial software, available for download or on DVD.
- The Eclipse Platform newsgroups should be your first stop to discuss questions regarding Eclipse. (Selecting this will launch your default Usenet news reader application and open eclipse.platform.)
- The Eclipse newsgroups has many resources for people interested in using and extending Eclipse.
- Participate in developerWorks blogs and get involved in the developerWorks community.
Dig deeper into Open source on developerWorks
Get samples, articles, product docs, and community resources to help build, deploy, and manage your cloud apps.
Experiment with new directions in software development.
Software development in the cloud. Register today to create a project.
Evaluate IBM software and solutions, and transform challenges into opportunities.