diff --git a/src/main/java/org/openrewrite/staticanalysis/ReplaceClassIsInstanceWithInstanceof.java b/src/main/java/org/openrewrite/staticanalysis/ReplaceClassIsInstanceWithInstanceof.java new file mode 100644 index 0000000000..4ed0d4bca8 --- /dev/null +++ b/src/main/java/org/openrewrite/staticanalysis/ReplaceClassIsInstanceWithInstanceof.java @@ -0,0 +1,86 @@ +/* + * Copyright 2024 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.jspecify.annotations.Nullable;
+import org.openrewrite.ExecutionContext;
+import org.openrewrite.Recipe;
+import org.openrewrite.java.JavaTemplate;
+import org.openrewrite.java.JavaVisitor;
+import org.openrewrite.java.MethodMatcher;
+import org.openrewrite.java.tree.Expression;
+import org.openrewrite.java.tree.J;
+import org.openrewrite.java.tree.J.FieldAccess;
+import org.openrewrite.java.tree.J.Identifier;
+import org.openrewrite.java.tree.J.MethodInvocation;
+import org.openrewrite.java.tree.JavaType;
+
+import java.util.Collections;
+import java.util.Set;
+
+public class ReplaceClassIsInstanceWithInstanceof extends Recipe {
+
+ @Override
+ public String getDisplayName() {
+ return "Replace `A.class.isInstance(a)` with `a instanceof A`";
+ }
+
+ @Override
+ public String getDescription() {
+ return "There should be no `A.class.isInstance(a)`, it should be replaced by `a instanceof A`.";
+ }
+
+ @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.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+import static org.openrewrite.java.Assertions.javaVersion;
+
+@SuppressWarnings({"RedundantClassCall", "ConstantValue", "UnusedAssignment"})
+class ReplaceClassIsInstanceWithInstanceofTest implements RewriteTest {
+
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.recipe(new ReplaceClassIsInstanceWithInstanceof());
+ }
+
+ @Test
+ @DocumentExample
+ void changeInstanceOf() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ class A {
+ void foo() {
+ String s = "";
+ boolean result = String.class.isInstance(s);
+ result = Integer.class.isInstance(s);
+ }
+ }
+ """,
+ """
+ class A {
+ void foo() {
+ String s = "";
+ boolean result = s instanceof String;
+ result = s instanceof Integer;
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void doNotChangeWhenAlreadyInstanceOf() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ class A {
+ boolean foo() {
+ String s = "";
+ return s instanceof String;
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void doNotChangeWhenVariable() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ class A {
+ void foo(Class> clazz) {
+ String s = "";
+ boolean result = clazz.isInstance(s);
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void doNotChangeInstanceOfWithVariable() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ class A {
+ String foo(Object obj) {
+ if (obj instanceof String s) {
+ return s;
+ }
+ return null;
+ }
+ }
+ """,
+ spec -> spec.markers(javaVersion(17))
+ )
+ );
+ }
+
+}