Renaming elements

This recipe focuses on how the IDE handles the renaming of all elements of a project, being the project itself, classes, methods, variables, and packages.

How to do it...

Let's create the code to be renamed:

  1. Create a new project, this can be achieved by either clicking File and then New Project or pressing Ctrl+Shift+N.
  2. On New Project window, choose Java on the Categories side, and on the Projects side select Java Application. Then click Next.
  3. Under Name and Location : name the project as RenameElements and click Finish.

With the project created we will need to clear the RenameElements.java class of the main method and insert the following code:

package renameelements;
import java.io.File;
public class RenameElements {
private void printFiles(String string) {
File file = new File(string);
if (file.isFile()) {
System.out.println(file.getPath());
} else if (file.isDirectory()) {
for(String directory : file.list())
printFiles(string + file.separator + directory);
}
if (!file.exists())
System.out.println(string + " does not exist.");
}
}

The next step is to rename the package, so place the cursor on top of the package name, renameelements, and press Ctrl+R.

A Rename dialog pops-up with the package name. Type util under New Name and click on Refactor.

How to do it...

Our class contains several variables we can rename:

  1. Place the cursor on the top of the String parameter named string and press Ctrl+R.
  2. Type path and press Enter.

Let's rename the other variables:

  1. Rename file into filePath.

To rename methods, perform the steps below:

  1. Place the cursor on the top of the method declaration, printFiles, right-click it then select Refactor and Rename....
  2. On the Rename Method dialog, under New Name enter recursiveFilePrinting and press Refactor.

Then let's rename classes:

  1. To rename a class navigate to the Projects window and press Ctrl+R on the RenameElements.java file.
  2. On the Rename Class dialog enter FileManipulator and press Enter.

And finally renaming an entire project:

  1. Navigate to the Project window, right-click on the project name, RenamingElements, and choose Rename....
  2. Under Project Name enter FileSystem and tick Also Rename Project Folder; after that, click on Rename.
How to do it...

How it works...

Renaming a project works a bit differently from renaming a variable, since in this action NetBeans needs to rename the folder where the project is placed. The Ctrl+R shortcut is not enough in itself so NetBeans shows the Rename Project dialog. This emphasizes to the developer that something deeper is happening.

When renaming a project, NetBeans gives the developer the possibility of renaming the folder where the project is contained to the same name of the project. This is a good practice and, more often than not, is followed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.242.204