Skip to content

Commit 74221d4

Browse files
committed
Use real Gradle API types in MethodMatcher patterns for KTS support
Update MethodMatcher patterns from synthetic Groovy template types (RewriteGradleProject, RewriteSettings, PluginSpec, Plugin) to real Gradle API types (org.gradle.api.Project, Settings, PluginDependenciesSpec, PluginDependencySpec) with matchOverrides=true, enabling matching for both Groovy DSL and Kotlin DSL scripts.
1 parent a8ef4b3 commit 74221d4

10 files changed

Lines changed: 38 additions & 58 deletions

rewrite-gradle/src/main/groovy/Plugin.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
interface PluginSpec {
16+
interface PluginSpec extends org.gradle.plugin.use.PluginDependenciesSpec {
1717
Plugin id(String i)
1818
}
1919

20-
interface Plugin {
20+
interface Plugin extends org.gradle.plugin.use.PluginDependencySpec {
2121
Plugin version(String v)
2222
Plugin apply(boolean a)
2323
}

rewrite-gradle/src/main/java/org/openrewrite/gradle/DependencyConstraintToRule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
@EqualsAndHashCode(callSuper = false)
4646
public class DependencyConstraintToRule extends Recipe {
4747

48-
private static final MethodMatcher DEPENDENCIES_DSL_MATCHER = new MethodMatcher("RewriteGradleProject dependencies(..)");
48+
private static final MethodMatcher DEPENDENCIES_DSL_MATCHER = new MethodMatcher("org.gradle.api.Project dependencies(..)", true);
4949
private static final String CONSTRAINT_MATCHER = "org.gradle.api.artifacts.dsl.DependencyHandler *(..)";
5050

5151
String displayName = "Dependency constraint to resolution rule";

rewrite-gradle/src/main/java/org/openrewrite/gradle/UpdateJavaCompatibility.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
@Value
4343
@EqualsAndHashCode(callSuper = false)
4444
public class UpdateJavaCompatibility extends Recipe {
45-
private static final MethodMatcher SOURCE_COMPATIBILITY_DSL = new MethodMatcher("RewriteGradleProject setSourceCompatibility(..)");
46-
private static final MethodMatcher TARGET_COMPATIBILITY_DSL = new MethodMatcher("RewriteGradleProject setTargetCompatibility(..)");
45+
private static final MethodMatcher SOURCE_COMPATIBILITY_DSL = new MethodMatcher("org.gradle.api.Project setSourceCompatibility(..)", true);
46+
private static final MethodMatcher TARGET_COMPATIBILITY_DSL = new MethodMatcher("org.gradle.api.Project setTargetCompatibility(..)", true);
4747

4848
@Option(displayName = "Java version",
4949
description = "The Java version to upgrade to.",

rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeTransitiveDependencyVersion.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
@EqualsAndHashCode(callSuper = false)
6969
@RequiredArgsConstructor
7070
public class UpgradeTransitiveDependencyVersion extends ScanningRecipe<UpgradeTransitiveDependencyVersion.DependencyVersionState> {
71-
private static final MethodMatcher DEPENDENCIES_DSL_MATCHER = new MethodMatcher("RewriteGradleProject dependencies(..)");
71+
private static final MethodMatcher DEPENDENCIES_DSL_MATCHER = new MethodMatcher("org.gradle.api.Project dependencies(..)", true);
7272
private static final MethodMatcher CONSTRAINTS_MATCHER = new MethodMatcher("org.gradle.api.artifacts.dsl.DependencyHandler constraints(..)", true);
7373
private static final String CONSTRAINT_MATCHER = "org.gradle.api.artifacts.dsl.DependencyHandler *(..)";
7474

rewrite-gradle/src/main/java/org/openrewrite/gradle/plugins/AddPluginVisitor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ private G.CompilationUnit addPluginToGroovyCompilationUnit(G.CompilationUnit cu,
188188
AtomicInteger doubleQuote = new AtomicInteger();
189189
AtomicBoolean pluginAlreadyApplied = new AtomicBoolean();
190190
new JavaIsoVisitor<Integer>() {
191-
final MethodMatcher pluginIdMatcher = new MethodMatcher("PluginSpec id(..)");
191+
final MethodMatcher pluginIdMatcher = new MethodMatcher("org.gradle.plugin.use.PluginDependenciesSpec id(..)", true);
192192

193193
@Override
194194
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Integer integer) {
@@ -231,7 +231,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Integ
231231
.map(parsed -> parsed.getStatements().get(0))
232232
.orElseThrow(() -> new IllegalArgumentException("Could not parse as Gradle"));
233233

234-
if (FindMethods.find(cu, "RewriteGradleProject plugins(..)").isEmpty() && FindMethods.find(cu, "RewriteSettings plugins(..)").isEmpty()) {
234+
if (FindMethods.find(cu, "org.gradle.api.Project plugins(..)", true).isEmpty() && FindMethods.find(cu, "org.gradle.api.initialization.Settings plugins(..)", true).isEmpty()) {
235235
int insertAtIdx = 0;
236236

237237
if (cu.getSourcePath().endsWith(Paths.get("settings.gradle"))) {
@@ -269,8 +269,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Integ
269269
return cu.withStatements(ListUtils.insert(cu.getStatements(), autoFormat(statement.withPrefix(Space.format("\n\n")), ctx, getCursor()), insertAtIdx));
270270
}
271271
} else {
272-
MethodMatcher buildPluginsMatcher = new MethodMatcher("RewriteGradleProject plugins(groovy.lang.Closure)");
273-
MethodMatcher settingsPluginsMatcher = new MethodMatcher("RewriteSettings plugins(groovy.lang.Closure)");
272+
MethodMatcher buildPluginsMatcher = new MethodMatcher("org.gradle.api.Project plugins(groovy.lang.Closure)", true);
273+
MethodMatcher settingsPluginsMatcher = new MethodMatcher("org.gradle.api.initialization.Settings plugins(groovy.lang.Closure)", true);
274274
J.MethodInvocation pluginDef = (J.MethodInvocation) ((J.Return) ((J.Block) ((J.Lambda) ((J.MethodInvocation) autoFormat(statement, ctx, getCursor())).getArguments().get(0)).getBody()).getStatements().get(0)).getExpression();
275275
return cu.withStatements(ListUtils.map(cu.getStatements(), stat -> {
276276
if (stat instanceof J.MethodInvocation) {

rewrite-gradle/src/main/java/org/openrewrite/gradle/plugins/ChangePlugin.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ public Validated<Object> validate() {
9898

9999
@Override
100100
public TreeVisitor<?, ExecutionContext> getVisitor() {
101-
MethodMatcher pluginMatcher = new MethodMatcher("PluginSpec id(..)");
102-
MethodMatcher versionMatcher = new MethodMatcher("Plugin version(..)");
103-
MethodMatcher applyMatcher = new MethodMatcher("RewriteGradleProject apply(..)");
101+
MethodMatcher pluginMatcher = new MethodMatcher("org.gradle.plugin.use.PluginDependenciesSpec id(..)", true);
102+
MethodMatcher versionMatcher = new MethodMatcher("org.gradle.plugin.use.PluginDependencySpec version(..)", true);
103+
MethodMatcher applyMatcher = new MethodMatcher("org.gradle.api.Project apply(..)", true);
104104
return Preconditions.check(
105105
Preconditions.or(new IsBuildGradle<>(), new IsSettingsGradle<>()),
106106
new GroovyIsoVisitor<ExecutionContext>() {

rewrite-gradle/src/main/java/org/openrewrite/gradle/plugins/RemovePluginVisitor.java

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,19 @@
2525
import org.openrewrite.java.MethodMatcher;
2626
import org.openrewrite.java.tree.Expression;
2727
import org.openrewrite.java.tree.J;
28-
import org.openrewrite.kotlin.tree.K;
2928

3029
@Value
3130
@EqualsAndHashCode(callSuper = false)
3231
public 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;

rewrite-gradle/src/main/java/org/openrewrite/gradle/search/DependencyInsight.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class DependencyInsight extends Recipe {
5353
transient ExplainDependenciesInUse explainDependenciesInUse = new ExplainDependenciesInUse(this);
5454

5555
private static final MethodMatcher DEPENDENCY_CONFIGURATION_MATCHER = new MethodMatcher("DependencyHandlerSpec *(..)");
56-
private static final MethodMatcher DEPENDENCY_CLOSURE_MATCHER = new MethodMatcher("RewriteGradleProject dependencies(..)");
56+
private static final MethodMatcher DEPENDENCY_CLOSURE_MATCHER = new MethodMatcher("org.gradle.api.Project dependencies(..)", true);
5757
private static final Function<Object, Set<GroupArtifactVersion>> EMPTY = gav -> new HashSet<>();
5858

5959
@Option(displayName = "Group pattern",

rewrite-gradle/src/main/java/org/openrewrite/gradle/search/FindRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public class FindRepository extends Recipe {
6161

6262
@Override
6363
public TreeVisitor<?, ExecutionContext> getVisitor() {
64-
MethodMatcher pluginManagementMatcher = new MethodMatcher("RewriteSettings pluginManagement(..)");
65-
MethodMatcher buildscriptMatcher = new MethodMatcher("RewriteGradleProject buildscript(..)");
64+
MethodMatcher pluginManagementMatcher = new MethodMatcher("org.gradle.api.initialization.Settings pluginManagement(..)", true);
65+
MethodMatcher buildscriptMatcher = new MethodMatcher("org.gradle.api.Project buildscript(..)", true);
6666
return Preconditions.check(Preconditions.or(new IsBuildGradle<>(), new IsSettingsGradle<>()), new JavaIsoVisitor<ExecutionContext>() {
6767
@Override
6868
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {

rewrite-gradle/src/main/java/org/openrewrite/gradle/trait/GradleDependencies.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
@RequiredArgsConstructor
4646
public class GradleDependencies implements Trait<J.MethodInvocation> {
47-
private static final MethodMatcher DEPENDENCY_DSL_MATCHER = new MethodMatcher("RewriteGradleProject dependencies(..)");
47+
private static final MethodMatcher DEPENDENCY_DSL_MATCHER = new MethodMatcher("org.gradle.api.Project dependencies(..)", true);
4848

4949
@Getter
5050
private final Cursor cursor;

0 commit comments

Comments
 (0)