Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse, refactoring a java method into a another class

Tags:

eclipse

How can I refactor(move) a Java method in classA into classB and have all references to the method to it updated?

Is this supported in Eclipse ?

like image 994
mjs Avatar asked Feb 17 '12 15:02

mjs


2 Answers

For a static method, you can right click and select 'Move'.

Obj1.myMethod()

would then get 'moved' to

Obj2.myMethod()

and eclipse would fix your imports etc.

For a non-static method, this may not work depending on the relationship between classA and classB.

Obj1 myobj1 = new Obj1();
myobj1.myMethod();
myobj1.myOtherMethod();

If you move myMethod() to a different class, the refactoring would have to change the object initialization. If myOtherMethod isn't getting moved, then it can't just change the type of myobj1 to Obj2 because then myOtherMethod won't work.

like image 133
Thomas Avatar answered Oct 16 '22 09:10

Thomas


  • Select the method in Outline view
  • Refactor > Move

If you want to move the method to a new class - Refactor > Extract Class

like image 23
Deepak Azad Avatar answered Oct 16 '22 08:10

Deepak Azad