Skip to content

Commit

Permalink
ChangeMethodName: Slightly improve performance
Browse files Browse the repository at this point in the history
Don't call `DeclaresMethod` twice when `ignoreDefinition = true`.
  • Loading branch information
knutwannheden committed Jan 9, 2024
1 parent affb71c commit fb7dbd6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down

0 comments on commit fb7dbd6

Please sign in to comment.