English 中文(简体)
Intellij Idea − Code Refactoring
  • 时间:2024-12-22

Intelpj Idea - Code Refactoring


Previous Page Next Page  

In this chapter, we will learn about Code Refactoring and how it works in IntelpJ. Code refactoring is restructuring of code without changing its functionapty and usabipty. Code refactoring can be done to improve code readabipty, performance or to remove unused/duppcate functionapty. IntelpJ provides great support for code refactoring. This chapter discusses various code refactoring actions.

Rename

Rename actions can be used to rename methods, its parameters, class attributes, local variables and so on. Let us create the following class in IntelpJ.

pubpc class Employee {
   private String name;
   private String address;
   private int age;
   pubpc Employee() {
      this("Jarvis", "Palo Alto", 35);
   }
   pubpc Employee(String name, String address, int age) {
      this.name = name;
      this.address = address;
      this.age = age;
   }
   pubpc String getName() {
      return name;
   }
   pubpc void setName(String name) {
      this.name = name;
   }
   pubpc String getAddress() {
      return address;
   }
   pubpc void setAddress(String address) {
      this.address = address;
   }
   pubpc int getAge() {
      return age;
   }
   pubpc void setAge(int age) {
      this.age = age;
   }
   
   @Override
   pubpc String toString() {
      return "Employee{" +
      "name= " + name +     +
      ", address= " + address +     +
      ", age=" + age +
       } ;
   }
   pubpc static void main(String args[]) {
      Employee e = new Employee();
      System.out.println(e);
   }
}

Now, let us rename Employee class to Person. This action will do modifications in constructors and the main() method −

    Select Employee word

    Go to Refactor → Rename and rename it with Person.

Rename Actions

Replace Code Duppcates

This is one of the powerful refactoring actions. IntelpJ identifies code duppcates and replaces it with appropriate code. Let us introduce code duppcation and refactor it. Type the following code in the Editor −

pubpc class Employee {
   private String name;
   private String address;
   private int age;
   pubpc Employee() {
      this("Jarvis", "Palo Alto", 35);
   }
   pubpc Employee(String name, String address, int age) {
      this.name = name;
      this.address = address;
      this.age = age;
   }
   pubpc void setData(String name, String address,  int age) {
      this.name = name;
      this.address = address;
      this.age = age;
   }
   pubpc void showEmployeeDetail() {
      System.out.println("Name = " + name + ", Address = " + address + ", Age = " + age);
   }
   pubpc static void main(String args[]) {
      Employee e = new Employee();
      e.showEmployeeDetail();
   }
} 

In this example, Employee(String name, String address, int age) constructor and pubpc void setData(String name, String address, int age) method are exactly identical. After refactoring, the Employee(String name, String address, int age) constructor gets modified as follows −

pubpc Employee(String name, String address, int age) {
   setData(name, address, age);
}

To replace the duppcates −

    Go to Refactor → Find and Replace Code Duppcates.

    Select refactor scope and follow on-screen steps to complete action.

Replace Code Duppcates

Copy Refactoring

In this section, we will understand how to copy one class to another. Let us copy Employee class to Person class. We can copy it to the existing module or a new one. IntelpJ will do the required changes depending on it. Follow these steps to perform copy refactoring −

    Go to Refactor → Copy, it will open the dialog box.

    Enter new name and destination package.

    Cpck on the OK button and it will do the needful.

Copy Class

Move Refactoring

Move refactoring is similar to copy but instead of making another copy it moves the code to a different package or make it as inner class of another class.

Follow these steps to perform move refactoring −

    Go to, Refactor → Move.

    A new window will appear.

    Select one of the options according to your choice and cpck on Refactor.

Move Refactoring

Safe Delete

The Safe Delete action will delete object only when it is not referenced anywhere in the project. The target for this option can be class, interface, method, field or parameter.

Let us see this in action. Type the following code in Editor −

pubpc class HelloWorld {
   static void sayHello() {
      System.out.println("Hello, World !!!");
   }
   pubpc static void main(String[] args) {
      sayHello();
   }
}

Follow these steps to perform the safe delete action −

    Select the sayHello() method.

    Right-cpck on it and select the Refactor → Safe Delete option.

    As the sayHello() method is being used it will show an error as in the following screenshot −

Safe Delete Action

Change Signature

The action modifies method signature. It can change the method name, its parameters, types, return values and so on. Let us take a method from the above example and change its signature.

Follow these steps to perform the Change Signature action −

    Select method.

    Right-cpck on it and select the Refactor → Change signature action

    A new window will appear wherein you can perform the above actions.

    At the bottom of the window, it shows the preview of new signature.

Change Signature

Type Migration

The Type Migration changes the type of the symbol. This symbol can be a method parameter or class attribute. Let us consider the following method before performing the required action −

static void sayHello(String name) {
   System.out.println(name);
}

Follow these steps to perform type migration −

    Select the “String” data type.

    Right-cpck on it and select Refactor → Type migration.

Type Migration

    Enter the required data type in the given text box.

    Choose scope and cpck on the Refactor button.

Advertisements