Java Development on Eclipse, Part 2

Editor's note: In part one of this two-part series of excerpts from Eclipse, author Steve Holzner provided examples of how Eclipse makes it easier to create Java code from scratch. Continuing in that vein, in this week's concluding excerpt Steve covers creating Javadocs, refactoring, adding certain skills to your Eclipse toolbox, and customizing the development environment.

Creating Javadoc

Eclipse also makes it easy to develop Javadoc documentation, the standard Java
documentation that accompanies Java programs. You'll notice that in the code
it generates, Eclipse inserts some text for Javadoc, as you see in Ch02_05.java:

If you want to enter your own Javadoc, code assist helps you here, too; for
example, if you enter @param and invoke code assist with Ctrl+Space,
code assist will list the parameters a method takes. Typing @exception and using code assist will list the exceptions a method throws, and so on. Typing
@ in a comment and pausing will make code assist display the Javadoc
possibilities, like @author, @deprecated, and so on.

To generate Javadoc from your code, select the Project→ Generate Javadoc
item, opening the Generate Javadoc dialog, which lets you select the project
for which you want to create Javadocs. To browse a project's Javadocs, select
the Navigate→ Open External Javadoc menu item. For example, you can see
the generated Javadoc for the Ch02_05 project in Figure 2-19.

Figure 2-19. Browsing Javadoc

Refactoring

One of the major advantages of using a good Java IDE like Eclipse is that it
can let you rename and move Java elements around, and it will update all references
to those items throughout your code automatically.

Renaming Elements

For example, take a look at the code in Example 2-6. Here, we've used code
assist to create a new method to display a simple message, but we forgot to
change the default name for the method that code assist supplied.

This default name for the new method, name, is called in the main method, and it could be called from other locations in your code as well. How
can you change the name of this method and automatically update all calls to
it? Select name in the editor and then select the Refactor→ Rename menu item, opening the Rename Method dialog you see in Figure 2-20.

Figure 2-20. Refactoring a method

Enter the new name for the method, printer in this case, and click
OK. When you do, the name of this method and all references to it will be updated
throughout your code, including all code in the project, as you see here:

We've also misnamed the class in this example—Ch0206, instead
of Ch02_06. To rename the class, select Ch0206 in
the editor and select the Refactor→ Rename menu item, opening the Rename
Type dialog you see in Figure 2-21. Enter the new name, Ch02_06,
and click OK to rename the class.

Figure 2-21. Refactoring a class

Clicking OK not only changes the name of the class in the code, it even changes
the name of the class's file from Ch0206.java to Ch02_06.java,
as you can see by checking the Package Explorer. Here's the new code:

In fact, we've unaccountably managed to misname the package as well when creating
this example—org.eclipse.ch02 instead of org.eclipsebook.ch02.
When you refactor it, the name is changed both in the Package Explorer and throughout
your code:

As you can see, it's easy to rename Java elements in your code—Eclipse
will handle the details, making the changes throughout your code automatically.

TIP: If you simply type over a Java element in your code, no refactoring
happens. You've got to explicitly refactor if you want those changes to echo
throughout your code.

Moving Elements

Refactoring works automatically across files as well. Say, for example, that
you want to move the printer method to another class, Ch02_06Helper.
To see how this works, create that new class now, which Eclipse will put in
its own file, Ch02_06Helper.java. Then select the method you want to
move, printer, by selecting the word "printer" in the declaration
of this method. Next, select the Refactor→ Move to open the dialog you
see in Figure 2-22. To move this method to the Ch02_06Helper class,
enter the fully qualified name of that class, org.eclipsebook.ch02.Ch02_06Helper,
in the dialog and click OK. This moves the printer method to the
Ch02_06Helper class like this:

Extracting Interfaces

You can also extract interfaces using refactoring. To see how this works, we'll
create an interface for the Ch02_06Helper class (this class has
the printer method in it). Convert printer from a
static to a standard method by deleting the keyword static in the
method declaration. Then select the name of the class, Ch02_06Helper,
in the editor and select Refactor→ Extract Interface to open the Extract
Interface dialog you see in Figure 2-23. Select the printer method
to add that method to the interface, and then enter the name of the new interface—Ch02_06HelperInterface—and
click OK.

Figure 2-23. Extracting an interface

Clicking OK creates a new file, Ch02_06HelperInterface.java, where the interface is declared:

Besides renaming and moving elements and extracting interfaces, there are other
operations you can perform with refactoring, such as converting anonymous classes
to nested classes, changing a method's signature, and converting a local variable
to a class field. For these and other options, take a look at the items available
in the Refactor menu.