3232public class RemovePluginVisitor extends JavaIsoVisitor <ExecutionContext > {
3333 String pluginId ;
3434
35- MethodMatcher buildPluginsContainerMatcher = new MethodMatcher ("org.gradle.api.Project plugins(..)" , true );
35+ // Wildcard type because KTS extension functions have a file-level declaring type, not Project/Settings
36+ MethodMatcher pluginsMatcher = new MethodMatcher ("* plugins(..)" , false );
3637 MethodMatcher applyPluginMatcher = new MethodMatcher ("org.gradle.api.Project apply(..)" , true );
38+ MethodMatcher buildPluginsContainerMatcher = new MethodMatcher ("org.gradle.api.Project plugins(..)" , true );
3739 MethodMatcher settingsPluginsContainerMatcher = new MethodMatcher ("org.gradle.api.initialization.Settings plugins(..)" , true );
3840
3941 MethodMatcher pluginIdMatcher = new MethodMatcher ("org.gradle.plugin.use.PluginDependenciesSpec id(..)" , true );
@@ -45,15 +47,7 @@ public J.Block visitBlock(J.Block block, ExecutionContext executionContext) {
4547 J .Block b = super .visitBlock (block , executionContext );
4648
4749 J .MethodInvocation enclosingMethod = getCursor ().firstEnclosing (J .MethodInvocation .class );
48- if (enclosingMethod == null ) {
49- return b ;
50- }
51-
52- boolean isKotlin = getCursor ().firstEnclosing (K .CompilationUnit .class ) != null ;
53- boolean isPluginsBlock = isKotlin ?
54- "plugins" .equals (enclosingMethod .getSimpleName ()) :
55- buildPluginsContainerMatcher .matches (enclosingMethod ) || settingsPluginsContainerMatcher .matches (enclosingMethod );
56- if (!isPluginsBlock ) {
50+ if (enclosingMethod == null || !isPluginsMethod (enclosingMethod )) {
5751 return b ;
5852 }
5953
@@ -68,28 +62,28 @@ public J.Block visitBlock(J.Block block, ExecutionContext executionContext) {
6862 }
6963
7064 // Check for id("pluginId")
71- if (isIdMethodInvocation (m , isKotlin )) {
65+ if (isIdMethodInvocation (m )) {
7266 if (isPluginLiteral (m .getArguments ().get (0 ))) {
7367 return null ;
7468 }
7569 }
7670 // Check for id("pluginId").version("...")
77- else if (isVersionMethodInvocation (m , isKotlin )) {
71+ else if (isVersionMethodInvocation (m )) {
7872 if (m .getSelect () instanceof J .MethodInvocation &&
7973 isPluginLiteral (((J .MethodInvocation ) m .getSelect ()).getArguments ().get (0 ))) {
8074 return null ;
8175 }
8276 }
8377 // Check for id("pluginId").apply(...) or id("pluginId").version("...").apply(...)
84- else if (isApplyMethodInvocation (m , isKotlin )) {
85- if (isIdMethodInvocation (m .getSelect (), isKotlin )) {
78+ else if (isApplyMethodInvocation (m )) {
79+ if (isIdMethodInvocation (m .getSelect ())) {
8680 if (m .getSelect () instanceof J .MethodInvocation &&
8781 isPluginLiteral (((J .MethodInvocation ) m .getSelect ()).getArguments ().get (0 ))) {
8882 return null ;
8983 }
90- } else if (isVersionMethodInvocation (m .getSelect (), isKotlin )) {
84+ } else if (isVersionMethodInvocation (m .getSelect ())) {
9185 if (m .getSelect () instanceof J .MethodInvocation &&
92- isIdMethodInvocation (((J .MethodInvocation ) m .getSelect ()).getSelect (), isKotlin )) {
86+ isIdMethodInvocation (((J .MethodInvocation ) m .getSelect ()).getSelect ())) {
9387 if (((J .MethodInvocation ) m .getSelect ()).getSelect () instanceof J .MethodInvocation &&
9488 isPluginLiteral (((J .MethodInvocation ) ((J .MethodInvocation ) m .getSelect ()).getSelect ()).getArguments ().get (0 ))) {
9589 return null ;
@@ -102,62 +96,53 @@ else if (isApplyMethodInvocation(m, isKotlin)) {
10296 }));
10397 }
10498
99+ private boolean isPluginsMethod (J .MethodInvocation m ) {
100+ // Specifically for Kotlin type information is still missing; match strongly where possible for Groovy
101+ return getCursor ().firstEnclosing (K .CompilationUnit .class ) != null ?
102+ pluginsMatcher .matches (m ) :
103+ buildPluginsContainerMatcher .matches (m , true ) || settingsPluginsContainerMatcher .matches (m );
104+ }
105+
105106 private boolean isPluginLiteral (Expression expression ) {
106107 return expression instanceof J .Literal &&
107108 pluginId .equals (((J .Literal ) expression ).getValue ());
108109 }
109110
110- private boolean isIdMethodInvocation (@ Nullable Expression expr , boolean isKotlin ) {
111+ private boolean isIdMethodInvocation (@ Nullable Expression expr ) {
111112 if (!(expr instanceof J .MethodInvocation )) {
112113 return false ;
113114 }
114- J .MethodInvocation m = (J .MethodInvocation ) expr ;
115- return isKotlin ?
116- "id" .equals (m .getSimpleName ()) :
117- pluginIdMatcher .matches (m );
115+ return pluginIdMatcher .matches ((J .MethodInvocation ) expr , true );
118116 }
119117
120- private boolean isVersionMethodInvocation (@ Nullable Expression expr , boolean isKotlin ) {
118+ private boolean isVersionMethodInvocation (@ Nullable Expression expr ) {
121119 if (!(expr instanceof J .MethodInvocation )) {
122120 return false ;
123121 }
124- J .MethodInvocation m = (J .MethodInvocation ) expr ;
125- return isKotlin ?
126- "version" .equals (m .getSimpleName ()) :
127- pluginVersionMatcher .matches (m );
122+ return pluginVersionMatcher .matches ((J .MethodInvocation ) expr , true );
128123 }
129124
130- private boolean isApplyMethodInvocation (@ Nullable Expression expr , boolean isKotlin ) {
125+ private boolean isApplyMethodInvocation (@ Nullable Expression expr ) {
131126 if (!(expr instanceof J .MethodInvocation )) {
132127 return false ;
133128 }
134- J .MethodInvocation m = (J .MethodInvocation ) expr ;
135- return isKotlin ?
136- "apply" .equals (m .getSimpleName ()) :
137- pluginApplyMatcher .matches (m );
129+ return pluginApplyMatcher .matches ((J .MethodInvocation ) expr , true );
138130 }
139131
140132 @ Override
141133 public J .@ Nullable MethodInvocation visitMethodInvocation (J .MethodInvocation method , ExecutionContext executionContext ) {
142134 J .MethodInvocation m = super .visitMethodInvocation (method , executionContext );
143135
144- boolean isKotlin = getCursor ().firstEnclosing (K .CompilationUnit .class ) != null ;
145-
146136 // Check for empty plugins{} block
147- boolean isPluginsMethod = isKotlin ?
148- "plugins" .equals (m .getSimpleName ()) :
149- (buildPluginsContainerMatcher .matches (m ) || settingsPluginsContainerMatcher .matches (m ));
150-
151- if (isPluginsMethod ) {
137+ if (isPluginsMethod (m )) {
152138 if (m .getArguments ().get (0 ) instanceof J .Lambda &&
153139 ((J .Lambda ) m .getArguments ().get (0 )).getBody () instanceof J .Block &&
154140 ((J .Block ) ((J .Lambda ) m .getArguments ().get (0 )).getBody ()).getStatements ().isEmpty ()) {
155141 return null ;
156142 }
157143 }
158144 // Check for TOP-LEVEL apply plugin: "..." or apply(plugin = "...")
159- else if ((isKotlin && "apply" .equals (m .getSimpleName ())) ||
160- (!isKotlin && applyPluginMatcher .matches (m ))) {
145+ else if (applyPluginMatcher .matches (m , true )) {
161146 for (Expression arg : m .getArguments ()) {
162147 if (arg instanceof G .MapEntry ) {
163148 G .MapEntry me = (G .MapEntry ) arg ;
0 commit comments