Skip to content
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

Refactor mutable field in EqualsMethodUsage #614

Closed
wants to merge 2 commits into from
Closed
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
59 changes: 31 additions & 28 deletions src/main/java/org/openrewrite/kotlin/cleanup/EqualsMethodUsage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,9 +41,6 @@
@EqualsAndHashCode(callSuper = false)
public class EqualsMethodUsage extends Recipe {

@SuppressWarnings("ALL")
private static J.@Nullable Binary equalsBinaryTemplate;

@Override
public String getDisplayName() {
return "Structural equality tests should use `==` or `!=`";
Expand Down Expand Up @@ -69,6 +69,9 @@ public Duration getEstimatedEffortPerOccurrence() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new KotlinVisitor<ExecutionContext>() {

private J.@Nullable Binary equalsBinaryTemplate;

@Override
public J visitUnary(J.Unary unary, ExecutionContext ctx) {
unary = (J.Unary) super.visitUnary(unary, ctx);
Expand Down Expand Up @@ -111,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<AtomicReference<J.Binary>>() {
@Override
public J visitBinary(J.Binary binary, AtomicReference<J.Binary> 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<AtomicReference<J.Binary>>() {
@Override
public J visitBinary(J.Binary binary, AtomicReference<J.Binary> target) {
target.set(binary);
return binary;
}
}.reduce(kcu, new AtomicReference<J.Binary>()).get();
}

Space rhsPrefix = right.getPrefix();
if (rhsPrefix.getWhitespace().isEmpty()) {
rhsPrefix = rhsPrefix.withWhitespace(" ");
}
}.reduce(kcu, new AtomicReference<J.Binary>()).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));
}
};
}
}