|
| 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 | +} |
0 commit comments