The term refactoring simply means changing the structure of your source code. For example, you may decide to change the name of a class, a method, or a field inside one of your source files. Or perhaps you need to move a class from one package to another. Or you can add a parameter to a frequently called method and use a default value wherever it is invoked throughout your source. In short, refactoring is generally about making source better, including
Depending on the tools you use, refactoring can be either time-consuming or quick and easy. Fortunately, NetBeans comes with a solid amount of refactoring support built in. This article aims to teach you about some of the more common refactoring options, as well as to show you ways to use these options in everyday situations with your Java technology source code. NetBeans presently supports four primary refactoring approaches:
You can select these options from the Refactoring menu at the top of the NetBeans IDE, from context-sensitive menus inside the editor, or by automatically invoking them from selective actions inside the Projects window. Additional refactoring support in NetBeans is also in the works. You can view a listing at http://refactoring.netbeans.org/. Here are some refactoring features coming in future versions of NetBeans.
Some Sample Source Code
Before we go any further, let's start off with some source code that's ripe for refactoring. You can download this source code using the link at the end of the article. First, for
And here is the source code for
After creating these classes in NetBeans 4.1 using the template for a standard Java application, you should have a project that looks like Figure 1.
At this point, let's say that you've inherited this source code after the previous programmer left the company and that you need to add more functionality (for example, classes that subtract, multiply, and divide). Clearly, we need to get this source ready to handle enhancements. Simple Refactoring: Renaming a Package
With NetBeans 4.1, you can use the built-in refactoring tools to change the name of a field, a method, a class, or even a package. This change will then propagate throughout the source tree to all the classes that reference that particular item, instead of forcing you to hunt down references by yourself or through compilation errors. Each of these tools is listed under the Refactor menu at the top of the NetBeans IDE. Note: Don't confuse this with the RefactorIT menu, if you have it, which is a separate plug-in. For example, let's say that we don't like our source classes to be in the
Note that the check box for Preview All Changes in the lower-left corner of the dialog box is selected. Leave it selected, and press Next. At this point, the preview of the refactoring tasks is displayed in the bottom output window of the IDE, as shown in Figure 3.
You can review the refactoring information presented in the output window and select and clear various changes that the IDE proposes to make. If the changes look good, press the Do Refactoring button to save the modifications. Figure 4 shows the saved changes. Note that not only has the package been changed in the Projects window in the upper left, but the package declaration at the top of the source code has been changed from
Renaming Fields
Source code can often have inappropriate or misleading field names, especially after the code has been updated for new functionality. Renaming fields can be particularly helpful in keeping your source code legible. At this point, you may be wondering: "Why not just use the Replace All button in a text Replace dialog to change all the field names?" Here are a couple of reasons:
Renaming fields in NetBeans is easy. In this example, let's change the name of the awkward
Again, you can preview the changes as they appear. Note that one of changes that NetBeans suggests is in
You can rename methods as well by placing the insertion beam over the line containing the definition of the method and choosing Rename from the Refactor menu. Next, let's change the name of the class from
Encapsulating Fields
You can change the encapsulation of fields inside an object by choosing the Encapsulate Field menu item from the Refactoring menu. (Other IDEs sometimes call this generating getters and setters.) In this case, we don't want the result field to be public. Let's change the field from public to private and create a
First, clear the check boxes for the
Note that in Figure 9, NetBeans 4.1 wants to create public accessors,
You can also use the Note that we didn't select the option to Finally, note that the
Changing Method Parameters
With NetBeans, you can also change method parameters, again with the effect of having those changes propagate throughout the source code. This can be a little more involved, however. Obviously, you will need to add code inside of the method so that it can take advantage of the modified parameters, but you also have the ability to specify a default value for any source that calls that method, both inside and outside of the target source file. In this case, let's change the
In
Note that if we had explicitly called Moving Classes
Finally, we can move classes from one location to another using the NetBeans refactoring tools. In this case, let's create a new package called
Note that because Source Code
You can download the sample source code for this article here. For More Information
Books
Web Sites
JavaOne
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|
| ||||||||||||