Skip to content

Commit

Permalink
AtomicPrimitiveEqualsUsesGet: Performance
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Jan 17, 2024
1 parent 8c21316 commit 62c22af
Showing 1 changed file with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,19 @@
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class AtomicPrimitiveEqualsUsesGet extends Recipe {

public static final Set<String> ATOMIC_PRIMITIVE_TYPES = new HashSet<>(Arrays.asList(
"java.util.concurrent.atomic.AtomicBoolean",
"java.util.concurrent.atomic.AtomicInteger",
"java.util.concurrent.atomic.AtomicLong"
));

@Override
public String getDisplayName() {
return "Atomic Boolean, Integer, and Long equality checks compare their values";
Expand All @@ -51,9 +59,9 @@ public Set<String> getTags() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(Preconditions.or(
new UsesType<>("java.util.concurrent.atomic.AtomicBoolean", true),
new UsesType<>("java.util.concurrent.atomic.AtomicInteger", true),
new UsesType<>("java.util.concurrent.atomic.AtomicLong", true)
new UsesType<>("java.util.concurrent.atomic.AtomicBoolean", false),
new UsesType<>("java.util.concurrent.atomic.AtomicInteger", false),
new UsesType<>("java.util.concurrent.atomic.AtomicLong", false)
), new JavaVisitor<ExecutionContext>() {
private final MethodMatcher aiMethodMatcher = new MethodMatcher("java.lang.Object equals(java.lang.Object)");

Expand All @@ -66,22 +74,17 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
if (fqt != null) {
String templateString = "#{any(" + fqt.getFullyQualifiedName() + ")}.get() == #{any(" + fqt.getFullyQualifiedName() + ")}.get()";
return JavaTemplate.builder(templateString)
.imports(fqt.getFullyQualifiedName()).build()
.imports(fqt.getFullyQualifiedName())
.build()
.apply(updateCursor(mi), mi.getCoordinates().replace(), mi.getSelect(), mi.getArguments().get(0));
}
}
return mi;
}

private boolean isAtomicEqualsType(@Nullable JavaType type) {
if (type != null) {
for (String fqn : new String[]{"java.util.concurrent.atomic.AtomicBoolean", "java.util.concurrent.atomic.AtomicInteger", "java.util.concurrent.atomic.AtomicLong"}) {
if (TypeUtils.isOfClassType(type, fqn)) {
return true;
}
}
}
return false;
return type instanceof JavaType.FullyQualified &&
ATOMIC_PRIMITIVE_TYPES.contains(((JavaType.FullyQualified) type).getFullyQualifiedName());
}
});
}
Expand Down

0 comments on commit 62c22af

Please sign in to comment.