Skip to content

Commit

Permalink
Avoid placing redundant, general search result when a more specific r…
Browse files Browse the repository at this point in the history
…esult is available
  • Loading branch information
sambsnyd committed Aug 8, 2024
1 parent f86bfd2 commit 2b49e9f
Showing 1 changed file with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import java.util.stream.Stream;

Expand Down Expand Up @@ -57,20 +58,6 @@ public String getDescription() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
MethodMatcher pluginMatcher = new MethodMatcher("PluginSpec id(..)", false);
TreeVisitor<?, ExecutionContext> jv = Preconditions.check(
Preconditions.or(new IsBuildGradle<>(), new IsSettingsGradle<>()),
new JavaVisitor<ExecutionContext>() {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (pluginMatcher.matches(method)) {
if (method.getArguments().get(0) instanceof J.Literal &&
pluginId.equals(((J.Literal) method.getArguments().get(0)).getValue())) {
return SearchResult.found(method);
}
}
return super.visitMethodInvocation(method, ctx);
}
});

return new TreeVisitor<Tree, ExecutionContext>() {
@Override
Expand All @@ -80,14 +67,31 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
}
SourceFile s = (SourceFile) tree;

AtomicBoolean found = new AtomicBoolean(false);
TreeVisitor<?, ExecutionContext> jv = Preconditions.check(
Preconditions.or(new IsBuildGradle<>(), new IsSettingsGradle<>()),
new JavaVisitor<ExecutionContext>() {

@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (pluginMatcher.matches(method)) {
if (method.getArguments().get(0) instanceof J.Literal &&
pluginId.equals(((J.Literal) method.getArguments().get(0)).getValue())) {
found.set(true);
return SearchResult.found(method);
}
}
return super.visitMethodInvocation(method, ctx);
}
});
if (jv.isAcceptable(s, ctx)) {
s = (SourceFile) jv.visitNonNull(s, ctx);
}

// Even if we couldn't find a declaration the metadata might show the plugin is in use
GradleProject gp = s.getMarkers().findFirst(GradleProject.class).orElse(null);
if (s == tree && gp != null && gp.getPlugins().stream()
.anyMatch(it -> pluginId.equals(it.getId()))) {
if (!found.get() && gp != null && gp.getPlugins().stream()
.anyMatch(it -> pluginId.equals(it.getId()))) {
s = SearchResult.found(s);
}

Expand All @@ -96,6 +100,11 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
};
}

@Override
public int maxCycles() {
return 1;
}

/**
* @param j The subtree to search.
* @param pluginIdPattern A method pattern. See {@link MethodMatcher} for details about this syntax.
Expand Down

0 comments on commit 2b49e9f

Please sign in to comment.