Skip to content

Commit

Permalink
Remove hashCode calls on arrays (#128)
Browse files Browse the repository at this point in the history
* added first tests for recipe. All tests pass right now

* updated recipe displayName and description

* added RSPEC tags, switched assert statement for if statement and updated description

* Polish

- 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

---------

Co-authored-by: Tim te Beek <tim@moderne.io>
  • Loading branch information
AlekSimpson and timtebeek committed Jun 26, 2023
1 parent 743d359 commit a6e524c
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.staticanalysis;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.Expression;
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
public String getDisplayName() {
return "`hashCode()` should not be called on array instances";
}

@Override
public String getDescription() {
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(new UsesMethod<>(HASHCODE_MATCHER), new RemoveHashCodeCallsFromArrayInstancesVisitor());
}

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

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

return mi;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.staticanalysis;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.Issue;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

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

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

@Test
@DocumentExample
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/44")
void replaceHashCodeCalls() {
//language=java
rewriteRun(
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);
}
}
""")
);
}

@Test
void selectIsAMethod() {
//language=java
rewriteRun(
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());
}
public int[] getArr() {
return new int[]{1, 2, 3};
}
}
""")
);
}

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

0 comments on commit a6e524c

Please sign in to comment.