Skip to content

Migrate Traits class usage to constructor calls #5613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.openrewrite.gradle.marker.GradleProject;
import org.openrewrite.gradle.search.FindGradleProject;
import org.openrewrite.gradle.trait.GradleDependency;
import org.openrewrite.gradle.trait.Traits;
import org.openrewrite.groovy.tree.G;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.StringUtils;
Expand Down Expand Up @@ -183,7 +182,7 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);

GradleDependency.Matcher gradleDependencyMatcher = Traits.gradleDependency()
GradleDependency.Matcher gradleDependencyMatcher = new GradleDependency.Matcher()
.groupId(oldGroupId)
.artifactId(oldArtifactId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.openrewrite.gradle.marker.GradleDependencyConfiguration;
import org.openrewrite.gradle.marker.GradleProject;
import org.openrewrite.gradle.trait.GradleDependency;
import org.openrewrite.gradle.trait.Traits;
import org.openrewrite.groovy.GroovyIsoVisitor;
import org.openrewrite.groovy.tree.G;
import org.openrewrite.internal.ListUtils;
Expand Down Expand Up @@ -113,7 +112,7 @@ public G.CompilationUnit visitCompilationUnit(G.CompilationUnit cu, ExecutionCon
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);

GradleDependency.Matcher gradleDependencyMatcher = Traits.gradleDependency()
GradleDependency.Matcher gradleDependencyMatcher = new GradleDependency.Matcher()
.configuration(configuration)
.groupId(groupId)
.artifactId(artifactId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.openrewrite.gradle.marker.GradleDependencyConfiguration;
import org.openrewrite.gradle.marker.GradleProject;
import org.openrewrite.gradle.trait.GradleDependency;
import org.openrewrite.gradle.trait.Traits;
import org.openrewrite.groovy.GroovyIsoVisitor;
import org.openrewrite.groovy.tree.G;
import org.openrewrite.internal.ListUtils;
Expand Down Expand Up @@ -111,7 +110,7 @@ public G.CompilationUnit visitCompilationUnit(G.CompilationUnit cu, ExecutionCon
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);
GradleDependency.Matcher gradleDependencyMatcher = Traits.gradleDependency()
GradleDependency.Matcher gradleDependencyMatcher = new GradleDependency.Matcher()
.configuration(configuration)
.groupId(groupId)
.artifactId(artifactId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.openrewrite.gradle.internal.Dependency;
import org.openrewrite.gradle.internal.DependencyStringNotationConverter;
import org.openrewrite.gradle.trait.GradleDependency;
import org.openrewrite.gradle.trait.Traits;
import org.openrewrite.groovy.GroovyIsoVisitor;
import org.openrewrite.groovy.tree.G;
import org.openrewrite.internal.StringUtils;
Expand Down Expand Up @@ -95,7 +94,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);

GradleDependency.Matcher gradleDependencyMatcher = Traits.gradleDependency()
GradleDependency.Matcher gradleDependencyMatcher = new GradleDependency.Matcher()
.configuration(configuration);

if (!gradleDependencyMatcher.get(getCursor()).isPresent() && !matchesOtherDependency(m)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.openrewrite.gradle.internal.Dependency;
import org.openrewrite.gradle.internal.DependencyStringNotationConverter;
import org.openrewrite.gradle.trait.GradleDependency;
import org.openrewrite.gradle.trait.Traits;
import org.openrewrite.groovy.GroovyIsoVisitor;
import org.openrewrite.groovy.tree.G;
import org.openrewrite.internal.ListUtils;
Expand Down Expand Up @@ -85,7 +84,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);

GradleDependency.Matcher gradleDependencyMatcher = Traits.gradleDependency()
GradleDependency.Matcher gradleDependencyMatcher = new GradleDependency.Matcher()
.configuration(configuration)
.groupId(groupId)
.artifactId(artifactId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.openrewrite.gradle.marker.GradleDependencyConfiguration;
import org.openrewrite.gradle.marker.GradleProject;
import org.openrewrite.gradle.trait.GradleDependency;
import org.openrewrite.gradle.trait.Traits;
import org.openrewrite.groovy.GroovyIsoVisitor;
import org.openrewrite.groovy.tree.G;
import org.openrewrite.internal.ListUtils;
Expand Down Expand Up @@ -113,7 +112,7 @@ public G.CompilationUnit visitCompilationUnit(G.CompilationUnit cu, ExecutionCon
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);

GradleDependency.Matcher gradleDependencyMatcher = Traits.gradleDependency()
GradleDependency.Matcher gradleDependencyMatcher = new GradleDependency.Matcher()
.configuration(configuration)
.groupId(groupId)
.artifactId(artifactId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.openrewrite.gradle.internal.Dependency;
import org.openrewrite.gradle.internal.DependencyStringNotationConverter;
import org.openrewrite.gradle.trait.GradleDependency;
import org.openrewrite.gradle.trait.Traits;
import org.openrewrite.groovy.GroovyVisitor;
import org.openrewrite.groovy.tree.G;
import org.openrewrite.internal.ListUtils;
Expand Down Expand Up @@ -62,7 +61,7 @@ private static class KotlinScriptVisitor extends KotlinVisitor<ExecutionContext>
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);

GradleDependency.Matcher gradleDependencyMatcher = Traits.gradleDependency();
GradleDependency.Matcher gradleDependencyMatcher = new GradleDependency.Matcher();

if (!gradleDependencyMatcher.get(getCursor()).isPresent()) {
return m;
Expand Down Expand Up @@ -94,7 +93,7 @@ private static class GroovyScriptVisitor extends GroovyVisitor<ExecutionContext>
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);

GradleDependency.Matcher gradleDependencyMatcher = Traits.gradleDependency();
GradleDependency.Matcher gradleDependencyMatcher = new GradleDependency.Matcher();

if (!gradleDependencyMatcher.get(getCursor()).isPresent()) {
return m;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.gradle.trait.GradleDependency;
import org.openrewrite.gradle.trait.Traits;
import org.openrewrite.groovy.tree.G;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.tree.Expression;
Expand Down Expand Up @@ -54,7 +53,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);

GradleDependency.Matcher gradleDependencyMatcher = Traits.gradleDependency();
GradleDependency.Matcher gradleDependencyMatcher = new GradleDependency.Matcher();

if (!gradleDependencyMatcher.get(getCursor()).isPresent()) {
return m;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.openrewrite.gradle.marker.GradleDependencyConfiguration;
import org.openrewrite.gradle.marker.GradleProject;
import org.openrewrite.gradle.trait.GradleDependency;
import org.openrewrite.gradle.trait.Traits;
import org.openrewrite.groovy.tree.G;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.StringUtils;
Expand Down Expand Up @@ -77,7 +76,7 @@ public String getDescription() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new IsBuildGradle<>(), new JavaIsoVisitor<ExecutionContext>() {
final GradleDependency.Matcher gradleDependencyMatcher = Traits.gradleDependency()
final GradleDependency.Matcher gradleDependencyMatcher = new GradleDependency.Matcher()
.configuration(configuration)
.groupId(groupId)
.artifactId(artifactId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.openrewrite.gradle.marker.GradleDependencyConfiguration;
import org.openrewrite.gradle.marker.GradleProject;
import org.openrewrite.gradle.trait.GradleDependency;
import org.openrewrite.gradle.trait.Traits;
import org.openrewrite.groovy.tree.G;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.StringUtils;
Expand Down Expand Up @@ -329,7 +328,7 @@ boolean shouldRemoveRedundantConstraint(@Nullable Dependency constraint, @Nullab
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);

Optional<GradleDependency> maybeGradleDependency = Traits.gradleDependency()
Optional<GradleDependency> maybeGradleDependency = new GradleDependency.Matcher()
.groupId(groupPattern)
.artifactId(artifactPattern)
.get(getCursor());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.openrewrite.gradle.marker.GradleDependencyConfiguration;
import org.openrewrite.gradle.marker.GradleProject;
import org.openrewrite.gradle.trait.GradleDependency;
import org.openrewrite.gradle.trait.Traits;
import org.openrewrite.groovy.tree.G;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.StringUtils;
Expand Down Expand Up @@ -170,7 +169,7 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
GradleDependency.Matcher gradleDependencyMatcher = Traits.gradleDependency();
GradleDependency.Matcher gradleDependencyMatcher = new GradleDependency.Matcher();

if (gradleDependencyMatcher.get(getCursor()).isPresent()) {
if (m.getArguments().get(0) instanceof G.MapEntry) {
Expand Down Expand Up @@ -532,7 +531,7 @@ public J postVisit(J tree, ExecutionContext ctx) {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
GradleDependency.Matcher gradleDependencyMatcher = Traits.gradleDependency();
GradleDependency.Matcher gradleDependencyMatcher = new GradleDependency.Matcher();

if (gradleDependencyMatcher.get(getCursor()).isPresent()) {
List<Expression> depArgs = m.getArguments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import static java.util.Objects.requireNonNull;
import static org.openrewrite.gradle.AddDependencyVisitor.DependencyModifier.ENFORCED_PLATFORM;
import static org.openrewrite.gradle.AddDependencyVisitor.DependencyModifier.PLATFORM;
import static org.openrewrite.gradle.trait.Traits.gradleDependency;

@RequiredArgsConstructor
public class AddDependencyVisitor extends JavaIsoVisitor<ExecutionContext> {
Expand Down Expand Up @@ -135,7 +134,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
return m;
}

Optional<GradleDependency> maybeDependency = gradleDependency()
Optional<GradleDependency> maybeDependency = new GradleDependency.Matcher()
.configuration(configuration)
.groupId(groupId)
.artifactId(artifactId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.openrewrite.*;
import org.openrewrite.gradle.marker.GradleDependencyConfiguration;
import org.openrewrite.gradle.marker.GradleProject;
import org.openrewrite.gradle.trait.Traits;
import org.openrewrite.gradle.trait.GradleDependency;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.marker.JavaProject;
Expand Down Expand Up @@ -231,7 +231,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}

if (configurationToDirectDependency.containsKey(m.getSimpleName())) {
return Traits.gradleDependency().get(getCursor()).map(dependency -> {
return new GradleDependency.Matcher().get(getCursor()).map(dependency -> {
ResolvedGroupArtifactVersion gav = dependency.getResolvedDependency().getGav();
Optional<GroupArtifactVersion> configurationGav = configurationToDirectDependency.get(m.getSimpleName()).stream()
.filter(dep -> dep.asGroupArtifact().equals(gav.asGroupArtifact()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import java.util.Set;
import java.util.stream.Collectors;

import static org.openrewrite.gradle.trait.Traits.jvmTestSuite;

@Value
@EqualsAndHashCode(callSuper = false)
public class FindJVMTestSuites extends Recipe {
Expand All @@ -54,7 +52,7 @@ public String getDescription() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
boolean tableAvailable = this.insertRows == null || this.insertRows;
return Preconditions.check(new IsBuildGradle<>(), jvmTestSuite().asVisitor((suite, ctx) -> {
return Preconditions.check(new IsBuildGradle<>(), new JvmTestSuite.Matcher().asVisitor((suite, ctx) -> {
if (tableAvailable) {
jvmTestSuitesDefined.insertRow(ctx, new JVMTestSuitesDefined.Row(suite.getName()));
}
Expand All @@ -67,7 +65,7 @@ public static Set<JvmTestSuite> jvmTestSuites(SourceFile sourceFile) {
return Collections.emptySet();
}

return jvmTestSuite().lower(sourceFile)
return new JvmTestSuite.Matcher().lower(sourceFile)
.collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,26 @@
*/
package org.openrewrite.gradle.trait;

/**
* @deprecated Use specific matchers like {@link GradleDependency.Matcher} or {@link JvmTestSuite.Matcher} instead.
*/
@Deprecated
public class Traits {
private Traits() {
}

/**
* @deprecated Use {@link GradleDependency.Matcher} instead.
*/
@Deprecated
public static GradleDependency.Matcher gradleDependency() {
return new GradleDependency.Matcher();
}

/**
* @deprecated Use {@link JvmTestSuite.Matcher} instead.
*/
@Deprecated
public static JvmTestSuite.Matcher jvmTestSuite() {
return new JvmTestSuite.Matcher();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@

import static org.openrewrite.gradle.Assertions.buildGradle;
import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi;
import static org.openrewrite.gradle.trait.Traits.gradleDependency;

class GradleDependencyTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec
.beforeRecipe(withToolingApi())
.recipe(RewriteTest.toRecipe(() -> gradleDependency().asVisitor(dep ->
.recipe(RewriteTest.toRecipe(() -> new GradleDependency.Matcher().asVisitor(dep ->
SearchResult.found(dep.getTree(), dep.getResolvedDependency().getGav().toString()))));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@
import static org.openrewrite.gradle.Assertions.buildGradle;
import static org.openrewrite.gradle.Assertions.buildGradleKts;
import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi;
import static org.openrewrite.gradle.trait.Traits.jvmTestSuite;

class JvmTestSuiteTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.beforeRecipe(withToolingApi())
.recipe(RewriteTest.toRecipe(() -> jvmTestSuite().asVisitor(suite ->
.recipe(RewriteTest.toRecipe(() -> new JvmTestSuite.Matcher().asVisitor(suite ->
SearchResult.found(suite.getTree()))));
}

Expand Down Expand Up @@ -98,7 +97,7 @@ implementation project()
@Test
void findByName() {
rewriteRun(
spec -> spec.recipe(RewriteTest.toRecipe(() -> jvmTestSuite().name("integrationTest").asVisitor(suite ->
spec -> spec.recipe(RewriteTest.toRecipe(() -> new JvmTestSuite.Matcher().name("integrationTest").asVisitor(suite ->
SearchResult.found(suite.getTree())))),
buildGradle(
"""
Expand Down Expand Up @@ -156,7 +155,7 @@ implementation project()
@Test
void addDependency() {
rewriteRun(
spec -> spec.recipe(RewriteTest.toRecipe((recipe) -> jvmTestSuite().asVisitor((suite, ctx) ->
spec -> spec.recipe(RewriteTest.toRecipe((recipe) -> new JvmTestSuite.Matcher().asVisitor((suite, ctx) ->
suite.addDependency("implementation", "com.google.guava", "guava", "29.0-jre", null, null, null, new MavenMetadataFailures(recipe), null, ctx).visitNonNull(suite.getTree(), ctx, suite.getCursor().getParentOrThrow())))),
buildGradle(
"""
Expand Down Expand Up @@ -291,7 +290,7 @@ val functionalTest by registering(JvmTestSuite::class) {
@Test
void findByName() {
rewriteRun(
spec -> spec.recipe(RewriteTest.toRecipe(() -> jvmTestSuite().name("integrationTest").asVisitor(suite ->
spec -> spec.recipe(RewriteTest.toRecipe(() -> new JvmTestSuite.Matcher().name("integrationTest").asVisitor(suite ->
SearchResult.found(suite.getTree())))),
buildGradleKts(
"""
Expand Down Expand Up @@ -349,7 +348,7 @@ val test by getting(JvmTestSuite::class) {
@Test
void addDependency() {
rewriteRun(
spec -> spec.recipe(RewriteTest.toRecipe((recipe) -> jvmTestSuite().asVisitor((suite, ctx) ->
spec -> spec.recipe(RewriteTest.toRecipe((recipe) -> new JvmTestSuite.Matcher().asVisitor((suite, ctx) ->
suite.addDependency("implementation", "com.google.guava", "guava", "29.0-jre", null, null, null, new MavenMetadataFailures(recipe), null, ctx).visitNonNull(suite.getTree(), ctx, suite.getCursor().getParentOrThrow())))),
buildGradleKts(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import lombok.Value;
import org.openrewrite.*;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.trait.Traits;
import org.openrewrite.java.trait.MethodAccess;
import org.openrewrite.java.tree.Expression;

@Value
Expand Down Expand Up @@ -50,7 +50,7 @@ public String getDescription() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(
new UsesMethod<>(methodPattern),
Traits.methodAccess(methodPattern)
new MethodAccess.Matcher(methodPattern)
.asVisitor(ma ->
JavaTemplate.apply(replacement, ma.getCursor(),
((Expression) ma.getCursor().getValue()).getCoordinates().replace())));
Expand Down
Loading