diff --git a/build.gradle.kts b/build.gradle.kts index 18c1c1ddf8..314a0b3bb5 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -15,6 +15,7 @@ dependencies { implementation(platform("org.openrewrite:rewrite-bom:${rewriteVersion}")) implementation("org.openrewrite:rewrite-java") + implementation("org.openrewrite:rewrite-groovy:${rewriteVersion}") implementation("org.openrewrite:rewrite-kotlin:${rewriteVersion}") implementation("org.openrewrite.meta:rewrite-analysis:${rewriteVersion}") implementation("org.apache.commons:commons-text:latest.release") diff --git a/src/main/java/org/openrewrite/staticanalysis/StringLiteralEquality.java b/src/main/java/org/openrewrite/staticanalysis/StringLiteralEquality.java index 483fb3fed0..0c86c7b9b7 100644 --- a/src/main/java/org/openrewrite/staticanalysis/StringLiteralEquality.java +++ b/src/main/java/org/openrewrite/staticanalysis/StringLiteralEquality.java @@ -20,6 +20,7 @@ import org.openrewrite.java.search.UsesType; import org.openrewrite.java.tree.*; import org.openrewrite.marker.Markers; +import org.openrewrite.staticanalysis.groovy.GroovyFileChecker; import org.openrewrite.staticanalysis.kotlin.KotlinFileChecker; import java.time.Duration; @@ -57,8 +58,11 @@ public Duration getEstimatedEffortPerOccurrence() { public TreeVisitor getVisitor() { // Don't change for Kotlin because In Kotlin, `==` means structural equality, so it's redundant to call equals(). // see https://rules.sonarsource.com/kotlin/RSPEC-6519/ - TreeVisitor preconditions = Preconditions.and(Preconditions.not(new KotlinFileChecker<>()), - new UsesType<>("java.lang.String", false)); + TreeVisitor preconditions = Preconditions.and( + Preconditions.and( + Preconditions.not(new KotlinFileChecker<>()), + Preconditions.not(new GroovyFileChecker<>())), + new UsesType<>("java.lang.String", false)); return Preconditions.check(preconditions, new JavaVisitor() { private final JavaType.FullyQualified TYPE_STRING = TypeUtils.asFullyQualified(JavaType.buildType("java.lang.String")); private final JavaType TYPE_OBJECT = JavaType.buildType("java.lang.Object"); @@ -79,7 +83,7 @@ private J.MethodInvocation asEqualsMethodInvocation(J.Binary binary) { Markers.EMPTY, new JRightPadded<>(binary.getLeft().withPrefix(Space.EMPTY), Space.EMPTY, Markers.EMPTY), null, - new J.Identifier(Tree.randomId(), Space.EMPTY, Markers.EMPTY, emptyList(),"equals", JavaType.Primitive.Boolean, null), + new J.Identifier(Tree.randomId(), Space.EMPTY, Markers.EMPTY, emptyList(), "equals", JavaType.Primitive.Boolean, null), JContainer.build(singletonList(new JRightPadded<>(binary.getRight().withPrefix(Space.EMPTY), Space.EMPTY, Markers.EMPTY))), new JavaType.Method( null, diff --git a/src/main/java/org/openrewrite/staticanalysis/groovy/GroovyFileChecker.java b/src/main/java/org/openrewrite/staticanalysis/groovy/GroovyFileChecker.java new file mode 100644 index 0000000000..2476b20ac0 --- /dev/null +++ b/src/main/java/org/openrewrite/staticanalysis/groovy/GroovyFileChecker.java @@ -0,0 +1,35 @@ +/* + * Copyright 2023 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.staticanalysis.groovy; + +import org.openrewrite.Tree; +import org.openrewrite.TreeVisitor; +import org.openrewrite.groovy.tree.G; +import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.marker.SearchResult; + +/** + * Add a search marker if vising a Groovy file + */ +public class GroovyFileChecker

extends TreeVisitor { + @Override + public @Nullable Tree visit(@Nullable Tree tree, P p) { + if (tree instanceof G.CompilationUnit) { + return SearchResult.found(tree); + } + return tree; + } +} diff --git a/src/test/java/org/openrewrite/staticanalysis/groovy/StringLiteralEqualityTest.java b/src/test/java/org/openrewrite/staticanalysis/groovy/StringLiteralEqualityTest.java new file mode 100644 index 0000000000..97f0a7e8cf --- /dev/null +++ b/src/test/java/org/openrewrite/staticanalysis/groovy/StringLiteralEqualityTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 2023 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.staticanalysis.groovy; + +import org.junit.jupiter.api.Test; +import org.openrewrite.staticanalysis.StringLiteralEquality; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.groovy.Assertions.groovy; + +class StringLiteralEqualityTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(new StringLiteralEquality()); + } + + // Don't change for Groovy because in Groovy, `==` means structural equality and it's redundant to call equals(). + @Test + void doNotChangeForGroovy() { + rewriteRun( + groovy( + """ + def foo(String token) { + if (token == "Foo" ) { + } + } + """ + ) + ); + } +}