Skip to content

Commit

Permalink
added RSPEC tags, switched assert statement for if statement and upda…
Browse files Browse the repository at this point in the history
…ted description
  • Loading branch information
AlekSimpson committed Jun 22, 2023
1 parent c4aaa4d commit f50c032
Showing 1 changed file with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;

import java.util.Collections;
import java.util.Set;

public class RemoveHashCodeCallsFromArrayInstances extends Recipe {
private static final MethodMatcher HASHCODE_MATCHER = new MethodMatcher("java.lang.Object hashCode()");
@Override
Expand All @@ -37,7 +40,12 @@ public String getDisplayName() {
@Override
public String getDescription() {
return "Removes `hashCode()` calls on arrays and replaces it with `Arrays.hashCode()` because the results from `hashCode()`" +
" are largely useless.";
" not helpful.";
}

@Override
public Set<String> getTags() {
return Collections.singleton("RSPEC-2116");
}

public TreeVisitor<?, ExecutionContext> getVisitor() {
Expand All @@ -54,19 +62,17 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation mi, Execution
if (HASHCODE_MATCHER.matches(m)) {
String builder_string = "Arrays.hashCode(#{anyArray(java.lang.Object)})";
Expression select = m.getSelect();
assert select != null;

if (!(select.getType() instanceof JavaType.Array)) {
return m;
if (select != null) {
if (!(select.getType() instanceof JavaType.Array)) {
return m;
}
maybeAddImport("java.util.Arrays");
return JavaTemplate.builder(builder_string)
.imports("java.util.Arrays")
.build()
.apply(getCursor(), m.getCoordinates().replace(), select);
}

J.MethodInvocation invocation = JavaTemplate.builder(builder_string)
.imports("java.util.Arrays")
.build()
.apply(getCursor(), m.getCoordinates().replace(), select);
maybeAddImport("java.util.Arrays");

return invocation;
}

return m;
Expand Down

0 comments on commit f50c032

Please sign in to comment.