Refactoring with Inspect and Transform in the NetBeans IDE Java Editor
The NetBeans IDE Java Editor features an Inspect and Transform feature, which is a refactoring tool for running various inspections and transformations on the selected scope of files at once. It also enables a user to define a custom inspection.
In this tutorial, you will learn to perform a code review using inspections with refactoring and predefined set of inspections, manage configurations, and create custom inspections.
To complete this tutorial, you need the following software and resources.
Setting Up the Tutorial Environment
In addition to the required software listed above you should have the InspectTransform.zip file unzipped into your NetBeans project folder and opened in the NetBeans IDE as follows:
The Single Inspection option in the Inspect and Transform dialog box (Refactor > Inspect and Transform) offers a big variety of inspections related to particular refactoring transformations. The complete list of such operations is available in the Manage Inspections dialog box after clicking the Browse button to the right of the Single Inspection option.
The Search field enables you to check if a required inspection is available using a keyword lookup. For example, there are several inspections found when operator is searched for.
Every inspection is provided with a description available either in the Description field in the Manage Inspection dialog box or when an inspection is being selected in the Single Inspection drop-down list like shown in the screenshot below.
To run an inspection and apply an existing Inspection:
A set of refactorings grouped together constitute a configuration that can be run on your scope of interest, for example, a class or several classes, packages, or projects. The IDE offers the following two predefined configurations:
Convert to JDK 7
The Convert to JDK 7 configuration available in the IDE by default incorporates such refactoring operations as diamond operator usage, try-with-resources conversion, multicatch usage, and switch over Strings application.
Note: The list of refactorings in the configuration can be modified in the Inspections list of the Manage Inspections dialog box by selecting and deselecting the items under the JDK 1.5 and later node.
To run and apply the default Convert to JDK 7 configuration:
The Organize Imports configuration enables you to inspect the way import statements are organized in the code and refactor your code if desired. By default it includes a single inspection that checks whether import statements correspond to the specified code style rules.
Note: To configure the code style rules for import statements:
To run and apply the default Organize Imports configuration:
Managing Custom Inspections
A custom inspection can be created to instruct the IDE what code structures are to be found and how to transform them.
Note: To avoid adding a duplicate inspection to the IDE, choose Refactor > Inspect and Transform from the main menu, click either Manage or Browse, and, in the Manage Inspections dialog box, use the Search field to look for the required inspection prior to creating a new inspection.
To create a custom inspection:
To run the custom inspection you created:
This tutorial covers most frequent usages of the Inspect and Transform feature. Please note that with the Inspect and Transform functionality you can also perform custom refactoring at project scope, or apply particular refactoring configurations to several projects open in the IDE, etc.
For related material see the following documents: