Refactoring static methods to instance methods in Eclipse

How can I reorganize

class Plugh { static void foo(Bar bar); } 

in

 class Bar { void foo(); } 

using eclipse? IOW static methods are used in instance methods of one of the arguments.

+6
java eclipse refactoring
source share
3 answers

Remove the static keyword and then refactor the Move Method. He should offer the Bar as a target class.

(It seems crazy to me that Eclipse does this only for non-static methods, but it works like that. It seems like a mistake. Maybe I should work on a fix, and not just complain about it! -)

+9
source share

I don't believe there is a fully automated way to do this, but I would have Plugh.foo() bar.foo() , then use Quick Fix (control-1) to create bar.foo() , then cut it out and insert the (remaining) body of Plugh.foo() into bar.foo() .

Then insert all the calls into Plugh.foo() and do the initial assignment inside bar.foo() : Bar bar = this; , then insert local (and maybe clear all this. in the method).

+4
source share
  • Change your code to this:

     class Plugh { static void foo(Bar bar) { bar.foo(); } } 
  • Then the inline method is Plugh.foo(...) .
    All calls to Plugh.foo(...) will be replaced by barInstance.foo(); .
    barInstance can be called anything in the calling code. It will be replaced correctly.

0
source share

All Articles