Skip to content

Commit

Permalink
Merge branch 'main' into recognize-bundle-packaging-type-as-jar
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Jun 3, 2024
2 parents f5f6910 + fadb443 commit df724bc
Show file tree
Hide file tree
Showing 15 changed files with 416 additions and 56 deletions.
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionSha256Sum=544c35d6bd849ae8a5ed0bcea39ba677dc40f49df7d1835561582da2009b961d
distributionSha256Sum=a4b4158601f8636cdeeab09bd76afb640030bb5b144aafe261a5e8af027dc612
2 changes: 1 addition & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite;

import lombok.Getter;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.marker.SearchResult;

Expand Down Expand Up @@ -136,13 +137,15 @@ public RecipeCheck(Recipe check, TreeVisitor<?, ExecutionContext> v) {
this.check = check;
}

public Recipe getCheck() {
public Recipe getRecipe() {
return check;
}
}

public static class Check extends TreeVisitor<Tree, ExecutionContext> {
@Getter
private final TreeVisitor<?, ExecutionContext> check;

private final TreeVisitor<?, ExecutionContext> v;

public Check(TreeVisitor<?, ExecutionContext> check, TreeVisitor<?, ExecutionContext> v) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,37 @@ public DependencyVersionState getInitialValue(ExecutionContext ctx) {
return new DependencyVersionState();
}

private static final MethodMatcher DEPENDENCY_DSL_MATCHER = new MethodMatcher("DependencyHandlerSpec *(..)");
private static final MethodMatcher DEPENDENCY_DSL_MATCHER = new MethodMatcher("RewriteGradleProject dependencies(groovy.lang.Closure)");
private static final MethodMatcher DEPENDENCY_CONFIGURATION_MATCHER = new MethodMatcher("DependencyHandlerSpec *(..)");

private static boolean isLikelyDependencyConfiguration(Cursor cursor) {
if (!(cursor.getValue() instanceof J.MethodInvocation)) {
return false;
}
J.MethodInvocation m = cursor.getValue();
if (DEPENDENCY_CONFIGURATION_MATCHER.matches(m)) {
return true;
}
// If it's a configuration created by a plugin, we may not be able to type-attribute it
// In the absence of type-attribution use its presence within a dependencies block to approximate
if (m.getType() != null) {
return false;
}
while (cursor != null) {
if (cursor.getValue() instanceof J.MethodInvocation) {
m = cursor.getValue();
String methodName = m.getSimpleName();
if ("constraints".equals(methodName)) {
return false;
}
if (DEPENDENCY_DSL_MATCHER.matches(m)) {
return true;
}
}
cursor = cursor.getParent();
}
return false;
}

@Override
public TreeVisitor<?, ExecutionContext> getScanner(DependencyVersionState acc) {
Expand All @@ -159,7 +189,7 @@ public J visitCompilationUnit(G.CompilationUnit cu, ExecutionContext ctx) {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (DEPENDENCY_DSL_MATCHER.matches(m)) {
if (isLikelyDependencyConfiguration(getCursor())) {
if (m.getArguments().get(0) instanceof G.MapEntry) {
String groupId = null;
String artifactId = null;
Expand Down Expand Up @@ -283,9 +313,10 @@ public TreeVisitor<?, ExecutionContext> getVisitor(DependencyVersionState acc) {
private class UpdateProperties extends PropertiesVisitor<ExecutionContext> {
final DependencyVersionState acc;
final DependencyMatcher dependencyMatcher = new DependencyMatcher(groupId, artifactId, null);

@Override
public Properties visitFile(Properties.File file, ExecutionContext ctx) {
if(!file.getSourcePath().endsWith(GRADLE_PROPERTIES_FILE_NAME)) {
if (!file.getSourcePath().endsWith(GRADLE_PROPERTIES_FILE_NAME)) {
return file;
}
return super.visitFile(file, ctx);
Expand Down Expand Up @@ -323,7 +354,7 @@ private class UpdateGroovy extends GroovyVisitor<ExecutionContext> {
public J visitCompilationUnit(G.CompilationUnit cu, ExecutionContext ctx) {
gradleProject = cu.getMarkers().findFirst(GradleProject.class)
.orElse(null);
if(gradleProject == null) {
if (gradleProject == null) {
return cu;
}
return super.visitCompilationUnit(cu, ctx);
Expand Down Expand Up @@ -352,12 +383,12 @@ public J postVisit(J tree, ExecutionContext ctx) {

@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if("constraints".equals(method.getSimpleName())) {
if ("constraints".equals(method.getSimpleName())) {
// don't mess with anything inside a constraints block, leave that to UpgradeTransitiveDependency version recipe
return method;
}
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (DEPENDENCY_DSL_MATCHER.matches(m)) {
if (isLikelyDependencyConfiguration(getCursor())) {
List<Expression> depArgs = m.getArguments();
if (depArgs.get(0) instanceof J.Literal || depArgs.get(0) instanceof G.GString || depArgs.get(0) instanceof G.MapEntry) {
m = updateDependency(m, ctx);
Expand Down
2 changes: 1 addition & 1 deletion rewrite-gradle/src/main/resources/gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -855,4 +855,31 @@ void leaveConstraintsAlone() {
)
);
}

@Test
void unknownConfiguration() {
rewriteRun(
spec -> spec.recipe(new UpgradeDependencyVersion("org.openapitools", "openapi-generator-cli", "5.2.1", null)),
buildGradle(
"""
plugins {
id 'java'
id "org.hidetake.swagger.generator" version "2.18.2"
}
dependencies {
swaggerCodegen "org.openapitools:openapi-generator-cli:5.2.0"
}
""",
"""
plugins {
id 'java'
id "org.hidetake.swagger.generator" version "2.18.2"
}
dependencies {
swaggerCodegen "org.openapitools:openapi-generator-cli:5.2.1"
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1953,13 +1953,18 @@ public TypeTree visitVariableExpressionType(VariableExpression expression) {

if (expression.isDynamicTyped()) {
Space prefix = whitespace();
String defOrVar = source.substring(cursor, cursor + 3);
cursor += 3;
String keyword;
if(source.substring(cursor).startsWith("final")) {
keyword = "final";
} else {
keyword = source.substring(cursor, cursor + 3);
}
cursor += keyword.length();
return new J.Identifier(randomId(),
prefix,
Markers.EMPTY,
emptyList(),
defOrVar,
keyword,
type, null);
}
Space prefix = sourceBefore(expression.getOriginType().getUnresolvedName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ void varKeyword() {
);
}

@Test
void finalKeyword() {
rewriteRun(
groovy("final a = 1")
);
}

@Test
void singleVariableDeclaration() {
Expand Down
Loading

0 comments on commit df724bc

Please sign in to comment.