• No results found

Rename refactoring

In document NetBeans IDE 8 Cookbook (Page 81-84)

}

8. Press Shift + F6 to run the application.

Running the application now shows that we have two distinct books made up from two paperbacks and one e-book.

How it works…

When using the wizard to override methods, NetBeans displays a list of all the methods within the class that can be overridden. If the class is not explicitly derived from any other class, then only the base methods on java.lang.Object that can be overridden are displayed.

If the class, however, extends another class in your project, then any methods in your class hierarchy that can be overridden are also shown within the wizard.

Rename refactoring

Sometimes, when developing applications, a developer's first choice of a name for a package, class, or member, and so on, isn't the best choice. NetBeans allows developers to easily change the name of these objects using the Rename refactoring wizard. This wizard is clever enough to change both the name of the selected object and any references to it throughout the project's code base.

Getting ready

First we will need to create a new project, so please refer to the recipes in Chapter 1, Using NetBeans Projects, for creating Java projects. We will use the same project for all the refactoring recipes in this chapter. When naming the project, enter Refactoring in the Project Name field and ensure that a main class called refactoring.Refactoring is created. When the Projects explorer shows the Refactoring project, expand the Refactoring node if not yet expanded.

How to do it…

First off, we will rename the initial package that was created with the project using the following steps:

1. Expand the Source Packages node (if not yet expanded).

2. Right-click on refactoring. 3. Select Refactor and then Rename….

4. In the Rename refactoring dialog, enter com.davidsalter.cookbook.refactor in the New Name field.

5. Click on Refactor.

A progress bar will briefly be shown on the Rename refactoring dialog showing the progress of the refactoring and then the dialog will automatically close when the refactoring has completed. When the refactoring has been completed, you will note that the Source Packages node in the Projects explorer now shows that the package name has been changed to com.davidsalter.cookbook.refactor as specified in step 4. If you edit the Refactoring.java file within this project, you can see that the package name of this class has also been modified accordingly.

Now, let's add some code to the Refactoring class and then show how we can rename it with the following steps:

1. Open the Refactoring.java file for editing by double-clicking on it within the Projects explorer (if the file is not already open).

2. We are going to use this class for printing out environment variables, so

Refactoring probably isn't the best name for the class. Right-click on the class name Refactoring on the line where the class is defined (that is, on the line that starts public class Refactoring).

3. Select Refactor and then Rename….

4. On the Rename Class Refactoring dialog, enter EnvironmentPrinter in the New Name field.

5. Click on Refactor.

6. Notice how both the filename and the class name have been updated with this new name.

7. Edit the EnvironmentPrinter.java class so that it looks like this:

public class EnvironmentPrinter {

public static void main(String[] args) {

EnvironmentPrinter ep = new EnvironmentPrinter();

ep.print("JAVA_HOME");

}

public void print(String env) {

String envVariableValue = System.getenv(env);

System.out.println(envVariableValue);

} }

8. Press Shift + F6 to run the file and note that the value of the JAVA_HOME environment variable is displayed within the Output window.

The code we have written works as expected and prints out the environment variable JAVA_HOME in the Output window. We're still not entirely happy with the code, however, as the name of the parameter to the print() method should probably be a bit more descriptive. Let's change it to environmentVariableName with the following steps:

1. Right-click on the parameter name env within the line public void print(String env).

2. Select Refactor and then Rename….

3. On the Rename env dialog, enter environmentVariableName in the New Name field.

4. Click on Refactor.

The parameter name is now refactored to environmentVariableName. You can see that the use of this variable within the method has also been updated to use the new name. The new print() method should look like the following code:

public void print(String environmentVariableName) { String envVariableValue =

System.getenv(environmentVariableName);

System.out.println(envVariableValue);

}

How it works…

When refactoring, NetBeans has the ability to change variable, class, interface, and package names using a simple wizard. The power of refactoring, however, comes with the ability to change references to refactored objects as well as the original object. We saw that in the previous example, where we changed the name of a parameter in a method and saw that the references to the parameter within the method also changed.

NetBeans can not only perform Rename refactoring at a class level, but throughout an entire project. So for example, if you rename a class, every reference to the class throughout the project is also changed.

There's more...

To shortcut Rename refactoring, simply click on the object to rename (class, variable, package, and so on) within the editor window and press Ctrl + R. If the scope of the

object being renamed is local and cannot affect any other object in the project (for example, renaming a local variable), then an in-place editor will be displayed on the screen rather than the Rename refactor wizard. All the local instances of the object will be renamed as the new name is typed in.

If the scope of an object being renamed can possibly affect other parts of a project, then the Rename refactor wizard is displayed. As renaming an object of this type can have repercussions across the whole of a project, the Preview button allows the developer to see a preview of all the files that would be changed if the refactoring was performed.

This can be particularly useful when renaming affects large parts of a project.

In general, when refactoring a file (using any type of refactoring, not just Rename refactoring), it's a good practice to ensure that the entire project can be compiled before proceeding with the refactoring. If the project cannot be compiled, then unexpected results may occur when refactoring.

If a file has been modified and you start to refactor some of the contents of the file, then the file will be saved before the Rename refactoring dialog is displayed.

In document NetBeans IDE 8 Cookbook (Page 81-84)