diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/search/FindPlugins.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/search/FindPlugins.java index 3069f204b72..c3578e0fb73 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/search/FindPlugins.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/search/FindPlugins.java @@ -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; @@ -57,20 +58,6 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { MethodMatcher pluginMatcher = new MethodMatcher("PluginSpec id(..)", false); - TreeVisitor jv = Preconditions.check( - Preconditions.or(new IsBuildGradle<>(), new IsSettingsGradle<>()), - new JavaVisitor() { - @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() { @Override @@ -80,14 +67,31 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) } SourceFile s = (SourceFile) tree; + AtomicBoolean found = new AtomicBoolean(false); + TreeVisitor jv = Preconditions.check( + Preconditions.or(new IsBuildGradle<>(), new IsSettingsGradle<>()), + new JavaVisitor() { + + @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); } @@ -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.