Skip to content

Commit c91c113

Browse files
committed
PlanJavaMigration
1 parent 0f9298e commit c91c113

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate.search;
17+
18+
import org.openrewrite.*;
19+
import org.openrewrite.java.marker.JavaVersion;
20+
import org.openrewrite.java.migrate.table.JavaVersionMigrationPlan;
21+
import org.openrewrite.java.tree.JavaSourceFile;
22+
import org.openrewrite.marker.BuildTool;
23+
import org.openrewrite.marker.Markers;
24+
25+
import java.io.File;
26+
import java.util.Collection;
27+
import java.util.Collections;
28+
29+
public class PlanJavaMigration extends ScanningRecipe<JavaVersionMigrationPlan.Row.Builder> {
30+
transient JavaVersionMigrationPlan plan = new JavaVersionMigrationPlan(this);
31+
32+
@Override
33+
public String getDisplayName() {
34+
return "Plan a Java version migration";
35+
}
36+
37+
@Override
38+
public String getDescription() {
39+
return "Study the set of Java versions and associated tools in " +
40+
"use across many repositories.";
41+
}
42+
43+
@Override
44+
public JavaVersionMigrationPlan.Row.Builder getInitialValue(ExecutionContext ctx) {
45+
return JavaVersionMigrationPlan.Row.builder();
46+
}
47+
48+
@Override
49+
public TreeVisitor<?, ExecutionContext> getScanner(JavaVersionMigrationPlan.Row.Builder acc) {
50+
return new TreeVisitor<Tree, ExecutionContext>() {
51+
52+
@Override
53+
public Tree preVisit(Tree tree, ExecutionContext ctx) {
54+
if (tree instanceof SourceFile) {
55+
File sourceFile = ((SourceFile) tree).getSourcePath().toFile();
56+
if (sourceFile.getName().contains("build.gradle")) {
57+
acc.hasGradleBuild(true);
58+
} else if (sourceFile.getName().contains("pom.xml")) {
59+
acc.hasMavenPom(true);
60+
}
61+
}
62+
63+
if (tree instanceof JavaSourceFile) {
64+
acc.hasJava(true);
65+
Markers markers = tree.getMarkers();
66+
markers.findFirst(JavaVersion.class).ifPresent(javaVersion -> {
67+
acc.sourceCompatibility(javaVersion.getSourceCompatibility());
68+
acc.majorVersionSourceCompatibility(javaVersion.getMajorVersion());
69+
acc.targetCompatibility(javaVersion.getTargetCompatibility());
70+
});
71+
markers.findFirst(BuildTool.class).ifPresent(buildTool -> {
72+
switch (buildTool.getType()) {
73+
case Gradle:
74+
acc.gradleVersion(buildTool.getVersion());
75+
break;
76+
case Maven:
77+
acc.mavenVersion(buildTool.getVersion());
78+
break;
79+
}
80+
});
81+
}
82+
return tree;
83+
}
84+
};
85+
}
86+
87+
@Override
88+
public Collection<? extends SourceFile> generate(JavaVersionMigrationPlan.Row.Builder acc, ExecutionContext ctx) {
89+
plan.insertRow(ctx, acc.build());
90+
return Collections.emptyList();
91+
}
92+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate.table;
17+
18+
import lombok.Builder;
19+
import lombok.Value;
20+
import org.openrewrite.Column;
21+
import org.openrewrite.DataTable;
22+
import org.openrewrite.Recipe;
23+
24+
public class JavaVersionMigrationPlan extends DataTable<JavaVersionMigrationPlan.Row> {
25+
26+
public JavaVersionMigrationPlan(Recipe recipe) {
27+
super(
28+
recipe,
29+
"Java version migration plan",
30+
"A per-repository view of the current state of Java versions and associated build tools"
31+
);
32+
}
33+
34+
@Builder(builderClassName = "Builder")
35+
@Value
36+
public static class Row {
37+
@Column(displayName = "Has Java",
38+
description = "Whether this is a Java repository at all.")
39+
boolean hasJava;
40+
41+
@Column(displayName = "Source compatibility",
42+
description = "The source compatibility of the source file.")
43+
String sourceCompatibility;
44+
45+
@Column(displayName = "Major version source compatibility",
46+
description = "The major version.")
47+
Integer majorVersionSourceCompatibility;
48+
49+
@Column(displayName = "Target compatibility",
50+
description = "The target compatibility or `--release` version of the source file.")
51+
String targetCompatibility;
52+
53+
@Column(displayName = "Gradle version",
54+
description = "The version of Gradle in use, if any.")
55+
String gradleVersion;
56+
57+
@Column(displayName = "Has Gradle build",
58+
description = "Whether a build.gradle file exists in the repository.")
59+
Boolean hasGradleBuild;
60+
61+
@Column(displayName = "Maven version",
62+
description = "The version of Maven in use, if any.")
63+
String mavenVersion;
64+
65+
@Column(displayName = "Has Maven pom",
66+
description = "Whether a pom.xml file exists in the repository.")
67+
Boolean hasMavenPom;
68+
}
69+
}

0 commit comments

Comments
 (0)