From fb7dbd6c95ef5492301dcb941e7f8eefd223621e Mon Sep 17 00:00:00 2001 From: Knut Wannheden <knut@moderne.io> Date: Tue, 9 Jan 2024 10:36:21 +0100 Subject: [PATCH] `ChangeMethodName`: Slightly improve performance Don't call `DeclaresMethod` twice when `ignoreDefinition = true`. --- .../java/org/openrewrite/java/ChangeMethodName.java | 13 +++++++------ .../org/openrewrite/java/search/DeclaresMethod.java | 4 +--- .../org/openrewrite/java/search/UsesMethod.java | 4 +--- 3 files changed, 9 insertions(+), 12 deletions(-) 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<?, ExecutionContext> 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<P> extends JavaIsoVisitor<P> { 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<P> extends JavaIsoVisitor<P> { 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);