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

Remove hashCode calls on arrays #128

Merged
merged 4 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
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
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();
}
}
""")
);
}
}