Over 70 highly focused practical recipes to maximize your output with NetBeans
Be warned that many of the refactoring techniques presented in this article might break some code. NetBeans, and other IDEs for that matter too, make it easier to revert changes but of course be wary of things going wrong.
With that in mind, let's dig in.
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.
Let's create the code to be renamed:
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.
Our class contains several variables we can rename:
Let's rename the other variables:
To rename methods, perform the steps below:
Then let's rename classes:
And finally renaming an entire project:
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.
NetBeans enables the developer to easily move classes around different projects and packages.
No more breaking compatibility when moving those classes around, since all are seamlessly handled by the IDE.
For this recipe we will need a Java project and a Java class so we can exemplify how moving elements really work.
The exisiting code, created in the previous recipe, is going to be enough. Also you can try doing this with your own code since moving classes are not such a complicated step that can't be undone.
Let's create a project:
When clicking the Refactor button the class is removed from the current project and placed in the project that was selected from the dialog.
The package in that class is then updated to match.
Extracting superclasses enables NetBeans to add different levels of hierarchy even after the code is written.
Usually, requirements changing in the middle of development, and rewriting classes to support inheritance would quite complicated and time-consuming.
NetBeans enables the developer to create those superclasses in a few clicks and, by understanding how this mechanism works, even creates superclasses that extend other superclasses.
We will need to create a Project based on the Getting Ready section of the previous recipe, since it is very similar.
The only change from the previous recipe is that this recipe's project name will be SuperClassExtraction.
After project creation:
Replace the entire content of the DataAnalyzer.java with the following code:
package superclassextraction;
import java.util.ArrayList;
public class DataAnalyzer {
ArrayList<String> data;
static final boolean CORRECT = true;
static final boolean INCORRECT = false;
private void fetchData() {
//code
}
void saveData() {
}
public boolean parseData() {
return CORRECT;
}
public String analyzeData(ArrayList<String> data, int offset) {
//code
return "";
}
}
Now let's extract our superclass.
When the Refactor button is pressed, NetBeans copies the marked methods from DataAnalyzer.java and re-creates them in the superclass.
NetBeans deals intelligently with methods marked as abstract. The abstract methods are moved up in the hierarchy and the implementation is left in the concrete class. In our example analyzeData is moved to the abstract class but marked as abstract; the real implementation is then left in DataAnalyzer.
NetBeans also supports the moving of fields, in our case the CORRECT and INCORRECT fields.
The following is the code in DataAnalyzer.java:
public class DataAnalyzer extends Analyzer {
public void saveData() {
//code
}
public String analyzeData(ArrayList<String> data, int offset) {
//code
return "";
}
}
The following is the code in Analyzer.java:
public abstract class Analyzer {
static final boolean CORRECT = true;
static final boolean INCORRECT = false;
ArrayList<String> data;
public Analyzer() {
}
public abstract String analyzeData(ArrayList<String> data, int
offset);
public void fetchData() {
//code
}
public boolean parseData() {
//code
return DataAnalyzer.CORRECT;
}
}
Let's learn how to implement parent class methods.
Let's add a method to the parent class:
public void clearData(){
data.clear();
}
@Override
public void clearData() {
super.clearData();
}