Reputation: 697
I am trying to re factor some code by breaking a class into several other classes. to do so i want to move some methods already existing in my old class to new class. But these methods are being referred in a lot of places and manually updating the references seems tiresome. So is there any way to move methods as well as update their references in eclipse?
Upvotes: 10
Views: 19107
Reputation: 51343
I would do it this way:
Also a good book for learning how to apply re-factoring patterns is Working Effectively with Legacy Code
Upvotes: 10
Reputation: 787
I will show you the process I follow. Consider such code:
public class GodClass {
public someInstanceMethodToMove() {
// some code 1
}
public static someStaticMethodToMove() {
// some code 2
}
public static void main(String[] args) {
GodClass c = ...;
c.someInstanceMethodToMove();
GodClass.someStaticMethodToMove();
}
}
Create the new class:
public class SingleResponsibilityClass {
}
The static method can be directly moved to the SingleResponsibilityClass
by using Eclipse's Refactor
> Move...
refactoring as described by Prabhakaran:
public class GodClass {
public someInstanceMethodToMove() {
// some code 1
}
public static void main(String[] args) {
GodClass c = ...;
c.someInstanceMethodToMove();
SingleResponsibilityClass.someStaticMethodToMove();
}
}
public class SingleResponsibilityClass {
public static someStaticMethodToMove() {
// some code 2
}
}
For the instance method, the process is a little more complex. Read below.
Extract a method out of someInstanceMethodToMove()
and let's name it someInstanceMethodToMove2()
:
public class GodClass {
public someInstanceMethodToMove() {
someInstanceMethodToMove2();
}
private someInstanceMethodToMove2() {
// some code 1
}
// ...
}
Use the SingleResponsibilityClass
in the original method:
public class GodClass {
public someInstanceMethodToMove() {
someInstanceMethodToMove2(new SingleResponsibilityClass());
}
private someInstanceMethodToMove2(SingleResponsibilityClass obj) {
// some code 1
}
// ...
}
Note: it is important that SingleResponsibilityClass
is a parameter of the instance method to move, otherwise Eclipse will not move it to this type.
From there, right click on someInstanceMethodToMove2()
, and select Refactor
> Move...
, select SingleResponsibilityClass type in the wizard, then apply:
public class GodClass {
public someInstanceMethodToMove() {
new SingleResponsibilityClass().someInstanceMethodToMove2();
}
// ...
}
public class SingleResponsibilityClass {
private someInstanceMethodToMove2() {
// some code 1
}
public static someStaticMethodToMove() {
// some code 2
}
}
Then right click on SingleResponsibilityClass
' someInstanceMethodToMove2()
method and Refactor
> Rename
it to someInstanceMethodToMove()
:
public class GodClass {
public someInstanceMethodToMove() {
new SingleResponsibilityClass().someInstanceMethodToMove();
}
// ...
}
public class SingleResponsibilityClass {
private someInstanceMethodToMove() {
// some code 1
}
public static someStaticMethodToMove() {
// some code 2
}
}
Then right click on GodClass
' someInstanceMethodToMove()
method and Refactor
> Inline
:
public class GodClass {
public static void main(String[] args) {
GodClass c = ...;
new SingleResponsibilityClass().someInstanceMethodToMove();
SingleResponsibilityClass.someStaticMethodToMove();
}
}
public class SingleResponsibilityClass {
private someInstanceMethodToMove() {
// some code 1
}
public static someStaticMethodToMove() {
// some code 2
}
}
Upvotes: 2
Reputation: 40336
That's all you need. It may not be quite that simple, because in steps 1 and 2 you may need to add arguments and/or make the method static, but that is the essence of how to do it.
Upvotes: 1
Reputation: 6617
does it have any of your satisfaction
package com.hussi.stackOverFlow;
class ClassOne {
public void methodInClassOne(String stringParam)
{
ClassTwo classTwoObj = new ClassTwo();
classTwoObj.methodInClassTwo(stringParam);
}
}
class ClassTwo {
public void methodInClassTwo(String stringParam)
{
System.out.println(stringParam);
}
}
public class ClassThree {
public static void main(String[] args)
{
ClassOne objClassOne = new ClassOne();
// calling method of class two in class one
objClassOne.methodInClassOne("pass this String value");
}
}
Upvotes: 1
Reputation: 26094
if you using eclipse IDE then refactor will help you.
Upvotes: 3
Reputation: 19895
If you use any of the standard IDEs (e.g., Eclipse
or IntelliJ IDEA
), they all have a simple menu option to do that (depending on how the code is organized).
If you go to each method and right-click on its name, the menu has a "Refactor" option, which leads to a "Move" option. Select that and follow the instructions.
The above is especially easy for static methods. For the non-static ones, you may need to do subclassing, or pass references to the appropriate objects around. Even so, the "Refactor -> Move" option is a good start.
Upvotes: 0