2525import org .openrewrite .java .MethodMatcher ;
2626import org .openrewrite .java .tree .Expression ;
2727import org .openrewrite .java .tree .J ;
28- import org .openrewrite .kotlin .tree .K ;
2928
3029@ Value
3130@ EqualsAndHashCode (callSuper = false )
3231public class RemovePluginVisitor extends JavaIsoVisitor <ExecutionContext > {
3332 String pluginId ;
3433
35- MethodMatcher buildPluginsContainerMatcher = new MethodMatcher ("RewriteGradleProject plugins(..)" );
36- MethodMatcher applyPluginMatcher = new MethodMatcher ("RewriteGradleProject apply(..)" );
37- MethodMatcher settingsPluginsContainerMatcher = new MethodMatcher ("RewriteSettings plugins(..)" );
34+ MethodMatcher buildPluginsContainerMatcher = new MethodMatcher ("org.gradle.api.Project plugins(..)" , true );
35+ MethodMatcher applyPluginMatcher = new MethodMatcher ("org.gradle.api.Project apply(..)" , true );
36+ MethodMatcher settingsPluginsContainerMatcher = new MethodMatcher ("org.gradle.api.initialization.Settings plugins(..)" , true );
3837
39- MethodMatcher buildPluginMatcher = new MethodMatcher ("PluginSpec id(..)" );
40- MethodMatcher buildPluginWithVersionMatcher = new MethodMatcher ("Plugin version(..)" );
41- MethodMatcher buildPluginWithApplyMatcher = new MethodMatcher ("Plugin apply(..)" );
42- MethodMatcher settingsPluginMatcher = new MethodMatcher ("PluginSpec id(..)" );
43- MethodMatcher settingsPluginWithVersionMatcher = new MethodMatcher ("Plugin version(..)" );
44- MethodMatcher settingsPluginWithApplyMatcher = new MethodMatcher ("Plugin apply(..)" );
38+ MethodMatcher pluginIdMatcher = new MethodMatcher ("org.gradle.plugin.use.PluginDependenciesSpec id(..)" , true );
39+ MethodMatcher pluginVersionMatcher = new MethodMatcher ("org.gradle.plugin.use.PluginDependencySpec version(..)" , true );
40+ MethodMatcher pluginApplyMatcher = new MethodMatcher ("org.gradle.plugin.use.PluginDependencySpec apply(..)" , true );
4541
4642 @ Override
4743 public J .Block visitBlock (J .Block block , ExecutionContext executionContext ) {
@@ -52,10 +48,7 @@ public J.Block visitBlock(J.Block block, ExecutionContext executionContext) {
5248 return b ;
5349 }
5450
55- boolean isKotlin = getCursor ().firstEnclosing (K .CompilationUnit .class ) != null ;
56- boolean isPluginsBlock = isKotlin ?
57- "plugins" .equals (enclosingMethod .getSimpleName ()) :
58- buildPluginsContainerMatcher .matches (enclosingMethod ) || settingsPluginsContainerMatcher .matches (enclosingMethod );
51+ boolean isPluginsBlock = buildPluginsContainerMatcher .matches (enclosingMethod ) || settingsPluginsContainerMatcher .matches (enclosingMethod );
5952 if (!isPluginsBlock ) {
6053 return b ;
6154 }
@@ -71,28 +64,28 @@ public J.Block visitBlock(J.Block block, ExecutionContext executionContext) {
7164 }
7265
7366 // Check for id("pluginId")
74- if (isIdMethodInvocation (m , isKotlin )) {
67+ if (isIdMethodInvocation (m )) {
7568 if (isPluginLiteral (m .getArguments ().get (0 ))) {
7669 return null ;
7770 }
7871 }
7972 // Check for id("pluginId").version("...")
80- else if (isVersionMethodInvocation (m , isKotlin )) {
73+ else if (isVersionMethodInvocation (m )) {
8174 if (m .getSelect () instanceof J .MethodInvocation &&
8275 isPluginLiteral (((J .MethodInvocation ) m .getSelect ()).getArguments ().get (0 ))) {
8376 return null ;
8477 }
8578 }
8679 // Check for id("pluginId").apply(...) or id("pluginId").version("...").apply(...)
87- else if (isApplyMethodInvocation (m , isKotlin )) {
88- if (isIdMethodInvocation (m .getSelect (), isKotlin )) {
80+ else if (isApplyMethodInvocation (m )) {
81+ if (isIdMethodInvocation (m .getSelect ())) {
8982 if (m .getSelect () instanceof J .MethodInvocation &&
9083 isPluginLiteral (((J .MethodInvocation ) m .getSelect ()).getArguments ().get (0 ))) {
9184 return null ;
9285 }
93- } else if (isVersionMethodInvocation (m .getSelect (), isKotlin )) {
86+ } else if (isVersionMethodInvocation (m .getSelect ())) {
9487 if (m .getSelect () instanceof J .MethodInvocation &&
95- isIdMethodInvocation (((J .MethodInvocation ) m .getSelect ()).getSelect (), isKotlin )) {
88+ isIdMethodInvocation (((J .MethodInvocation ) m .getSelect ()).getSelect ())) {
9689 if (((J .MethodInvocation ) m .getSelect ()).getSelect () instanceof J .MethodInvocation &&
9790 isPluginLiteral (((J .MethodInvocation ) ((J .MethodInvocation ) m .getSelect ()).getSelect ()).getArguments ().get (0 ))) {
9891 return null ;
@@ -110,46 +103,33 @@ private boolean isPluginLiteral(Expression expression) {
110103 pluginId .equals (((J .Literal ) expression ).getValue ());
111104 }
112105
113- private boolean isIdMethodInvocation (@ Nullable Expression expr , boolean isKotlin ) {
106+ private boolean isIdMethodInvocation (@ Nullable Expression expr ) {
114107 if (!(expr instanceof J .MethodInvocation )) {
115108 return false ;
116109 }
117- J .MethodInvocation m = (J .MethodInvocation ) expr ;
118- return isKotlin ?
119- "id" .equals (m .getSimpleName ()) :
120- (buildPluginMatcher .matches (m ) || settingsPluginMatcher .matches (m ));
110+ return pluginIdMatcher .matches ((J .MethodInvocation ) expr );
121111 }
122112
123- private boolean isVersionMethodInvocation (@ Nullable Expression expr , boolean isKotlin ) {
113+ private boolean isVersionMethodInvocation (@ Nullable Expression expr ) {
124114 if (!(expr instanceof J .MethodInvocation )) {
125115 return false ;
126116 }
127- J .MethodInvocation m = (J .MethodInvocation ) expr ;
128- return isKotlin ?
129- "version" .equals (m .getSimpleName ()) :
130- (buildPluginWithVersionMatcher .matches (m ) || settingsPluginWithVersionMatcher .matches (m ));
117+ return pluginVersionMatcher .matches ((J .MethodInvocation ) expr );
131118 }
132119
133- private boolean isApplyMethodInvocation (@ Nullable Expression expr , boolean isKotlin ) {
120+ private boolean isApplyMethodInvocation (@ Nullable Expression expr ) {
134121 if (!(expr instanceof J .MethodInvocation )) {
135122 return false ;
136123 }
137- J .MethodInvocation m = (J .MethodInvocation ) expr ;
138- return isKotlin ?
139- "apply" .equals (m .getSimpleName ()) :
140- (buildPluginWithApplyMatcher .matches (m ) || settingsPluginWithApplyMatcher .matches (m ));
124+ return pluginApplyMatcher .matches ((J .MethodInvocation ) expr );
141125 }
142126
143127 @ Override
144128 public J .@ Nullable MethodInvocation visitMethodInvocation (J .MethodInvocation method , ExecutionContext executionContext ) {
145129 J .MethodInvocation m = super .visitMethodInvocation (method , executionContext );
146130
147- boolean isKotlin = getCursor ().firstEnclosing (K .CompilationUnit .class ) != null ;
148-
149131 // Check for empty plugins{} block
150- boolean isPluginsMethod = isKotlin ?
151- "plugins" .equals (m .getSimpleName ()) :
152- (buildPluginsContainerMatcher .matches (m ) || settingsPluginsContainerMatcher .matches (m ));
132+ boolean isPluginsMethod = buildPluginsContainerMatcher .matches (m ) || settingsPluginsContainerMatcher .matches (m );
153133
154134 if (isPluginsMethod ) {
155135 if (m .getArguments ().get (0 ) instanceof J .Lambda &&
@@ -159,7 +139,7 @@ private boolean isApplyMethodInvocation(@Nullable Expression expr, boolean isKot
159139 }
160140 }
161141 // Check for TOP-LEVEL apply plugin: "..." or apply(plugin = "...")
162- else if (( isKotlin && "apply" . equals ( m . getSimpleName ())) || (! isKotlin && applyPluginMatcher .matches (m ) )) {
142+ else if (applyPluginMatcher .matches (m )) {
163143 for (Expression arg : m .getArguments ()) {
164144 if (arg instanceof G .MapEntry ) {
165145 G .MapEntry me = (G .MapEntry ) arg ;
0 commit comments