Pramati Technologies

Refactoring Java Code in Studio

Overview

Refactoring is the process of improving the design of code without changing the external behavior. It improves the design of a software or code, prevents decay of the program, improves readability, reduces bugs, makes it easier to spot bugs, speeds up code development and increases productivity.

Refactoring can be used to add a new feature or function to a code, to fix bugs or during code review. For more information on refactoring, visit www.refactoring.com.

This document describes the following Java refactoring available in Studio:

Refactoring framework in Studio

In Studio, files in the source tree are parsed prior to refactoring. This takes time based on the size of source. The selection is analyzed and it is ensured that refactoring is valid. Refactoring is performed for a valid selection. A backup copy of all affected files is made and an error message is displayed for an invalid selection. Studio provides error recovery options.

Refactoring in Studio has been designed to make the process easy. Refactoring is done by placing the cursor at the desired position. This is followed by analysis of the impact of refactoring, and manipulation of Abstract Syntax Tree(AST). An AST is a data structure representing parsed data. It can be used as an internal representation of a program being optimized and from which code is generated. The code is changed via manipulation of AST. Studying AST often helps in debugging and writing new refactoring. When refactoring is implemented, the manipulation functions contained in Studio should be used. Studio is aware of files that have been changed in AST.

Studio provides a preview option. When this option is enabled, all usages of the selected package, class, method, field, variable, interface or superclass to be refactored are in the output panel. The usages are grouped based on method invocation or declaration. User can include or exclude any usage. User must be careful while renaming an interface or overridden methods.

Renaming elements in the code

Rename refactoring in Studio enable you to rename a package, class, method, field, and local variable in the Java code. All references are updated, automatically. The following usages are renamed:

To Rename a package, class, method, field or a variable:

    1. In Editor, place the cursor at the element to be renamed. For our example, let us rename a package TopicSess.
  1. Select Refactor > Rename from the menu bar or right click on the concerned Java file and select Refactor > Rename or click Shift + F8.

  2. Type a more comprehensible name for example, TopicSession.
  3. Select Preview usages to view all usages of TopicSession in the Preview tab in output panel; Select Search in comments and literals option in the Rename dialog to correct the usages of the refactored member in all comments and strings.
  4. To continue, click Run in the preview panel toolbar. To cancel, click Stop.
Changing method signatures

Change Method Signature (CMS) enables you to change a method name, add a parameter, remove a parameter, reorder parameters, change a return type, change a parameter type, and change a parameter name.
CMS is applicable to constructors as well but their name or return type can not be changed. It searches for all usages of the method. All calls, implementations and overriding of the method are modified accordingly to reflect the change. If a parameter is added, it should be supplied with a default value to be used for method calls. To change a method signature:

    1. In Editor, place the cursor at a method to be refactored. For our example let us change method signature for credit().
  1. Select Rafactor > Change Method Signature from menu bar or as a right click option, or press Ctrl + F8.

    Table 1:
    Attribute
    Description
    Method Name
    Present name of the selected method is displayed
    Visibility
    Enables the user to select the method type such as Package, Private, Protected or Public
    Return Type
    Enables the user to select the return type for the method
    Throws Exception
    Enables the user to specify a particular exception to be detected
    Method Parameters
    Enables the user to add or remove parameters or change their order
Note: Parameters that are specified in the panel but do not exist in the selected file appear in red. Press enter to view details of the error.
  1. Select Preview option to view all occurrences of credit() in the output panel.
  2. To continue, select Run from the output panel tool bar. To cancel, select Stop.

When method signature can not be changed

A message appears if the method signature cannot be changed. A solution for the problem is also mentioned.

Extracting a superclass from an existing class

E xtract Superclass refactoring enables you to create a superclass based on an existing class. Fields and methods in the original class can be moved to the superclass. For a method, only the method declaration can be transferred. The implementation declaring the method as abstract in the superclass is non-transferable. The superclass and the original class are inherited from the superclass itself. Extract Superclass option in the main menu is disabled if cursor is placed at an invalid location.

    1. In Editor, place the cursor at the class to be extracted. For our example, let us extract the class CheckingBean.
  1. Select Refactor > Extract Superclass from menu bar. Enter the details in the panel.
    Table 2:
    Attribute
    Description
    Extract From
    Displays the concerned package.filename
    Superclass Name
    Name of the superclass
    Select Members
    Select class members to be included in the superclass. Abstract option enables transfer of the method declaration and the its implementation in the class.
    View Dependencies only
    Enabling this option displays the selected members and their dependents only. This option is enabled only if one of the listed members is selected
    Create JavaDoc
    Creates a copy, or moves existing Javadoc; Generates stubs with all necessary tags and attributes. Selecting None does not create any Javadocs for the extracted class


Note: Selecting a member highlights other dependent members that are mandatory in the extracted class.
  1. Enter superclass name as TestBean; select members. Click OK to proceed. The option `View dependencies only' is enabled after a member is selected from the list. You can choose to view only those members that depend on the selection.
  2. Select Copy, None, or Move options to create Javadocs in the extracted class.

TestBean.java is created in Studio with all necessary details.

Extracting an interface based on methods of a class

Extract Super Interface refactoring enables the user to create an interface based on the methods of a class. Static final fields declared in the initial class can be moved to the interface. This creates an interface containing the selected methods and fields. The specified class methods implement the corresponding interface methods.

    1. In the editor, place the cursor at the class name or within the class.
  1. Select Refactor > Extract Interface from the menu bar.
  2. Enter details in the following panel:
    Table 3:
    Attribute
    Description
    Extract From
    Displays the package and filename as package.filename
    Interface Name
    Name of the extracted interface
    Select Members
    Select class members to be included in the superclass.
    Create JavaDoc
    Generates a Javadoc stub with all necessary tags and attributes


Note: All the members are selected by default. the user can uncheck some members as per requirement.
Moving properties of an anonymous class to an inner class

In Studio, an anonymous class can be converted to a named inner class. If this class accesses any local variable, a constructor is created for the inner class and the values of the local variable are passed on. If the anonymous class is within a non-static method, a reference can be passed to the outer class of the constructor. Large anonymous classes can be simplified using this refactoring to make the code more readable and share selected inner class functionalities.

For example, to convert an anonymous class Runnable to an inner class Job:

    1. Place the cursor within the inner class.
Note: A warning appears if the cursor is placed at a wrong position.
  1. Select Refactor > Convert Anonymous To Inner from the right click menu or from the menu bar.
  2. In the panel, type the inner class name.

    For this example, type Job. Click OK.
Note: Unselect Make class static option to obtain a non-static inner class. Select the type of inner class as Default, Private, Protected, or Public. If constructors for the inner class are required, provide package details in the Constructor Parameters field.
Declaring method, variable, field, and constructors

Any undeclared variable for a method, field, or constructor is declared at the specified position. Studio provides:

Place the cursor at the undeclared variable and press Alt + Enter.

Limitations of refactoring

Refactoring has the following limitations:

Note: Design changes and mistakes are difficult to refactor. This area is still being worked upon.

Pramati Technologies  © Copyright   TOCPREVNEXTINDEX