Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
- Remove `Preconditions.or` for single element
- Collapse `if` statements for select type check
- Inline `builder_string`, as variable name does not conform to Java standards & limited use & scope allows for inlining
- Remove unnecessary elements from test cases to prevent confusion
- Remove public visibility from test class
  • Loading branch information
timtebeek committed Jun 26, 2023
1 parent f50c032 commit 1d9871f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,50 +32,45 @@

public class RemoveHashCodeCallsFromArrayInstances extends Recipe {
private static final MethodMatcher HASHCODE_MATCHER = new MethodMatcher("java.lang.Object hashCode()");

@Override
public String getDisplayName() {
return "`hashCode()` should not be called on array instances";
}

@Override
public String getDescription() {
return "Removes `hashCode()` calls on arrays and replaces it with `Arrays.hashCode()` because the results from `hashCode()`" +
" not helpful.";
return "Replace `hashCode()` calls on arrays with `Arrays.hashCode()` because the results from `hashCode()`" +
" are not helpful.";
}

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

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(Preconditions.or(
new UsesMethod<>(HASHCODE_MATCHER)
), new RemoveHashCodeCallsFromArrayInstancesVisitor());
return Preconditions.check(new UsesMethod<>(HASHCODE_MATCHER), new RemoveHashCodeCallsFromArrayInstancesVisitor());
}

private static class RemoveHashCodeCallsFromArrayInstancesVisitor extends JavaIsoVisitor<ExecutionContext> {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation mi, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(mi, ctx);

if (HASHCODE_MATCHER.matches(m)) {
String builder_string = "Arrays.hashCode(#{anyArray(java.lang.Object)})";
Expression select = m.getSelect();
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInvocation, ExecutionContext ctx) {
J.MethodInvocation mi = super.visitMethodInvocation(methodInvocation, ctx);

if (select != null) {
if (!(select.getType() instanceof JavaType.Array)) {
return m;
}
if (HASHCODE_MATCHER.matches(mi)) {
Expression select = mi.getSelect();
if (select != null && select.getType() instanceof JavaType.Array) {
maybeAddImport("java.util.Arrays");
return JavaTemplate.builder(builder_string)
return JavaTemplate.builder("Arrays.hashCode(#{anyArray(java.lang.Object)})")
.imports("java.util.Arrays")
.build()
.apply(getCursor(), m.getCoordinates().replace(), select);
.apply(getCursor(), mi.getCoordinates().replace(), select);
}
}

return m;
return mi;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,16 @@
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.Issue;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

@SuppressWarnings("ArrayHashCode")
public class RemoveHashCodeCallsFromArrayInstancesTest implements RewriteTest {
class RemoveHashCodeCallsFromArrayInstancesTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec
.recipe(new RemoveHashCodeCallsFromArrayInstances());
spec.recipe(new RemoveHashCodeCallsFromArrayInstances());
}

@Test
Expand All @@ -38,74 +36,66 @@ public void defaults(RecipeSpec spec) {
void replaceHashCodeCalls() {
//language=java
rewriteRun(
java(
"""
class SomeClass {
public static void main(String[] args) {
int argHash = args.hashCode();
}
java("""
class SomeClass {
public static void main(String[] args) {
int argHash = args.hashCode();
}
""",
"""
import java.util.Arrays;
class SomeClass {
public static void main(String[] args) {
int argHash = Arrays.hashCode(args);
}
}
""", """
import java.util.Arrays;
class SomeClass {
public static void main(String[] args) {
int argHash = Arrays.hashCode(args);
}
"""
)
}
""")
);
}

@Test
void selectIsAMethod() {
//language=java
rewriteRun(
java(
"""
class SomeClass {
public static void main(String[] args) {
int hashCode = getArr().hashCode();
}
public int[] getArr() {
return new int[]{1, 2, 3};
}
java("""
class SomeClass {
void foo() {
int hashCode = getArr().hashCode();
}
public int[] getArr() {
return new int[]{1, 2, 3};
}
}
""", """
import java.util.Arrays;
class SomeClass {
void foo() {
int hashCode = Arrays.hashCode(getArr());
}
""",
"""
import java.util.Arrays;
class SomeClass {
public static void main(String[] args) {
int hashCode = Arrays.hashCode(getArr());
}
public int[] getArr() {
return new int[]{1, 2, 3};
}
public int[] getArr() {
return new int[]{1, 2, 3};
}
"""
)
}
""")
);
}

@Test
void onlyRunOnArrayInstances() {
//language=java
rewriteRun(
java(
"""
class SomeClass {
public static void main(String[] args) {
int name = "bill";
int hashCode = name.hashCode();
}
java("""
class SomeClass {
void foo() {
String name = "bill";
int hashCode = name.hashCode();
}
"""
)
}
""")
);
}
}

0 comments on commit 1d9871f

Please sign in to comment.