Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I apply the "move method" refactoring with IntelliJ IDEA?

I want to be able to move an instance method from one class to another class ("Move method" from Fowler's "Refactoring") in IntelliJ IDEA. Unfortunately, when I try "Move..." (cmd: F6), it tells me that "There are no methods that have a reference type. Would you like to make method static and then move?" I do not want to make my method static, I want it to be an instance method on the other class instead.

My code example:

public class TheClass {

  public void doStuff(){
     int i = themethod();
  }

  private int theMethod() {
    System.out.println( "Hello World!" );
    return 0;
  }
}

public class OtherClass {

}

Say I want to move theMethod from TheClass to OtherClass. Is there an automatic refactoring in IDEA for this, and if so: How do I apply it?

like image 571
mranders Avatar asked Mar 10 '11 16:03

mranders


People also ask

What is Move method in refactoring?

Move Method Refactoring (MMR) refers to moving a method from one class to the class in which the method is used the most often. Manually inspecting and analyzing the source code of the system under consideration to determine the methods in need of MMR is a costly and time-consuming process.

How do I move a method to another class in C#?

Select the chunk of code you want to move to class and Right Click -> Refactor -> Generate Method.. This will generate a method with all dependants as Input, and then you can move the functions to another class or class library!


3 Answers

In IntelliJ 14-15 do the following:

  1. Position the caret on theMethod().
  2. press Ctrl/Cmd+F6 (Change signature).
  3. Introduce new parameter: Type=TheOtherClass, Name=theOtherClass, Default value=new TheOtherClass()
  4. Refactor
  5. Then press F6 (move) and move the method to theOtherClass.

You will end up with:

public class TheClass {
    public void doStuff() {
        int i = new TheOtherClass().theMethod();
    }
}
public class TheOtherClass {
    int theMethod() {
        System.out.println("Hello World!");
        return 0;
    }
}
like image 193
Maarten Avatar answered Oct 16 '22 21:10

Maarten


The Move Method refactoring in IDEA only considers moving the method into classes related to it, i.e. used as its parameter or return value, or called from inside the method. Which is kinda logical: if the method has nothing concrete to do with the target class, why should it be there? OTOH I found this limiting in some cases where I still had a valid reason to move the method. So I had to do it by hand.

like image 10
Péter Török Avatar answered Oct 16 '22 22:10

Péter Török


In intellij 13.1 (dont' know in previous version) it could be done with the

Choose Refactor | Extract | Delegate on the main menu

but there is a "strange" limit, apparently: it could be done only with a new freshly created class. So you have to do apply this refactoring without creating the "OtherClass" (it will be create directly when you apply the refactoring).

So a real "move" of method on an alredy created class seems missing, quite strange behaviou

like image 9
Antimo Avatar answered Oct 16 '22 22:10

Antimo