diff --git a/src/main/java/org/openrewrite/staticanalysis/RemoveHashCodeCallsFromArrayInstances.java b/src/main/java/org/openrewrite/staticanalysis/RemoveHashCodeCallsFromArrayInstances.java new file mode 100644 index 0000000000..fa01557bde --- /dev/null +++ b/src/main/java/org/openrewrite/staticanalysis/RemoveHashCodeCallsFromArrayInstances.java @@ -0,0 +1,76 @@ +/* + * Copyright 2023 the original author or authors. + *
+ * 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 + *
+ * https://www.apache.org/licenses/LICENSE-2.0 + *
+ * 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
+ * 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
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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();
+ }
+ }
+ """)
+ );
+ }
+}