From 5cdedcb7706715f704fbf035631e634d69db0154 Mon Sep 17 00:00:00 2001 From: Andreas Novak Date: Fri, 11 Oct 2024 11:13:01 +0200 Subject: [PATCH 01/17] WIP added ChangeGroovyMethodInvocationParameter Recipe (cherry picked from commit 7040a8b0466a1fcff00d7b9668b40eaf09f6bf65) --- ...ChangeGroovyMethodInvocationParameter.java | 58 ++++++++++++++ ...geGroovyMethodInvocationParameterTest.java | 79 +++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java create mode 100644 rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java diff --git a/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java new file mode 100644 index 00000000000..9c7d662adc0 --- /dev/null +++ b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java @@ -0,0 +1,58 @@ +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; + +import java.util.UUID; + +public class ChangeGroovyMethodInvocationParameter extends Recipe { + @Option + public String key; + + @Option + public String value; + + public ChangeGroovyMethodInvocationParameter() { + } + + public ChangeGroovyMethodInvocationParameter(String key, String value) { + this.key = key; + this.value = value; + } + + @Override + public @NlsRewrite.DisplayName String getDisplayName() { + return "Change groovy a parameter"; + } + + @Override + public @NlsRewrite.Description String getDescription() { + return "It updates the value of a given parameter on all groovy method invocations."; + } + + @Override + public TreeVisitor getVisitor() { + return new GroovyIsoVisitor<>() { + @Override + public J visitMapEntry(G.MapEntry mapEntry, ExecutionContext executionContext) { + mapEntry = (G.MapEntry) super.visitMapEntry(mapEntry, executionContext); + + if (mapEntry.getValue().toString().equals("'" + value + "'")) { + return mapEntry; + } + + if (mapEntry.getKey().toString().equals(key)) { + final J.Literal valueLiteral = new J.Literal(UUID.randomUUID(), Space.SINGLE_SPACE, Markers.EMPTY, "'" + value + "'", "'" + value + "'", null, JavaType.Primitive.String); + return mapEntry.withValue(valueLiteral); + } + + return mapEntry; + } + }; + } +} diff --git a/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java b/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java new file mode 100644 index 00000000000..5de76ac9f1d --- /dev/null +++ b/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java @@ -0,0 +1,79 @@ +package org.openrewrite.groovy; + +import org.junit.jupiter.api.Test; +import org.openrewrite.test.RewriteTest; + +class ChangeGroovyMethodInvocationParameterTest implements RewriteTest { + + @Test + public void givenGroovyFile_whenMultiParamSet_thenChangeToNewValue() { + rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("jdkVersion", "Eclipse Temurin 17.0.11")), + //language=groovy + Assertions.groovy(""" + releaseJob( + mavenVersion: 'Maven 3.9.5', + jdkVersion: "RedHat OpenJDK 11.0 latest", + gitUrl: 'github.com/openrewrite/rewrite.git', + baseDirectory: '.', + buildsToKeep: '5' + ) + """, """ + releaseJob( + mavenVersion: 'Maven 3.9.5', + jdkVersion: 'Eclipse Temurin 17.0.11', + gitUrl: 'github.com/openrewrite/rewrite.git', + baseDirectory: '.', + buildsToKeep: '5' + ) + """)); + } + + @Test + public void givenGroovyFile_whenSingleParamSet_thenChangeToNewValue() { + rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("mavenVersion", "Maven 3.9.6")), + //language=groovy + Assertions.groovy(""" + releaseJob( + mavenVersion: 'Maven 3.9.5' + ) + """, """ + releaseJob( + mavenVersion: 'Maven 3.9.5' + ) + """)); + } + + @Test + public void givenGroovyFile_whenSingleParamSet_thenChangeToNewValueUsingGStrings() { + rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("mavenVersion", "Maven 3.9.6")), + //language=groovy + Assertions.groovy(""" + releaseJob( + mavenVersion: "Maven 3.9.5" + ) + """, """ + releaseJob( + mavenVersion: "Maven 3.9.6" + ) + """)); + } + + @Test + public void givenGroovyFile_whenSingleParamSet_thenChangeToNewValueUsingTemplating() { + rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("jdkVersion", "Eclipse Temurin 17.0.11")), Assertions.groovy(""" + job( + mavenVersion: 'Maven 3.9.5' + ) + releaseJob( + mavenVersion: 'Maven 3.9.5' + ) + """, """ + job( + mavenVersion: 'Maven 3.9.5' + ) + releaseJob( + mavenVersion: 'Maven 3.9.6' + ) + """)); + } +} \ No newline at end of file From 3ec001b78f046dbbcfc159a4070caf475bc0bb67 Mon Sep 17 00:00:00 2001 From: Simon Gartner Date: Fri, 11 Oct 2024 11:52:21 +0200 Subject: [PATCH 02/17] added license header --- .../ChangeGroovyMethodInvocationParameter.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java index 9c7d662adc0..1efac101fdb 100644 --- a/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java +++ b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java @@ -1,3 +1,18 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * 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 + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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; @@ -37,7 +52,7 @@ public ChangeGroovyMethodInvocationParameter(String key, String value) { @Override public TreeVisitor getVisitor() { - return new GroovyIsoVisitor<>() { + return new GroovyIsoVisitor() { @Override public J visitMapEntry(G.MapEntry mapEntry, ExecutionContext executionContext) { mapEntry = (G.MapEntry) super.visitMapEntry(mapEntry, executionContext); From 2dcf846d1c42b39641fad58607a513640bffcda8 Mon Sep 17 00:00:00 2001 From: Simon Gartner Date: Fri, 11 Oct 2024 12:50:31 +0200 Subject: [PATCH 03/17] Simplified tests --- ...geGroovyMethodInvocationParameterTest.java | 79 ++++++++++--------- 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java b/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java index 5de76ac9f1d..98a30d2f044 100644 --- a/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java +++ b/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java @@ -1,3 +1,18 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * 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 + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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; @@ -6,73 +21,61 @@ class ChangeGroovyMethodInvocationParameterTest implements RewriteTest { @Test - public void givenGroovyFile_whenMultiParamSet_thenChangeToNewValue() { - rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("jdkVersion", "Eclipse Temurin 17.0.11")), + public void givenGroovyFile_whenParamSet_thenChangeToNewValue() { + rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("param", "newValue")), //language=groovy Assertions.groovy(""" - releaseJob( - mavenVersion: 'Maven 3.9.5', - jdkVersion: "RedHat OpenJDK 11.0 latest", - gitUrl: 'github.com/openrewrite/rewrite.git', - baseDirectory: '.', - buildsToKeep: '5' + method( + param: 'oldValue' ) """, """ - releaseJob( - mavenVersion: 'Maven 3.9.5', - jdkVersion: 'Eclipse Temurin 17.0.11', - gitUrl: 'github.com/openrewrite/rewrite.git', - baseDirectory: '.', - buildsToKeep: '5' + method( + param: 'newValue' ) """)); } @Test - public void givenGroovyFile_whenSingleParamSet_thenChangeToNewValue() { - rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("mavenVersion", "Maven 3.9.6")), + public void givenGroovyFile_whenNewValueEqualsOldValue_thenChangeNothing() { + rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("param", "value")), //language=groovy Assertions.groovy(""" - releaseJob( - mavenVersion: 'Maven 3.9.5' - ) - """, """ - releaseJob( - mavenVersion: 'Maven 3.9.5' + method( + param: 'value' ) """)); } @Test - public void givenGroovyFile_whenSingleParamSet_thenChangeToNewValueUsingGStrings() { - rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("mavenVersion", "Maven 3.9.6")), + public void givenGroovyFile_whenSingleParamSetWithGString_thenChangeToNewValue() { + rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("param", "newValue")), //language=groovy Assertions.groovy(""" - releaseJob( - mavenVersion: "Maven 3.9.5" + method( + param: "oldValue" ) """, """ - releaseJob( - mavenVersion: "Maven 3.9.6" + method( + param: "newValue" ) """)); } @Test - public void givenGroovyFile_whenSingleParamSet_thenChangeToNewValueUsingTemplating() { - rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("jdkVersion", "Eclipse Temurin 17.0.11")), Assertions.groovy(""" - job( - mavenVersion: 'Maven 3.9.5' + public void givenGroovyFile_whenTwoMethods_thenOnlyChangeOne() { + rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("param", "newValue")), Assertions.groovy(""" + method1( + param: "oldValue" ) - releaseJob( - mavenVersion: 'Maven 3.9.5' + method2( + param: "oldValue" ) """, """ - job( - mavenVersion: 'Maven 3.9.5' + method1( + param: "oldValue" ) - releaseJob( - mavenVersion: 'Maven 3.9.6' + method2( + param: "newValue" ) """)); } From e77a05a3f8ab6a67508e04d6a08e576a909437b0 Mon Sep 17 00:00:00 2001 From: "thomas.kianek" Date: Mon, 14 Oct 2024 16:44:03 +0200 Subject: [PATCH 04/17] Added support for double quotes and filtering for given method --- ...ChangeGroovyMethodInvocationParameter.java | 38 ++++++++++++++----- ...geGroovyMethodInvocationParameterTest.java | 18 ++++----- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java index 1efac101fdb..18ae2fcd46a 100644 --- a/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java +++ b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java @@ -15,7 +15,7 @@ */ package org.openrewrite.groovy; - +import org.jetbrains.annotations.NotNull; import org.openrewrite.*; import org.openrewrite.groovy.tree.G; import org.openrewrite.java.tree.J; @@ -26,28 +26,30 @@ import java.util.UUID; public class ChangeGroovyMethodInvocationParameter extends Recipe { + + @Option + public String methodName; + @Option public String key; @Option public String value; - public ChangeGroovyMethodInvocationParameter() { - } - - public ChangeGroovyMethodInvocationParameter(String key, String value) { + public ChangeGroovyMethodInvocationParameter(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 groovy a parameter"; + return "Change a groovy parameter"; } @Override public @NlsRewrite.Description String getDescription() { - return "It updates the value of a given parameter on all groovy method invocations."; + return "Changes the value of a given parameter in a given groovy method invocation."; } @Override @@ -57,17 +59,33 @@ public TreeVisitor getVisitor() { public J visitMapEntry(G.MapEntry mapEntry, ExecutionContext executionContext) { mapEntry = (G.MapEntry) super.visitMapEntry(mapEntry, executionContext); - if (mapEntry.getValue().toString().equals("'" + value + "'")) { + if (!isInTargetMethod()) { + return mapEntry; + } + + if (mapEntry.getValue().toString().equals(value)) { return mapEntry; } if (mapEntry.getKey().toString().equals(key)) { - final J.Literal valueLiteral = new J.Literal(UUID.randomUUID(), Space.SINGLE_SPACE, Markers.EMPTY, "'" + value + "'", "'" + value + "'", null, JavaType.Primitive.String); - return mapEntry.withValue(valueLiteral); + return replaceValue(mapEntry); } return mapEntry; } + + private boolean isInTargetMethod() { + return getCursor().firstEnclosingOrThrow(J.MethodInvocation.class).getSimpleName().equals(methodName); + } + + private G.@NotNull MapEntry replaceValue(final G.MapEntry mapEntry) { + final char quote = extractQuoting(mapEntry); + return mapEntry.withValue(new J.Literal(UUID.randomUUID(), Space.SINGLE_SPACE, Markers.EMPTY, value, String.format("%c%s%c", quote, value, quote), null, JavaType.Primitive.String)); + } + + private char extractQuoting(final G.MapEntry mapEntry) { + return mapEntry.getValue().printTrimmed(getCursor()).charAt(0); + } }; } } diff --git a/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java b/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java index 98a30d2f044..86d06c44c56 100644 --- a/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java +++ b/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java @@ -21,8 +21,8 @@ class ChangeGroovyMethodInvocationParameterTest implements RewriteTest { @Test - public void givenGroovyFile_whenParamSet_thenChangeToNewValue() { - rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("param", "newValue")), + void givenGroovyFile_whenParamSet_thenChangeToNewValue() { + rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("method", "param", "newValue")), //language=groovy Assertions.groovy(""" method( @@ -36,8 +36,8 @@ public void givenGroovyFile_whenParamSet_thenChangeToNewValue() { } @Test - public void givenGroovyFile_whenNewValueEqualsOldValue_thenChangeNothing() { - rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("param", "value")), + void givenGroovyFile_whenNewValueEqualsOldValue_thenChangeNothing() { + rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("method", "param", "value")), //language=groovy Assertions.groovy(""" method( @@ -47,8 +47,8 @@ public void givenGroovyFile_whenNewValueEqualsOldValue_thenChangeNothing() { } @Test - public void givenGroovyFile_whenSingleParamSetWithGString_thenChangeToNewValue() { - rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("param", "newValue")), + void givenGroovyFile_whenSingleParamSetWithGString_thenChangeToNewValue() { + rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("method", "param", "newValue")), //language=groovy Assertions.groovy(""" method( @@ -62,12 +62,12 @@ public void givenGroovyFile_whenSingleParamSetWithGString_thenChangeToNewValue() } @Test - public void givenGroovyFile_whenTwoMethods_thenOnlyChangeOne() { - rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("param", "newValue")), Assertions.groovy(""" + void givenGroovyFile_whenTwoMethods_thenOnlyChangeOne() { + rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("method2", "param", "newValue")), Assertions.groovy(""" method1( param: "oldValue" ) - method2( + method2( param: "oldValue" ) """, """ From 2a3d54e955bc43ff7df216622339ddbf08c96590 Mon Sep 17 00:00:00 2001 From: sgartner03 <117266185+sgartner03@users.noreply.github.com> Date: Wed, 16 Oct 2024 14:11:26 +0200 Subject: [PATCH 05/17] incorporated actions import change Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../groovy/ChangeGroovyMethodInvocationParameter.java | 1 - 1 file changed, 1 deletion(-) diff --git a/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java index 18ae2fcd46a..188e29d6fbf 100644 --- a/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java +++ b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java @@ -15,7 +15,6 @@ */ package org.openrewrite.groovy; -import org.jetbrains.annotations.NotNull; import org.openrewrite.*; import org.openrewrite.groovy.tree.G; import org.openrewrite.java.tree.J; From 4c8171d8c1674c627910e3f0fb5e598cd70142d1 Mon Sep 17 00:00:00 2001 From: sgartner03 <117266185+sgartner03@users.noreply.github.com> Date: Wed, 16 Oct 2024 14:12:15 +0200 Subject: [PATCH 06/17] Incoorporated UUID actions change Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../groovy/ChangeGroovyMethodInvocationParameter.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java index 188e29d6fbf..21cbb536ed2 100644 --- a/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java +++ b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java @@ -22,8 +22,6 @@ import org.openrewrite.java.tree.Space; import org.openrewrite.marker.Markers; -import java.util.UUID; - public class ChangeGroovyMethodInvocationParameter extends Recipe { @Option From b550ddfb8073580c016f43a99589f479accd8df0 Mon Sep 17 00:00:00 2001 From: sgartner03 <117266185+sgartner03@users.noreply.github.com> Date: Wed, 16 Oct 2024 14:12:31 +0200 Subject: [PATCH 07/17] incorporated actions ctx change Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../groovy/ChangeGroovyMethodInvocationParameter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java index 21cbb536ed2..dfa1459fcc0 100644 --- a/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java +++ b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java @@ -53,8 +53,8 @@ public ChangeGroovyMethodInvocationParameter(final String methodName, final Stri public TreeVisitor getVisitor() { return new GroovyIsoVisitor() { @Override - public J visitMapEntry(G.MapEntry mapEntry, ExecutionContext executionContext) { - mapEntry = (G.MapEntry) super.visitMapEntry(mapEntry, executionContext); + public J visitMapEntry(G.MapEntry mapEntry, ExecutionContext ctx) { + mapEntry = (G.MapEntry) super.visitMapEntry(mapEntry, ctx); if (!isInTargetMethod()) { return mapEntry; From a420fb9e5398294a6b5403950f45c182098ee10d Mon Sep 17 00:00:00 2001 From: sgartner03 <117266185+sgartner03@users.noreply.github.com> Date: Wed, 16 Oct 2024 14:13:03 +0200 Subject: [PATCH 08/17] incorporated actions method change Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../groovy/ChangeGroovyMethodInvocationParameter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java index dfa1459fcc0..f4db19aee16 100644 --- a/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java +++ b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java @@ -75,8 +75,8 @@ private boolean isInTargetMethod() { return getCursor().firstEnclosingOrThrow(J.MethodInvocation.class).getSimpleName().equals(methodName); } - private G.@NotNull MapEntry replaceValue(final G.MapEntry mapEntry) { - final char quote = extractQuoting(mapEntry); + private G. MapEntry replaceValue(final G.MapEntry mapEntry) { + 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)); return mapEntry.withValue(new J.Literal(UUID.randomUUID(), Space.SINGLE_SPACE, Markers.EMPTY, value, String.format("%c%s%c", quote, value, quote), null, JavaType.Primitive.String)); } From a0f861106816559d172a32b7adee42bfedbffa52 Mon Sep 17 00:00:00 2001 From: sgartner03 <117266185+sgartner03@users.noreply.github.com> Date: Wed, 16 Oct 2024 14:13:14 +0200 Subject: [PATCH 09/17] incorporated actions test change Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- ...ChangeGroovyMethodInvocationParameterTest.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java b/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java index 86d06c44c56..b3830e7b987 100644 --- a/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java +++ b/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java @@ -24,13 +24,14 @@ class ChangeGroovyMethodInvocationParameterTest implements RewriteTest { void givenGroovyFile_whenParamSet_thenChangeToNewValue() { rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("method", "param", "newValue")), //language=groovy - Assertions.groovy(""" - method( - param: 'oldValue' - ) - """, """ - method( - param: 'newValue' + Assertions.groovy( + """ + method( + param: 'oldValue' + ) + method( + param: 'newValue' + ) ) """)); } From 71fa8b949c1c01e395b2ee8d88e1ea832cda230a Mon Sep 17 00:00:00 2001 From: sgartner03 <117266185+sgartner03@users.noreply.github.com> Date: Wed, 16 Oct 2024 14:13:28 +0200 Subject: [PATCH 10/17] c Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../ChangeGroovyMethodInvocationParameterTest.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java b/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java index b3830e7b987..771ea79ddf2 100644 --- a/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java +++ b/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java @@ -40,10 +40,11 @@ void givenGroovyFile_whenParamSet_thenChangeToNewValue() { void givenGroovyFile_whenNewValueEqualsOldValue_thenChangeNothing() { rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("method", "param", "value")), //language=groovy - Assertions.groovy(""" - method( - param: 'value' - ) + Assertions.groovy( + """ + method( + param: 'value' + ) """)); } From fff88fbad3828f42589629e3b631636876a499cf Mon Sep 17 00:00:00 2001 From: sgartner03 <117266185+sgartner03@users.noreply.github.com> Date: Wed, 16 Oct 2024 14:13:39 +0200 Subject: [PATCH 11/17] incorporated actions test change Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- ...geGroovyMethodInvocationParameterTest.java | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java b/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java index 771ea79ddf2..86083b65d47 100644 --- a/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java +++ b/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java @@ -52,27 +52,29 @@ void givenGroovyFile_whenNewValueEqualsOldValue_thenChangeNothing() { void givenGroovyFile_whenSingleParamSetWithGString_thenChangeToNewValue() { rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("method", "param", "newValue")), //language=groovy - Assertions.groovy(""" - method( - param: "oldValue" - ) - """, """ - method( - param: "newValue" - ) - """)); - } - - @Test - void givenGroovyFile_whenTwoMethods_thenOnlyChangeOne() { - rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("method2", "param", "newValue")), Assertions.groovy(""" - method1( - param: "oldValue" - ) - method2( - param: "oldValue" - ) - """, """ + Assertions.groovy( + """ + method( + param: "oldValue" + ) + method( + param: "newValue" + ) + rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("method2", "param", "newValue")), Assertions.groovy( + """ + method1( + param: "oldValue" + ) + method2( + param: "oldValue" + ) + method1( + param: "oldValue" + ) + method2( + param: "newValue" + ) +} method1( param: "oldValue" ) From 2adc6ed1f12b2b6e80d8b614b6e0aa34b664da3c Mon Sep 17 00:00:00 2001 From: Simon Gartner Date: Wed, 16 Oct 2024 15:20:31 +0200 Subject: [PATCH 12/17] Fixed code after broken GH Actions suggestions --- ...ChangeGroovyMethodInvocationParameter.java | 2 +- ...geGroovyMethodInvocationParameterTest.java | 35 ++++++++++--------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java index f4db19aee16..2cbe6d23b0f 100644 --- a/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java +++ b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java @@ -76,8 +76,8 @@ private boolean isInTargetMethod() { } private G. MapEntry replaceValue(final G.MapEntry mapEntry) { + char quote = extractQuoting(mapEntry); 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)); - return mapEntry.withValue(new J.Literal(UUID.randomUUID(), Space.SINGLE_SPACE, Markers.EMPTY, value, String.format("%c%s%c", quote, value, quote), null, JavaType.Primitive.String)); } private char extractQuoting(final G.MapEntry mapEntry) { diff --git a/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java b/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java index 86083b65d47..5bee1f591bc 100644 --- a/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java +++ b/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java @@ -29,10 +29,10 @@ void givenGroovyFile_whenParamSet_thenChangeToNewValue() { method( param: 'oldValue' ) + """, """ method( param: 'newValue' ) - ) """)); } @@ -57,30 +57,31 @@ void givenGroovyFile_whenSingleParamSetWithGString_thenChangeToNewValue() { method( param: "oldValue" ) + """, """ method( param: "newValue" ) - rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("method2", "param", "newValue")), Assertions.groovy( + """)); + } + + @Test + void givenGroovyFile_whenTwoMethods_thenOnlyChangeOne() { + rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("method2", "param", "newValue")), + Assertions.groovy( """ - method1( + method1( param: "oldValue" - ) - method2( + ) + method2( param: "oldValue" - ) - method1( + ) + """, """ + method1( param: "oldValue" - ) - method2( + ) + method2( param: "newValue" - ) -} - method1( - param: "oldValue" - ) - method2( - param: "newValue" - ) + ) """)); } } \ No newline at end of file From c6180936dbcd6b486bfb9c0c89dec318bd74a8d9 Mon Sep 17 00:00:00 2001 From: Simon Gartner Date: Fri, 18 Oct 2024 13:58:57 +0200 Subject: [PATCH 13/17] Changed Recipe name and added tests --- ...ueOfNamedParameterInMethodInvocation.java} | 24 +++++---- ...ParameterValueOfMethodInvocationTest.java} | 50 +++++++++++++++++-- 2 files changed, 59 insertions(+), 15 deletions(-) rename rewrite-groovy/src/main/java/org/openrewrite/groovy/{ChangeGroovyMethodInvocationParameter.java => ChangeStringValueOfNamedParameterInMethodInvocation.java} (79%) rename rewrite-groovy/src/test/java/org/openrewrite/groovy/{ChangeGroovyMethodInvocationParameterTest.java => ChangeNamedStringParameterValueOfMethodInvocationTest.java} (52%) diff --git a/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeStringValueOfNamedParameterInMethodInvocation.java similarity index 79% rename from rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java rename to rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeStringValueOfNamedParameterInMethodInvocation.java index 2cbe6d23b0f..4102de34b67 100644 --- a/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameter.java +++ b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeStringValueOfNamedParameterInMethodInvocation.java @@ -22,7 +22,7 @@ import org.openrewrite.java.tree.Space; import org.openrewrite.marker.Markers; -public class ChangeGroovyMethodInvocationParameter extends Recipe { +public class ChangeStringValueOfNamedParameterInMethodInvocation extends Recipe { @Option public String methodName; @@ -33,7 +33,7 @@ public class ChangeGroovyMethodInvocationParameter extends Recipe { @Option public String value; - public ChangeGroovyMethodInvocationParameter(final String methodName, final String key, final String value) { + public ChangeStringValueOfNamedParameterInMethodInvocation(final String methodName, final String key, final String value) { this.methodName = methodName; this.key = key; this.value = value; @@ -41,12 +41,12 @@ public ChangeGroovyMethodInvocationParameter(final String methodName, final Stri @Override public @NlsRewrite.DisplayName String getDisplayName() { - return "Change a groovy parameter"; + 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."; + return "Changes the value of a given parameter in a given groovy method invocation, supporting Strings and GStrings."; } @Override @@ -60,23 +60,27 @@ public J visitMapEntry(G.MapEntry mapEntry, ExecutionContext ctx) { return mapEntry; } - if (mapEntry.getValue().toString().equals(value)) { + char quote = extractQuoting(mapEntry); + if (quote != '\'' && extractQuoting(mapEntry) != '"') { + return mapEntry; + } + + if (!mapEntry.getKey().toString().equals(key)) { return mapEntry; } - if (mapEntry.getKey().toString().equals(key)) { - return replaceValue(mapEntry); + if (mapEntry.getValue().toString().equals(value)) { + return mapEntry; } - return mapEntry; + return replaceValue(mapEntry, quote); } private boolean isInTargetMethod() { return getCursor().firstEnclosingOrThrow(J.MethodInvocation.class).getSimpleName().equals(methodName); } - private G. MapEntry replaceValue(final G.MapEntry mapEntry) { - char quote = extractQuoting(mapEntry); + 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)); } diff --git a/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java b/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeNamedStringParameterValueOfMethodInvocationTest.java similarity index 52% rename from rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java rename to rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeNamedStringParameterValueOfMethodInvocationTest.java index 5bee1f591bc..1c152a13fe2 100644 --- a/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeGroovyMethodInvocationParameterTest.java +++ b/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeNamedStringParameterValueOfMethodInvocationTest.java @@ -18,11 +18,11 @@ import org.junit.jupiter.api.Test; import org.openrewrite.test.RewriteTest; -class ChangeGroovyMethodInvocationParameterTest implements RewriteTest { +class ChangeNamedStringParameterValueOfMethodInvocationTest implements RewriteTest { @Test void givenGroovyFile_whenParamSet_thenChangeToNewValue() { - rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("method", "param", "newValue")), + rewriteRun(spec -> spec.recipe(new ChangeStringValueOfNamedParameterInMethodInvocation("method", "param", "newValue")), //language=groovy Assertions.groovy( """ @@ -38,7 +38,7 @@ void givenGroovyFile_whenParamSet_thenChangeToNewValue() { @Test void givenGroovyFile_whenNewValueEqualsOldValue_thenChangeNothing() { - rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("method", "param", "value")), + rewriteRun(spec -> spec.recipe(new ChangeStringValueOfNamedParameterInMethodInvocation("method", "param", "value")), //language=groovy Assertions.groovy( """ @@ -50,7 +50,7 @@ void givenGroovyFile_whenNewValueEqualsOldValue_thenChangeNothing() { @Test void givenGroovyFile_whenSingleParamSetWithGString_thenChangeToNewValue() { - rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("method", "param", "newValue")), + rewriteRun(spec -> spec.recipe(new ChangeStringValueOfNamedParameterInMethodInvocation("method", "param", "newValue")), //language=groovy Assertions.groovy( """ @@ -64,9 +64,29 @@ void givenGroovyFile_whenSingleParamSetWithGString_thenChangeToNewValue() { """)); } + @Test + void givenGroovyFile_whenMethodWithMultipleParameters_thenChangeOnlySelectedToNewValue() { + rewriteRun(spec -> spec.recipe(new ChangeStringValueOfNamedParameterInMethodInvocation("method", "param2", "newValue")), + //language=groovy + Assertions.groovy( + """ + method( + param1: "oldValue", + param2: "oldValue", + param3: "oldValue" + ) + """, """ + method( + param1: "oldValue", + param2: "newValue", + param3: "oldValue" + ) + """)); + } + @Test void givenGroovyFile_whenTwoMethods_thenOnlyChangeOne() { - rewriteRun(spec -> spec.recipe(new ChangeGroovyMethodInvocationParameter("method2", "param", "newValue")), + rewriteRun(spec -> spec.recipe(new ChangeStringValueOfNamedParameterInMethodInvocation("method2", "param", "newValue")), Assertions.groovy( """ method1( @@ -84,4 +104,24 @@ void givenGroovyFile_whenTwoMethods_thenOnlyChangeOne() { ) """)); } + + @Test + void givenGroovyFile_whenParameterWithOtherDatatype_thenDontChange() { + rewriteRun(spec -> spec.recipe(new ChangeStringValueOfNamedParameterInMethodInvocation("method", "param", "newValue")), + Assertions.groovy( + """ + method( + param: 1 + ) + """)); + } + + @Test + void givenGroovyFile_whenMethodWithoutParameters_thenDontChange() { + rewriteRun(spec -> spec.recipe(new ChangeStringValueOfNamedParameterInMethodInvocation("method2", "param", "newValue")), + Assertions.groovy( + """ + method() + """)); + } } \ No newline at end of file From 5d558f47d3f53506bf241f3ce8b9c7d10c72bd13 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 21 Oct 2024 15:18:47 +0200 Subject: [PATCH 14/17] Update test formatting and naming to match existing tests --- ...gParameterValueOfMethodInvocationTest.java | 168 ++++++++++-------- 1 file changed, 92 insertions(+), 76 deletions(-) diff --git a/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeNamedStringParameterValueOfMethodInvocationTest.java b/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeNamedStringParameterValueOfMethodInvocationTest.java index 1c152a13fe2..68cd4d7fa7b 100644 --- a/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeNamedStringParameterValueOfMethodInvocationTest.java +++ b/rewrite-groovy/src/test/java/org/openrewrite/groovy/ChangeNamedStringParameterValueOfMethodInvocationTest.java @@ -18,110 +18,126 @@ import org.junit.jupiter.api.Test; import org.openrewrite.test.RewriteTest; +import static org.openrewrite.groovy.Assertions.groovy; + class ChangeNamedStringParameterValueOfMethodInvocationTest implements RewriteTest { @Test - void givenGroovyFile_whenParamSet_thenChangeToNewValue() { + void whenParamSet_thenChangeToNewValue() { rewriteRun(spec -> spec.recipe(new ChangeStringValueOfNamedParameterInMethodInvocation("method", "param", "newValue")), //language=groovy - Assertions.groovy( - """ - method( - param: 'oldValue' - ) - """, """ - method( - param: 'newValue' - ) - """)); + groovy( + """ + method( + param: 'oldValue' + ) + """, """ + method( + param: 'newValue' + ) + """ + ) + ); } @Test - void givenGroovyFile_whenNewValueEqualsOldValue_thenChangeNothing() { + void noChangeWhenNewValueEqualsOldValue() { rewriteRun(spec -> spec.recipe(new ChangeStringValueOfNamedParameterInMethodInvocation("method", "param", "value")), //language=groovy - Assertions.groovy( - """ - method( - param: 'value' - ) - """)); + groovy( + """ + method( + param: 'value' + ) + """ + ) + ); } @Test - void givenGroovyFile_whenSingleParamSetWithGString_thenChangeToNewValue() { + void whenSingleParamSetWithGString_thenChangeToNewValue() { rewriteRun(spec -> spec.recipe(new ChangeStringValueOfNamedParameterInMethodInvocation("method", "param", "newValue")), //language=groovy - Assertions.groovy( - """ - method( - param: "oldValue" - ) - """, """ - method( - param: "newValue" - ) - """)); - } + groovy( + """ + method( + param: "oldValue" + ) + """, """ + method( + param: "newValue" + ) + """ + ) + ); + } @Test - void givenGroovyFile_whenMethodWithMultipleParameters_thenChangeOnlySelectedToNewValue() { + void whenMethodWithMultipleParameters_thenChangeOnlySelectedToNewValue() { rewriteRun(spec -> spec.recipe(new ChangeStringValueOfNamedParameterInMethodInvocation("method", "param2", "newValue")), //language=groovy - Assertions.groovy( - """ - method( - param1: "oldValue", - param2: "oldValue", - param3: "oldValue" - ) - """, """ - method( - param1: "oldValue", - param2: "newValue", - param3: "oldValue" - ) - """)); - } + groovy( + """ + method( + param1: "oldValue", + param2: "oldValue", + param3: "oldValue" + ) + """, """ + method( + param1: "oldValue", + param2: "newValue", + param3: "oldValue" + ) + """ + ) + ); + } @Test - void givenGroovyFile_whenTwoMethods_thenOnlyChangeOne() { + void whenTwoMethods_thenOnlyChangeOne() { rewriteRun(spec -> spec.recipe(new ChangeStringValueOfNamedParameterInMethodInvocation("method2", "param", "newValue")), - Assertions.groovy( - """ - method1( - param: "oldValue" - ) - method2( - param: "oldValue" - ) - """, """ - method1( - param: "oldValue" - ) - method2( - param: "newValue" - ) - """)); + groovy( + """ + method1( + param: "oldValue" + ) + method2( + param: "oldValue" + ) + """, """ + method1( + param: "oldValue" + ) + method2( + param: "newValue" + ) + """ + ) + ); } @Test - void givenGroovyFile_whenParameterWithOtherDatatype_thenDontChange() { + void noChangeWhenParameterWithOtherDatatype() { rewriteRun(spec -> spec.recipe(new ChangeStringValueOfNamedParameterInMethodInvocation("method", "param", "newValue")), - Assertions.groovy( - """ - method( - param: 1 - ) - """)); + groovy( + """ + method( + param: 1 + ) + """ + ) + ); } @Test - void givenGroovyFile_whenMethodWithoutParameters_thenDontChange() { + void noChangeWhenMethodWithoutParameters() { rewriteRun(spec -> spec.recipe(new ChangeStringValueOfNamedParameterInMethodInvocation("method2", "param", "newValue")), - Assertions.groovy( - """ - method() - """)); + groovy( + """ + method() + """ + ) + ); } -} \ No newline at end of file +} From 1b6c39844cdd69e3a777abd6adeff89cbde45a04 Mon Sep 17 00:00:00 2001 From: Simon Gartner Date: Mon, 21 Oct 2024 17:46:55 +0200 Subject: [PATCH 15/17] Removed extractQuoting --- ...lueOfNamedParameterInMethodInvocation.java | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeStringValueOfNamedParameterInMethodInvocation.java b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeStringValueOfNamedParameterInMethodInvocation.java index 4102de34b67..c165a1b4e91 100644 --- a/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeStringValueOfNamedParameterInMethodInvocation.java +++ b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeStringValueOfNamedParameterInMethodInvocation.java @@ -15,8 +15,10 @@ */ package org.openrewrite.groovy; +import groovy.lang.GString; import org.openrewrite.*; import org.openrewrite.groovy.tree.G; +import org.openrewrite.java.tree.Expression; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaType; import org.openrewrite.java.tree.Space; @@ -60,8 +62,7 @@ public J visitMapEntry(G.MapEntry mapEntry, ExecutionContext ctx) { return mapEntry; } - char quote = extractQuoting(mapEntry); - if (quote != '\'' && extractQuoting(mapEntry) != '"') { + if (!((J.Literal) mapEntry.getValue()).getType().equals(JavaType.Primitive.String)) { return mapEntry; } @@ -73,20 +74,25 @@ public J visitMapEntry(G.MapEntry mapEntry, ExecutionContext ctx) { return mapEntry; } - return replaceValue(mapEntry, quote); + return replaceValue(mapEntry); } private boolean isInTargetMethod() { return getCursor().firstEnclosingOrThrow(J.MethodInvocation.class).getSimpleName().equals(methodName); } - 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)); - } + private G. MapEntry replaceValue(final G.MapEntry mapEntry) { + J.Literal mapValue = (J.Literal) mapEntry.getValue(); + String oldValue = (String) mapValue.getValue(); + + mapValue = mapValue.withValue(value); - private char extractQuoting(final G.MapEntry mapEntry) { - return mapEntry.getValue().printTrimmed(getCursor()).charAt(0); + String newValueSource = mapValue.getValueSource().replace(oldValue, value); + mapValue = mapValue.withValueSource(newValueSource); + + return mapEntry.withValue(mapValue); } + }; } } From edd9a590eebb4ab69050462dfcfd8058dfb21bd4 Mon Sep 17 00:00:00 2001 From: Simon Gartner Date: Mon, 21 Oct 2024 18:04:44 +0200 Subject: [PATCH 16/17] Refactoring --- ...lueOfNamedParameterInMethodInvocation.java | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeStringValueOfNamedParameterInMethodInvocation.java b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeStringValueOfNamedParameterInMethodInvocation.java index c165a1b4e91..7b5299dad19 100644 --- a/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeStringValueOfNamedParameterInMethodInvocation.java +++ b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeStringValueOfNamedParameterInMethodInvocation.java @@ -15,14 +15,11 @@ */ package org.openrewrite.groovy; -import groovy.lang.GString; import org.openrewrite.*; import org.openrewrite.groovy.tree.G; -import org.openrewrite.java.tree.Expression; +import org.openrewrite.java.MethodMatcher; 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 { @@ -62,7 +59,7 @@ public J visitMapEntry(G.MapEntry mapEntry, ExecutionContext ctx) { return mapEntry; } - if (!((J.Literal) mapEntry.getValue()).getType().equals(JavaType.Primitive.String)) { + if (!hasStringValue(mapEntry)) { return mapEntry; } @@ -82,17 +79,26 @@ private boolean isInTargetMethod() { } private G. MapEntry replaceValue(final G.MapEntry mapEntry) { - J.Literal mapValue = (J.Literal) mapEntry.getValue(); - String oldValue = (String) mapValue.getValue(); + J.Literal entryValue = (J.Literal) mapEntry.getValue(); - mapValue = mapValue.withValue(value); + J.Literal newEntryValue = entryValue + .withValue(value) + .withValueSource(buildNewValueSource(entryValue)); - String newValueSource = mapValue.getValueSource().replace(oldValue, value); - mapValue = mapValue.withValueSource(newValueSource); + return mapEntry.withValue(newEntryValue); + } - return mapEntry.withValue(mapValue); + private String buildNewValueSource(J.Literal entryValue) { + String oldValue = entryValue.getValue() == null ? "" : entryValue.getValue().toString(); + return entryValue.getValueSource().replace(oldValue, value); } }; } + + private static boolean hasStringValue(G.MapEntry mapEntry) { + return ((J.Literal) mapEntry.getValue()).getType().equals(JavaType.Primitive.String); + } + + } From d7b1e1cf9ac8b538538abf8b66151f5dfb188d0d Mon Sep 17 00:00:00 2001 From: Simon Gartner Date: Mon, 21 Oct 2024 18:12:17 +0200 Subject: [PATCH 17/17] Added null check for warning --- ...eStringValueOfNamedParameterInMethodInvocation.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeStringValueOfNamedParameterInMethodInvocation.java b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeStringValueOfNamedParameterInMethodInvocation.java index 7b5299dad19..c2563c7ca6d 100644 --- a/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeStringValueOfNamedParameterInMethodInvocation.java +++ b/rewrite-groovy/src/main/java/org/openrewrite/groovy/ChangeStringValueOfNamedParameterInMethodInvocation.java @@ -15,9 +15,9 @@ */ package org.openrewrite.groovy; +import org.jspecify.annotations.Nullable; import org.openrewrite.*; import org.openrewrite.groovy.tree.G; -import org.openrewrite.java.MethodMatcher; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaType; @@ -88,11 +88,15 @@ private G. MapEntry replaceValue(final G.MapEntry mapEntry) { return mapEntry.withValue(newEntryValue); } - private String buildNewValueSource(J.Literal entryValue) { + 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); } - }; }