-
Notifications
You must be signed in to change notification settings - Fork 359
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
Open
ThomasKianek-Gepardec
wants to merge
22
commits into
openrewrite:main
Choose a base branch
from
Gepardec:feature/groovy-recipe-param
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
5cdedcb
WIP added ChangeGroovyMethodInvocationParameter Recipe
3ec001b
added license header
sgartner03 2dcf846
Simplified tests
sgartner03 e77a05a
Added support for double quotes and filtering for given method
ThomasKianek-Gepardec 3ae4b21
Merge branch 'main' into feature/groovy-recipe-param
timtebeek a52557e
Merge branch 'main' into feature/groovy-recipe-param
Xaeras 2a3d54e
incorporated actions import change
sgartner03 4c8171d
Incoorporated UUID actions change
sgartner03 b550ddf
incorporated actions ctx change
sgartner03 a420fb9
incorporated actions method change
sgartner03 a0f8611
incorporated actions test change
sgartner03 71fa8b9
c
sgartner03 fff88fb
incorporated actions test change
sgartner03 2adc6ed
Fixed code after broken GH Actions suggestions
sgartner03 a1fe037
Merge branch 'main' into feature/groovy-recipe-param
timtebeek c618093
Changed Recipe name and added tests
sgartner03 c6b9735
Merge branch 'main' into feature/groovy-recipe-param
timtebeek 5d558f4
Update test formatting and naming to match existing tests
timtebeek 1b6c398
Removed extractQuoting
sgartner03 edd9a59
Refactoring
sgartner03 d7b1e1c
Added null check for warning
sgartner03 8b5d7ec
Merge branch 'main' into feature/groovy-recipe-param
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
...main/java/org/openrewrite/groovy/ChangeStringValueOfNamedParameterInMethodInvocation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* 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.jspecify.annotations.Nullable; | ||
import org.openrewrite.*; | ||
import org.openrewrite.groovy.tree.G; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.JavaType; | ||
|
||
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; | ||
} | ||
|
||
if (!hasStringValue(mapEntry)) { | ||
return mapEntry; | ||
} | ||
|
||
if (!mapEntry.getKey().toString().equals(key)) { | ||
return mapEntry; | ||
} | ||
|
||
if (mapEntry.getValue().toString().equals(value)) { | ||
return mapEntry; | ||
} | ||
|
||
return replaceValue(mapEntry); | ||
} | ||
|
||
private boolean isInTargetMethod() { | ||
return getCursor().firstEnclosingOrThrow(J.MethodInvocation.class).getSimpleName().equals(methodName); | ||
} | ||
|
||
private G. MapEntry replaceValue(final G.MapEntry mapEntry) { | ||
J.Literal entryValue = (J.Literal) mapEntry.getValue(); | ||
|
||
J.Literal newEntryValue = entryValue | ||
.withValue(value) | ||
.withValueSource(buildNewValueSource(entryValue)); | ||
|
||
return mapEntry.withValue(newEntryValue); | ||
} | ||
|
||
private @Nullable String buildNewValueSource(J.Literal entryValue) { | ||
String oldValue = entryValue.getValue() == null ? "" : entryValue.getValue().toString(); | ||
|
||
if (entryValue.getValueSource() == null) { | ||
return null; | ||
} | ||
|
||
return entryValue.getValueSource().replace(oldValue, value); | ||
} | ||
}; | ||
} | ||
|
||
private static boolean hasStringValue(G.MapEntry mapEntry) { | ||
return ((J.Literal) mapEntry.getValue()).getType().equals(JavaType.Primitive.String); | ||
} | ||
|
||
|
||
} |
143 changes: 143 additions & 0 deletions
143
...st/java/org/openrewrite/groovy/ChangeNamedStringParameterValueOfMethodInvocationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
""" | ||
) | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.