diff --git a/src/main/java/org/openrewrite/kotlin/cleanup/EqualsMethodUsage.java b/src/main/java/org/openrewrite/kotlin/cleanup/EqualsMethodUsage.java index 888fc638..19bca510 100644 --- a/src/main/java/org/openrewrite/kotlin/cleanup/EqualsMethodUsage.java +++ b/src/main/java/org/openrewrite/kotlin/cleanup/EqualsMethodUsage.java @@ -18,7 +18,10 @@ import lombok.EqualsAndHashCode; import lombok.Value; import org.jspecify.annotations.Nullable; -import org.openrewrite.*; +import org.openrewrite.Cursor; +import org.openrewrite.ExecutionContext; +import org.openrewrite.Recipe; +import org.openrewrite.TreeVisitor; import org.openrewrite.java.tree.Expression; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.Space; @@ -38,8 +41,6 @@ @EqualsAndHashCode(callSuper = false) public class EqualsMethodUsage extends Recipe { - private static J.@Nullable Binary equalsBinaryTemplate; - @Override public String getDisplayName() { return "Structural equality tests should use `==` or `!=`"; @@ -68,6 +69,9 @@ public Duration getEstimatedEffortPerOccurrence() { @Override public TreeVisitor getVisitor() { return new KotlinVisitor() { + + private J.@Nullable Binary equalsBinaryTemplate; + @Override public J visitUnary(J.Unary unary, ExecutionContext ctx) { unary = (J.Unary) super.visitUnary(unary, ctx); @@ -110,31 +114,31 @@ public J visitMethodInvocation(J.MethodInvocation method, } return method; } - }; - } - @SuppressWarnings("all") - private static J.Binary buildEqualsBinary(Expression left, Expression right) { - if (equalsBinaryTemplate == null) { - K.CompilationUnit kcu = KotlinParser.builder().build() - .parse("fun method(a : String, b : String) {val isSame = a == b}") - .map(K.CompilationUnit.class::cast) - .findFirst() - .get(); - - equalsBinaryTemplate = new KotlinVisitor>() { - @Override - public J visitBinary(J.Binary binary, AtomicReference target) { - target.set(binary); - return binary; + @SuppressWarnings("all") + private J.Binary buildEqualsBinary(Expression left, Expression right) { + if (equalsBinaryTemplate == null) { + K.CompilationUnit kcu = KotlinParser.builder().build() + .parse("fun method(a : String, b : String) {val isSame = a == b}") + .map(K.CompilationUnit.class::cast) + .findFirst() + .get(); + + equalsBinaryTemplate = new KotlinVisitor>() { + @Override + public J visitBinary(J.Binary binary, AtomicReference target) { + target.set(binary); + return binary; + } + }.reduce(kcu, new AtomicReference()).get(); + } + + Space rhsPrefix = right.getPrefix(); + if (rhsPrefix.getWhitespace().isEmpty()) { + rhsPrefix = rhsPrefix.withWhitespace(" "); } - }.reduce(kcu, new AtomicReference()).get(); - } - - Space rhsPrefix = right.getPrefix(); - if (rhsPrefix.getWhitespace().isEmpty()) { - rhsPrefix = rhsPrefix.withWhitespace(" "); - } - return equalsBinaryTemplate.withLeft(left.withPrefix(left.getPrefix())).withRight(right.withPrefix(rhsPrefix)); + return equalsBinaryTemplate.withLeft(left.withPrefix(left.getPrefix())).withRight(right.withPrefix(rhsPrefix)); + } + }; } }