diff --git a/rewrite-java/src/main/java/org/openrewrite/java/ChangeMethodName.java b/rewrite-java/src/main/java/org/openrewrite/java/ChangeMethodName.java index c426399c205..a2d5d41b958 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/ChangeMethodName.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/ChangeMethodName.java @@ -23,8 +23,6 @@ import org.openrewrite.java.search.UsesMethod; import org.openrewrite.java.tree.*; -import static java.util.Objects.requireNonNull; - @Value @EqualsAndHashCode(callSuper = true) public class ChangeMethodName extends Recipe { @@ -73,16 +71,19 @@ public TreeVisitor getVisitor() { @Override public J visit(@Nullable Tree tree, ExecutionContext ctx) { if (tree instanceof JavaSourceFile) { - JavaSourceFile cu = (JavaSourceFile) requireNonNull(tree); + JavaSourceFile cu = (JavaSourceFile) tree; if (Boolean.TRUE.equals(ignoreDefinition)) { J j = new DeclaresMethod<>(methodPattern, matchOverrides).visitNonNull(cu, ctx); if (cu != j) { return cu; } + } else { + cu = (JavaSourceFile) new DeclaresMethod<>(methodPattern, matchOverrides).visitNonNull(cu, ctx); + if (cu != tree) { + return cu; + } } - cu = (JavaSourceFile) new UsesMethod<>(methodPattern, matchOverrides).visitNonNull(cu, ctx); - cu = (JavaSourceFile) new DeclaresMethod<>(methodPattern, matchOverrides).visitNonNull(cu, ctx); - return cu; + return new UsesMethod<>(methodPattern, matchOverrides).visitNonNull(cu, ctx); } return super.visit(tree, ctx); } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/search/DeclaresMethod.java b/rewrite-java/src/main/java/org/openrewrite/java/search/DeclaresMethod.java index afc162b4324..de28de0b31d 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/search/DeclaresMethod.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/search/DeclaresMethod.java @@ -24,8 +24,6 @@ import org.openrewrite.java.tree.JavaType; import org.openrewrite.marker.SearchResult; -import static java.util.Objects.requireNonNull; - public class DeclaresMethod

extends JavaIsoVisitor

{ private final MethodMatcher methodMatcher; @@ -48,7 +46,7 @@ public DeclaresMethod(MethodMatcher methodMatcher) { @Override public J visit(@Nullable Tree tree, P p) { if (tree instanceof JavaSourceFile) { - JavaSourceFile cu = (JavaSourceFile) requireNonNull(tree); + JavaSourceFile cu = (JavaSourceFile) tree; for (JavaType.Method method : cu.getTypesInUse().getDeclaredMethods()) { if (methodMatcher.matches(method)) { return SearchResult.found(cu); diff --git a/rewrite-java/src/main/java/org/openrewrite/java/search/UsesMethod.java b/rewrite-java/src/main/java/org/openrewrite/java/search/UsesMethod.java index 60ad4d76a14..48ae2153d84 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/search/UsesMethod.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/search/UsesMethod.java @@ -24,8 +24,6 @@ import org.openrewrite.java.tree.JavaType; import org.openrewrite.marker.SearchResult; -import static java.util.Objects.requireNonNull; - public class UsesMethod

extends JavaIsoVisitor

{ private final MethodMatcher methodMatcher; @@ -48,7 +46,7 @@ public UsesMethod(MethodMatcher methodMatcher) { @Override public J visit(@Nullable Tree tree, P p) { if (tree instanceof JavaSourceFile) { - JavaSourceFile cu = (JavaSourceFile) requireNonNull(tree); + JavaSourceFile cu = (JavaSourceFile) tree; for (JavaType.Method type : cu.getTypesInUse().getUsedMethods()) { if (methodMatcher.matches(type)) { return SearchResult.found(cu);