-
Notifications
You must be signed in to change notification settings - Fork 355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added recipe for changing Groovy params #4581
base: main
Are you sure you want to change the base?
Changes from 18 commits
5cdedcb
3ec001b
2dcf846
e77a05a
3ae4b21
a52557e
2a3d54e
4c8171d
b550ddf
a420fb9
a0f8611
71fa8b9
fff88fb
2adc6ed
a1fe037
c618093
c6b9735
5d558f4
1b6c398
edd9a59
d7b1e1c
8b5d7ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.groovy; | ||
|
||
import org.openrewrite.*; | ||
import org.openrewrite.groovy.tree.G; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.JavaType; | ||
import org.openrewrite.java.tree.Space; | ||
import org.openrewrite.marker.Markers; | ||
|
||
public class ChangeStringValueOfNamedParameterInMethodInvocation extends Recipe { | ||
|
||
@Option | ||
public String methodName; | ||
|
||
@Option | ||
public String key; | ||
|
||
@Option | ||
public String value; | ||
|
||
public ChangeStringValueOfNamedParameterInMethodInvocation(final String methodName, final String key, final String value) { | ||
this.methodName = methodName; | ||
this.key = key; | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public @NlsRewrite.DisplayName String getDisplayName() { | ||
return "Change the value of a groovy named string parameter of a method invocation"; | ||
} | ||
|
||
@Override | ||
public @NlsRewrite.Description String getDescription() { | ||
return "Changes the value of a given parameter in a given groovy method invocation, supporting Strings and GStrings."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return new GroovyIsoVisitor<ExecutionContext>() { | ||
@Override | ||
public J visitMapEntry(G.MapEntry mapEntry, ExecutionContext ctx) { | ||
mapEntry = (G.MapEntry) super.visitMapEntry(mapEntry, ctx); | ||
|
||
if (!isInTargetMethod()) { | ||
return mapEntry; | ||
} | ||
|
||
char quote = extractQuoting(mapEntry); | ||
if (quote != '\'' && extractQuoting(mapEntry) != '"') { | ||
return mapEntry; | ||
} | ||
|
||
if (!mapEntry.getKey().toString().equals(key)) { | ||
return mapEntry; | ||
} | ||
|
||
if (mapEntry.getValue().toString().equals(value)) { | ||
return mapEntry; | ||
} | ||
|
||
return replaceValue(mapEntry, quote); | ||
} | ||
|
||
private boolean isInTargetMethod() { | ||
return getCursor().firstEnclosingOrThrow(J.MethodInvocation.class).getSimpleName().equals(methodName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the intended use case here? Because we'd much rather match using a MethodMatcher as opposed to a String comparision, as this would match any method anywhere of the same name, which is not as type safe as we prefer to be. For Gradle build script type information isn't always available to match reliably, but even then there's established patterns to be a little more sure we're not just matching any method by simple name, hence why I ask here. |
||
} | ||
|
||
private G. MapEntry replaceValue(final G.MapEntry mapEntry, char quote) { | ||
return mapEntry.withValue(new J.Literal(Tree.randomId(), Space.SINGLE_SPACE, Markers.EMPTY, value, String.format("%c%s%c", quote, value, quote), null, JavaType.Primitive.String)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would we not want to retain any original formatting and comments by using |
||
} | ||
|
||
private char extractQuoting(final G.MapEntry mapEntry) { | ||
return mapEntry.getValue().printTrimmed(getCursor()).charAt(0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the only time I've seen There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, you're right. I've removed extractQuoting |
||
} | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.groovy; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.groovy.Assertions.groovy; | ||
|
||
class ChangeNamedStringParameterValueOfMethodInvocationTest implements RewriteTest { | ||
|
||
@Test | ||
void whenParamSet_thenChangeToNewValue() { | ||
rewriteRun(spec -> spec.recipe(new ChangeStringValueOfNamedParameterInMethodInvocation("method", "param", "newValue")), | ||
//language=groovy | ||
groovy( | ||
""" | ||
method( | ||
param: 'oldValue' | ||
) | ||
""", """ | ||
method( | ||
param: 'newValue' | ||
) | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void noChangeWhenNewValueEqualsOldValue() { | ||
rewriteRun(spec -> spec.recipe(new ChangeStringValueOfNamedParameterInMethodInvocation("method", "param", "value")), | ||
//language=groovy | ||
groovy( | ||
""" | ||
method( | ||
param: 'value' | ||
) | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void whenSingleParamSetWithGString_thenChangeToNewValue() { | ||
rewriteRun(spec -> spec.recipe(new ChangeStringValueOfNamedParameterInMethodInvocation("method", "param", "newValue")), | ||
//language=groovy | ||
groovy( | ||
""" | ||
method( | ||
param: "oldValue" | ||
) | ||
""", """ | ||
method( | ||
param: "newValue" | ||
) | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void whenMethodWithMultipleParameters_thenChangeOnlySelectedToNewValue() { | ||
rewriteRun(spec -> spec.recipe(new ChangeStringValueOfNamedParameterInMethodInvocation("method", "param2", "newValue")), | ||
//language=groovy | ||
groovy( | ||
""" | ||
method( | ||
param1: "oldValue", | ||
param2: "oldValue", | ||
param3: "oldValue" | ||
) | ||
""", """ | ||
method( | ||
param1: "oldValue", | ||
param2: "newValue", | ||
param3: "oldValue" | ||
) | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void whenTwoMethods_thenOnlyChangeOne() { | ||
rewriteRun(spec -> spec.recipe(new ChangeStringValueOfNamedParameterInMethodInvocation("method2", "param", "newValue")), | ||
groovy( | ||
""" | ||
method1( | ||
param: "oldValue" | ||
) | ||
method2( | ||
param: "oldValue" | ||
) | ||
""", """ | ||
method1( | ||
param: "oldValue" | ||
) | ||
method2( | ||
param: "newValue" | ||
) | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void noChangeWhenParameterWithOtherDatatype() { | ||
rewriteRun(spec -> spec.recipe(new ChangeStringValueOfNamedParameterInMethodInvocation("method", "param", "newValue")), | ||
groovy( | ||
""" | ||
method( | ||
param: 1 | ||
) | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void noChangeWhenMethodWithoutParameters() { | ||
rewriteRun(spec -> spec.recipe(new ChangeStringValueOfNamedParameterInMethodInvocation("method2", "param", "newValue")), | ||
groovy( | ||
""" | ||
method() | ||
""" | ||
) | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See below as well for a potential alternative looking at the type of the
mapEntry
instead, but calling the same method twice with the same argument is a bit suspect.