Using Java Development Tools in Eclipse
Eclipse provides a first-class set of Java Development Tools (JDT) for developing Java code. These tools include a Java perspective, a Java debug perspective, a Java project definition, editors, views, wizards, refactoring tools, a Java builder (compiler), a scrapbook for evaluating Java expressions, search tools, and many others that make writing Java code quick, fun, and productive.
The JDT views and editors provide an efficient and effective way to quickly and intuitively navigate through Java source code and view Javadoc. The Java editor supports syntax highlighting, content assist, error cluing, state of the art refactoring, type-ahead assistance, and code generation among a host of other capabilities. The Java compiler is an incremental compiler; it compiles only what it must based on your changes. It supports JDK 1.3 and 1.4. JDT can be configured with different JREs. You can develop code with one JRE and debug with another.
The Java editor is more than a source code editor. It builds and maintains indexed information about your code, enabling you to quickly search for references to classes, methods, and packages. The incremental compiler is constantly running in the background and will alert you to potential errors in your code before you save it. In many cases JDT will propose several solutions to the problems it detects, such as adding an import statement that was omitted, suggesting typographical corrections, or even creating a new class or interface. This allows you to focus on your code and not be distracted by compiler demands.
In the following sections we're going to look in more detail at the capabilities and use of the Java tools. We'll start with an overview of the JDT user interface and the fundamentals you'll need for creating and navigating Java resources. We'll then look in much more detail at JDT capabilities for coding Java. Then we'll see how to run Java code. In the final sections of this chapter we'll get into more detail on working with Java elements, tuning the performance of JDT, and specific functions in the JDT views and perspectives.
Getting Started
Let's go. We'll start with a quick overview of the JDT user interface. We'll then cover the fundamentals, like opening a class, navigating to a method or field, and running a Java program. We'll also discuss how to search Java code.
Overview of the JDT User Interface
The Java perspective is the default perspective for Java development and the one that comes up when you create a new Java project (see Figure 3.1).
Figure 3.1 Java Perspective
The left pane contains the Package Explorer view and Hierarchy view. The Package Explorer view does for the Java perspective what the Navigator view does for the Resource perspective. Use the Package Explorer view to navigate in your Java projects, perform operations on resources, and open files for editing. The middle pane contains open editors, Java and other- wise. The active editor is the one on top. In Figure 3.1, an editor has PrimeNumberGenerator.java open in it. The right pane is the Outline view, which presents a structured, hierarchical view of the contents of the active editor. On the bottom right is the Tasks view, the same one we saw in the section "Working with Tasks" in Chapter 2. As you navigate in the user interface, selecting Java files for editing and selecting Java elements, all the views and the editor stay synchronized with your actions.
The Java views display Java elements with icons for a package and for public methods. Some of these icons are decorated to provide further information with overlays, such as to indicate a class has a main method or to indicate a method overrides a method in a superclass. For a complete list of the JDT icons and decorations, refer to the "Icons" section in the "Reference" section in the Java Development User Guide.
You have two options for how code appears in the editor as you navigate in the user interface. Show Source of Selected Element Only on the toolbar controls this. The default is to show the contents of the entire file. If you prefer to focus on smaller portions of code, toggling this option will only show the source for the selected class, method, field, or import statement. This is mostly a matter of personal preference.
The Fundamentals
Here are the fundamental tasks you need to understand to create and navigate through Java resources. These are available when you are in one of the Java perspectives.
Creating a Java Project
All Java elements must exist in a Java project. To create a Java project, select File > New > Project... > Java Project from the menu or select Create a Java Project from the toolbar.
When creating Java projects and other elements, if you get the names wrong or later decide you want to change them, the JDT refactoring capabilities make it easy to rename elements and update references to them (refactoring will be discussed in more detail later in this chapter).
Creating a Package
Java types, that is, classes and interfaces, must exist in a package. If you do not create one, a default will be created for you. To create a package, select the containing project and select File > New > Package from the menu or select Create a Java Package from the toolbar.
Creating a Type
To create a type, select the containing project or package and select File > New > Class or File > New > Interface from the menu or select Create a Java Class or Create a Java Interface from the toolbar.
Opening a Type
To open a Java class or interface, do one of the following.
Double-click on a Java source file, class, or interface, or select one of these in a view and press F3 or Enter. You can do this from any Java view.
From the editor, select the name of a class or interface in the source code (or simply position the insertion cursor in the name), and then select Open Declaration from the context menu or press F3.
Select Ctrl+Shift+T and enter the name of a class or interface in the Open Type dialog.
Opening a Method or Field
To open a method or field definition, do one of the following.
Double-click on a method or field, or select a method and press F3 or Enter to see its definition. You can do this from any Java view.
From the editor, select the name of a method or field in the source code (or simply position the insertion cursor in the name) and then select Open Declaration from the context menu or press F3 to see its definition.
Viewing Supertypes and Subtypes
To view the supertypes or subtypes for a class or interface in the Hierarchy view, do one of the following.
Select a Java element, such as a Java source file, class, method, or field, and then select Open Type Hierarchy from the context menu or press F4. You can do this from any Java view.
From the editor, select the name of a Java element in the source code (or simply position the insertion cursor in the name) and then select Open Type Hierarchy from the context menu or press F4.
Select Ctrl+Shift+H and enter the name of a class or interface in the Open Type in Hierarchy dialog.
Navigating to a Type, Method, or Field
You can navigate to class, interface, method, and field definitions in your Java code simply by selecting one of these elements in a view, such as the Outline or Type Hierarchy view. The editor and open views scroll to your selection.
NOTE
If you select an element and you do not see it in an editor, it means the file containing the definition is not open. You must then first open the file using one of the methods described above.
Locating Elements in Projects
As you use the views and editor to navigate in your code, you can locate element definitions in projects by selecting a Java element in one of the views or the editor (or positioning the insertion cursor in the element name) and then selecting Show in Package Explorer from the context menu. The Package Explorer scrolls to the selected element. The element must be defined in an open project in your workspace.
Running a Java Program
To run a Java program, select a class with a main method and then select Run > Run As > Java Application from the menu. Output is shown in the Console view.
Searching
There are two types of searches: a general Eclipse file search for text strings, and a Java-specific search of your workspace for Java element references. The search capability uses an index of Java code in your workspace that is kept up-to-date in the background, independent of Java builds. This means that you don't have to have the auto-build preference selected or save your modifications in order to do a search.
Searching a File
To search the file in the active editor for any text, select Edit > Find/Replace, or press Ctrl+F. Eclipse also has an "incremental find" feature that provides a keystroke-efficient way to do this. Select Edit > Incremental Find from the menu or press Ctrl+J and note the prompt in the message area to the left on the bottom margin (see Figure 3.2). Start typing the text you're searching for. The message area displays the text you're searching for and the first match is selected in the editor. Press Ctrl+K to find the next occurrence of the text.
Figure 3.2 Incremental Find
Searching Your Workspace
Select Search or press Ctrl+H and then select the Java page to search your entire workspace, or a subset of it, for references to Java elements. There are three aspects to a Java search: what kind of reference, what kind of Java element, and within what scope. You can specify these by using Limit To, Search For, and Scope, respectively, in the Search dialog, as shown in Figure 3.3. To restrict a search, first select one or more Java elements in the Package Explorer, Outline, or Type Hierarchy view and then select Search, or define a Working Set and then specify that working set under Scope. (For more information on Working Sets, refer to the section "Working Sets" in Chapter 2.) For example, if you want to search for methods returning void in your projects, select the projects in the Package Explorer view, select Search, specify the Search string "* void", select Search For Method and Limit To Declarations, select Selected Resources, and then select the Search button (or press Enter).
Figure 3.3 Searching for Java Elements
Java search results are shown in the Search view (see Figure 3.4). Matches are indicated in the editor with entries on the marker bar. Navigate to matches from the Search view by double-clicking an entry or by selecting Show Next Match and Show Previous Match.
Figure 3.4 Search View
In all of the JDT views, including the Search view and the editor, you can select an entry or Java element and then search on that element from the context menu by selecting References > or Declarations >. This ability to successively search for Java elements from the views, especially the Type Hierarchy and Search views, provides a simple, efficient way to explore and understand Java code.