简体   繁体   中英

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.

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的引入间接重构

    Result

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

    1.2. Inline

    Eclipse的内联重构

    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.

After having learned the refactoring is called 'Convert to Instance Method' I searched the bug database of Eclipse JDT and I found bad news:

Bug 10605 Bug 118032 Bug 338449

So basically it is a Won't-Fix noone cares feature request and so it might be time that I also switch to IntelliJ. I have to contemplate about this... .

Emond Papegaaij suggested in the discussion of Bug 118032 a work around:

A simple workaround is to create the static method, call this static method from the method you want to become static and inline the method call. This works for me in 4.3.1.

This is interesting but again would not be an automatic refactoring and defeat the purpose of refactoring in the first place. Adding someone's own code introduce the chance of failure and requires the rerun of the test-suite resulting in no chance of safely refactoring legacy code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM