javaeclipseiderefactoringeclipse-jdt

Method refactoring in Eclipse


I try to do the following refactoring steps in Eclipse IDE (JDT) and can not find the required refactoring and can not remember the name of all of the steps. I checked the refactoring at SourceMacking and do not find the correct one.

Let's take for example the following scenario:

class A {

    method(B b) {
      doSomethingWithA();
      b.doSomethingWithB();
    }

    [...]
}

class B {
    [...]
}


1) Make method static (missing name of the refactoring?):

class A {

    static method(A a, B b) {
      a.doSomethingWithA();
      b.doSomethingWithB();
    }

    [...]
}

class B {
    [...]
}


2) Move method:

class A {
    [...]
}

class B {

    static method(A a, B b) {
      a.doSomethingWithA();
      b.doSomethingWithB();
    }

    [...]
}


3) Convert to instance method:

class A {
    [...]
}

class B {

    method(A a) {
      a.doSomethingWithA();
      doSomethingWithB();
    }

    [...]
}


So anyone knowing something to do this step by step in Eclipse or do know the name of the refactoring is welcome. The goal is to have IDE support for every step.


Solution

  • Unfortunately, Eclipse's refactoring functionality is not as complete as other IDEs (for example Jetbrains' IntelliJ). I'll include instructions on how to perform each of the refactorings you requested with both IntelliJ and Eclipse.

    With IntelliJ

    1. Make Method Static
    2. Move Instance Method
    3. Convert to Instance Method

    With Eclipse

    1. Make Method Static: Eclipse doesn't directly support it, but we can achieve this using two other refactorings.

      1.1. Introduce Indirection

      Eclipse's Introduce Indirection refactoring

      Result

      public static void method(A a, B b) {
          a.method(b);
      }
      
      public void method(B b){
          doSomethingWithA();
          b.doSomethingWithB();
      }
      

      1.2. Inline

      Eclipse's Inline refactoring

      Result

      public static void method(A a, B b) {
          a.doSomethingWithA();
          b.doSomethingWithB();
      }
      
    2. Move Static Members

    3. Convert to Instance Method: Now, this is where it gets tricky. If you want to go from step 1 to step 3, you could just use Eclipse's Move Method and it'll handle everything perfectly fine. However, there are no ways that I know of to go from step 2 to step 3 using Eclipse's automated refactorings.