-
Notifications
You must be signed in to change notification settings - Fork 482
Description
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?
Metadata
Metadata
Assignees
Labels
Type
Projects
Status