Search code examples
intellij-idearefactoringautomated-refactoring

How to "flatten" or "merge" the code that is spread across too many methods in Java (using Intellij)?


This may be a very simple question, however, for some reason, I did not find an answer.

To explain what I am trying to do, lets say I have:

public String A(){
return B();
}

public String B(){
return "HELLO";
}

I would like this to become:

public String A(){
return "HELLO";
}

I want to avoid doing this manually.


Solution

  • You can place your caret at the function you want to inline and hit Ctrl + Alt + N. The Intellij will find the occurrences and propose you a certain solutions listed below in the image.

    In your case you want to select the first option in order to remove the B function.

    enter image description here

    When you have trouble finding a refactoring automation you can place the caret to the piece of code you want to refactor and hit Ctrl + Alt + Shift + T a dialog will pop up with all the possible refactorings. You can refer to the official documentation for more information.