Shruti Rawat
Shruti Rawat

Reputation: 697

How to move a method from a class to another class when two classes are not at all related

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

Answers (6)

René Link
René Link

Reputation: 51343

I would do it this way:

  1. Ensure that your tests work and the code to be re-factored is covered. If you don't have tests write tests. They are your safety rope.
  2. Use the re-factoring pattern extract superclass to create the new class that you want to move some methods to.
  3. Use the re-factoring pattern pull up methods to move the methods along with the variables that they need to the superclass. Now you will see if the methods you want to move and the instance variables have dependencies to the other methods that you don't want to move. If so you must first break this dependencies.
  4. Find all client code that should use the new extracted class instead of the "old" class and rewrite it to the new extracted class.
  5. Remove the "extends" relationship between the two classes. Now the client code should work or you missed something.

Also a good book for learning how to apply re-factoring patterns is Working Effectively with Legacy Code

Upvotes: 10

JnRouvignac
JnRouvignac

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

Carl Manaster
Carl Manaster

Reputation: 40336

  1. Copy the method into the new class.
  2. Replace the method body in the old class with a call into the new class.
  3. Inline the old method.

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

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

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

if you using eclipse IDE then refactor will help you.

enter image description here

Upvotes: 3

PNS
PNS

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

Related Questions