rewrite icon indicating copy to clipboard operation
rewrite copied to clipboard

Allow AddMethodParameter to operate on overridden methods

Open dsgrieve opened this issue 1 month ago • 1 comments

What problem are you trying to solve?

Say I have interface A { int count(Sheep flock); } and an implementation of that class Sleep implements A { @Override public int count(Sheep flock) { return flock.size(); }

If interface A changes the count method to int count(Sheep flock, Wolf wolf);, I'd like to be able to add the new method parameter to classes that inherit/extend from the super class. I'd like to be able to use

recipeList:
  - org.openrewrite.java.AddMethodParameter
    methodPattern: "A count(Sheep)"
    parameterType: "Wolf"
    parameterName: "wolf"
    parameterIndex: 1

As AddMethodParameter is implemented now, it will only match the specific type. For this to work, then I would have to have methodPattern: "Sleep count(Sheep)". But what if I have many implementations of A? I would have to write an imperative recipe that pulls the type of the overriding class to properly configure and call AddMethodParameter on the fly.

Describe the solution you'd like

Before: class Sleep implements A { @Override public int count(Sheep flock) { return flock.size(); } After class Sleep implements A { @Override public int count(Sheep flock, Wolf wolf) { return flock.size(); }

Have you considered any alternatives or workarounds?

Create an imperative recipe that does something like this in a visitMethodDeclaration

                                String receiverType = enclosingClass.getType().getFullyQualifiedName();
                                String methodPattern = receiverType + " count(Sheep)";

                                doAfterVisit(new AddMethodParameter(
                                    methodPattern,
                                    "Wolf",
                                    "wolf",
                                    1
                                ).getVisitor());

Additional context

Are you interested in contributing this feature to OpenRewrite?

dsgrieve avatar Nov 14 '25 02:11 dsgrieve

I am on it :)

Anmol202005 avatar Nov 14 '25 15:11 Anmol202005