Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract a method across files?

In all my java files of my project, I want to replace occurrences of this:

myObject.getName() == null

With that:

myObject.hasName()

Is there any action or strategy to do this with IntelliJ IDEA?

  • I want it to be specific to the getName method of my class MyClass and not to replace code, that refers to methods called getName of other classes.
  • I want it to find as many matches as possible. It should not care about additional spaces, linebreaks, etc. If the variable has another name (like myObj or objX) it should also work. Calls like getObject().getName() should also be found. (The search has to care about semantics).
  • I want it to be fast and not to require me to go through the hundreds of matches one by one.
like image 887
slartidan Avatar asked Apr 18 '17 12:04

slartidan


People also ask

How do you extract a method?

To extract method: Select a code fragment you want to extract to a method. Press Ctrl+Alt+M or from the main menu, select Refactor | Extract | Method. In the dialog that opens, configure a method options, such as visibility, parameters, and so on. You can also change a name of the method if you need.

How do I extract a code?

Highlight the code you want to extract to a method and press ⌥⌘M (macOS), or Ctrl+Alt+M (Windows/Linux), to extract it.

What is extract method in Python?

Extract method Last modified: 27 September 2022. The Extract Method refactoring lets you take a code fragment that can be grouped, move it into a separated method, and replace the old code with a call to the method.


2 Answers

Use Find and Replace Code Duplicates.

  1. Create the instance method:

    public boolean hasName() {
        return getName() == null;
    }
    
    • To do that, I would suggest selecting myObject.getName() == null, then using actions Refactor | Extract | Method... and Refactor | Move..., so you don't have to type any code.
  2. Click on the method and use Refactor | Find and Replace Code Duplicates.

    • However it will not refactor usages like these:

      String name = myObject.getName();
      if (name == null) {
          ...
      }
      
    • You can also use this action on a non-instance method:

      private boolean hasName(MyClass myObject) {
          return myObject.getName() == null;
      }
      

      which will result in making the method static, then use Refactor | Convert To Instance Method. Same result.

like image 128
Meo Avatar answered Sep 21 '22 20:09

Meo


Use Structural Search & Replace (Edit | Find | Replace Structurally...), it is designed for use cases like this.

Use Search template:

$x$.getName() == null

Replacement template:

$x$.hasName()

Click the Edit Variables... button and specify Expression type (regexp) as MyClass. When searching this will also find your example of getObject().getName() == null

like image 21
Bas Leijdekkers Avatar answered Sep 21 '22 20:09

Bas Leijdekkers