Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,8 +58,11 @@ public Duration getEstimatedEffortPerOccurrence() {
public TreeVisitor<?, ExecutionContext> 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<?, ExecutionContext> preconditions = Preconditions.and(Preconditions.not(new KotlinFileChecker<>()),
new UsesType<>("java.lang.String", false));
TreeVisitor<?, ExecutionContext> 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<ExecutionContext>() {
private final JavaType.FullyQualified TYPE_STRING = TypeUtils.asFullyQualified(JavaType.buildType("java.lang.String"));
private final JavaType TYPE_OBJECT = JavaType.buildType("java.lang.Object");
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2023 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.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<P> extends TreeVisitor<Tree, P> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably make this a bit more generic, so that it can cover even more ground.

@Value
public class IsInstanceOf<P> extends TreeVisitor<Tree, P> {
    Class<? extends Tree> treeType;

    @Nullable
    public Tree visit(@Nullable Tree tree, P p) {
        if (tree != null && treeType.isInstance(tree.getClass()) {
            return SearchResult.found(tree);
        }
        return tree;
    }
}

Then this may be a candidate for inclusion down in rewrite-core.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I quite like this suggestion, thanks! Especially eyeing extended support coming up for Groovy, Kotlin, Typescript, Python, ... would help to handle this once in /rewrite and not repeat ourselves per module with such checks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kunli2 Since you're a little more involved with new language development and these exclusions, where would you see this fit best? And would you be willing to take this on?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logged as an improvement here, since I'd like to see this fix roll out before we get a response & rework through.
#144
Thanks for the suggestion!

@Override
public @Nullable Tree visit(@Nullable Tree tree, P p) {
if (tree instanceof G.CompilationUnit) {
return SearchResult.found(tree);
}
return tree;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2023 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.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() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a similar separate class to test Kotlin; Was wondering if we shouldn't just collapse these into @Nested test classes in StringLiteralEqualityTest in package org.openrewrite.staticanalysis.

rewriteRun(
groovy(
"""
def foo(String token) {
if (token == "Foo" ) {
}
}
"""
)
);
}
}