Adopt the latest rewrite snapshot

This commit is contained in:
aboyko
2023-05-31 18:54:16 -04:00
parent 73b6c6b5b0
commit 0274a64800
16 changed files with 200 additions and 107 deletions

View File

@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2022 VMware, Inc.
* Copyright (c) 2022, 2023 VMware, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -30,7 +30,7 @@ public class HelloMethodRenameRecipe extends Recipe {
}
@Override
protected TreeVisitor<?, ExecutionContext> getVisitor() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<>() {
@Override

View File

@@ -10,13 +10,21 @@
*******************************************************************************/
package org.springframework.rewrite.test;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.ChangePackage;
public class OssPackageRecipe extends Recipe {
public OssPackageRecipe() {
doNext(new ChangePackage("com.example", "org.example", true));
@Override
public String getDescription() {
return "Change package 'com.example' to 'org.example'";
}
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new ChangePackage("com.example", "org.example", true).getVisitor();
}
@Override

View File

@@ -107,7 +107,73 @@
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-tooling-api</artifactId>
<version>7.6</version>
<version>${gradle-tooling.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-base-services</artifactId>
<version>${gradle-core.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-core-api</artifactId>
<version>${gradle-core.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-language-groovy</artifactId>
<version>${gradle-core.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-language-java</artifactId>
<version>${gradle-core.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-logging</artifactId>
<version>${gradle-core.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-messaging</artifactId>
<version>${gradle-core.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-native</artifactId>
<version>${gradle-core.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-process-services</artifactId>
<version>${gradle-core.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-resources</artifactId>
<version>${gradle-core.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-testing-base</artifactId>
<version>${gradle-core.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-testing-jvm</artifactId>
<version>${gradle-core.version}</version>
<scope>runtime</scope>
</dependency>

View File

@@ -57,7 +57,7 @@ public class LoadUtils {
DeclarativeRecipe recipe = new DeclarativeRecipe(d.getName(), d.getDisplayName(), d.getDescription(),
d.getTags(), d.getEstimatedEffortPerOccurrence(), d.getSource(), false, d.getMaintainers());
for (RecipeDescriptor subDescriptor : d.getRecipeList()) {
recipe.doNext(createRecipe(subDescriptor, getRecipeClass));
recipe.getRecipeList().add(createRecipe(subDescriptor, getRecipeClass));
}
return recipe;
} else {

View File

@@ -18,6 +18,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.openrewrite.ExecutionContext;
@@ -28,6 +29,7 @@ import org.openrewrite.gradle.marker.GradleProject;
import org.openrewrite.gradle.toolingapi.OpenRewriteModel;
import org.openrewrite.gradle.toolingapi.OpenRewriteModelBuilder;
import org.openrewrite.groovy.GroovyParser;
import org.openrewrite.groovy.tree.G;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.JavaParser.Builder;
import org.openrewrite.java.marker.JavaProject;
@@ -72,9 +74,10 @@ public class GradleIJavaProjectParser extends AbstractJavaProjectParser {
Path buildFilePath = Paths.get(jp.getProjectBuild().getBuildFile());
List<G.CompilationUnit> gradleFiles = gradleParser.parseInputs(() ->
getInputs(Stream.of(buildFilePath)).iterator(), null, ctx).collect(Collectors.toList());
return ListUtils.map(
gradleParser.parseInputs(() ->
getInputs(Stream.of(buildFilePath)).iterator(), null, ctx), gb -> gb.withMarkers(gb.getMarkers().addIfAbsent(gradleProject))
gradleFiles, (i, gb) -> gb.withMarkers(gb.getMarkers().addIfAbsent(gradleProject))
);
}

View File

@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2022 VMware, Inc.
* Copyright (c) 2022, 2023 VMware, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -14,6 +14,7 @@ import java.util.ArrayList;
import java.util.List;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
@@ -22,6 +23,7 @@ import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeUtils;
import org.openrewrite.marker.SearchResult;
public class BeanPostProcessingIgnoreInAot extends Recipe {
@@ -80,57 +82,56 @@ public class BeanPostProcessingIgnoreInAot extends Recipe {
}
@Override
protected TreeVisitor<?, ExecutionContext> getApplicableTest() {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext executionContext) {
if (isApplicableClass(classDecl)) {
return classDecl.withMarkers(classDecl.getMarkers().searchResult());
}
return super.visitClassDeclaration(classDecl, executionContext);
}
};
}
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(
new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl,
ExecutionContext executionContext) {
if (isApplicableClass(classDecl)) {
return SearchResult.found(classDecl);
}
return super.visitClassDeclaration(classDecl, executionContext);
}
},
new JavaIsoVisitor<ExecutionContext>() {
@Override
protected TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {
private JavaTemplate createTemplate() {
return JavaTemplate.builder("@Override\n"
+ "public boolean isBeanExcludedFromAotProcessing() {\n"
+ " return false;\n"
+ "}\n"
)
.javaParser(JavaParser.fromJavaVersion())
.build();
}
private JavaTemplate createTemplate() {
return JavaTemplate.builder(this::getCursor, "@Override\n"
+ "public boolean isBeanExcludedFromAotProcessing() {\n"
+ " return false;\n"
+ "}\n"
)
.javaParser(() -> JavaParser.fromJavaVersion().build())
.build();
}
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext executionContext) {
J.ClassDeclaration c = super.visitClassDeclaration(classDecl, executionContext);
if (isApplicableClass(classDecl)) {
J.MethodDeclaration method = c.getBody().getStatements().stream().filter(J.MethodDeclaration.class::isInstance).map(J.MethodDeclaration.class::cast).filter(BeanPostProcessingIgnoreInAot::isApplicableMethod).findFirst().orElse(null);
if (method == null) {
c = c.withBody(createTemplate().apply(getCursor(), classDecl.getBody().getCoordinates().addMethodDeclaration((m1, m2) -> 1)));
}
}
return c;
}
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext executionContext) {
J.ClassDeclaration c = super.visitClassDeclaration(classDecl, executionContext);
if (isApplicableClass(classDecl)) {
J.MethodDeclaration method = c.getBody().getStatements().stream().filter(J.MethodDeclaration.class::isInstance).map(J.MethodDeclaration.class::cast).filter(BeanPostProcessingIgnoreInAot::isApplicableMethod).findFirst().orElse(null);
if (method == null) {
J.Block body = c.getBody().withTemplate(createTemplate(), classDecl.getBody().getCoordinates().addMethodDeclaration((m1, m2) -> 1));
c = c.withBody(body);
}
}
return c;
}
@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext executionContext) {
J.MethodDeclaration m = super.visitMethodDeclaration(method, executionContext);
if (isApplicableMethod(m)) {
J.ClassDeclaration c = getCursor().firstEnclosing(J.ClassDeclaration.class);
if (c != null && isApplicableClass(c) && isReturnTrue(m)) {
m = m.withTemplate(createTemplate(), m.getCoordinates().replace());
}
}
return m;
}
};
@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext executionContext) {
J.MethodDeclaration m = super.visitMethodDeclaration(method, executionContext);
if (isApplicableMethod(m)) {
J.ClassDeclaration c = getCursor().firstEnclosing(J.ClassDeclaration.class);
if (c != null && isApplicableClass(c) && isReturnTrue(m)) {
m = createTemplate().apply(getCursor(), m.getCoordinates().replace());
}
}
return m;
}
}
);
}
private static List<J.Return> findReturnStatementsInMethod(J.MethodDeclaration m) {

View File

@@ -11,6 +11,7 @@
package org.springframework.ide.vscode.commons.rewrite.java;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.search.UsesType;
@@ -34,13 +35,13 @@ public class ConvertAutowiredFieldIntoConstructorParameter extends Recipe {
}
@Override
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
return new UsesType<ExecutionContext>(AUTOWIRED, false);
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesType<ExecutionContext>(AUTOWIRED, false), new AutowiredFieldIntoConstructorParameterVisitor(classFqName, fieldName));
}
@Override
protected TreeVisitor<?, ExecutionContext> getVisitor() {
return new AutowiredFieldIntoConstructorParameterVisitor(classFqName, fieldName);
public String getDescription() {
return "Converts autowired fields into constructor parameters";
}
}

View File

@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2022 VMware, Inc.
* Copyright (c) 2022, 2023 VMware, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -59,7 +59,7 @@ public class DefineMethod extends Recipe {
}
@Override
protected TreeVisitor<?, ExecutionContext> getVisitor() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {
private MethodMatcher matcher = new MethodMatcher(targetFqName + ' ' + signature);
@@ -80,15 +80,14 @@ public class DefineMethod extends Recipe {
if (template != null && methodPresent == null) {
JavaType.FullyQualified type = c.getType();
if (type != null && targetFqName.equals(type.getFullyQualifiedName())) {
JavaTemplate t = JavaTemplate.builder(() -> getCursor(), template)
.javaParser(() -> JavaParser
JavaTemplate t = JavaTemplate.builder(template)
.javaParser(JavaParser
.fromJavaVersion()
.dependsOn(typeStubs.toArray(new String[typeStubs.size()]))
.classpath(classpath.stream().map(s -> Paths.get(s)).collect(Collectors.toList()))
.build())
.classpath(classpath.stream().map(s -> Paths.get(s)).collect(Collectors.toList())))
.imports(imports.toArray(new String[imports.size()])).build();
J.Block body = classDecl.getBody().withTemplate(t, classDecl.getBody().getCoordinates().addMethodDeclaration((m, n) -> 1));
J.Block body = t.apply(getCursor(), classDecl.getBody().getCoordinates().addMethodDeclaration((m, n) -> 1));
for (String fq : imports) {
maybeAddImport(fq);
}
@@ -100,4 +99,9 @@ public class DefineMethod extends Recipe {
};
}
@Override
public String getDescription() {
return "Defines a bean declaration method in a type";
}
}

View File

@@ -34,7 +34,6 @@ import org.openrewrite.Recipe;
import org.openrewrite.SourceFile;
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.RecipeIntrospectionUtils;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaParser.Builder;
@@ -297,7 +296,7 @@ public class ORAstUtils {
ctx.putMessage(JavaParser.SKIP_SOURCE_SET_TYPE_GENERATION, true);
List<CompilationUnit> cus = Collections.emptyList();
synchronized(parser) {
cus = parser.parse(sourceFiles, null, ctx);
cus = parser.parse(sourceFiles, null, ctx).collect(Collectors.toList());
}
List<J.CompilationUnit> finalCus = new ArrayList<>(cus.size());
for (CompilationUnit cu : cus) {
@@ -322,7 +321,7 @@ public class ORAstUtils {
List<CompilationUnit> cus = Collections.emptyList();
// long start = System.currentTimeMillis();
synchronized (parser) {
cus = parser.parseInputs(inputs, null, ctx);
cus = parser.parseInputs(inputs, null, ctx).collect(Collectors.toList());
}
// log.info("Rewrite parser: " + (System.currentTimeMillis() - start));
List<J.CompilationUnit> finalCus = new ArrayList<>(cus.size());
@@ -379,17 +378,17 @@ public class ORAstUtils {
}
@SuppressWarnings("unchecked")
private static List<TreeVisitor<J, ExecutionContext>> getAfterVisitors(TreeVisitor<J, ExecutionContext> visitor) {
private static List<TreeVisitor<?, ExecutionContext>> getAfterVisitors(TreeVisitor<?, ExecutionContext> visitor) {
try {
Method m = TreeVisitor.class.getDeclaredMethod("getAfterVisit");
m.setAccessible(true);
return (List<TreeVisitor<J, ExecutionContext>>) m.invoke(visitor);
return (List<TreeVisitor<?, ExecutionContext>>) m.invoke(visitor);
} catch (Exception e) {
return Collections.emptyList();
}
}
private static void makeVisitorNonTopLevel(JavaVisitor<ExecutionContext> visitor) {
private static void makeVisitorNonTopLevel(TreeVisitor<?, ExecutionContext> visitor) {
try {
Field f = TreeVisitor.class.getDeclaredField("afterVisit");
f.setAccessible(true);
@@ -403,18 +402,17 @@ public class ORAstUtils {
return new NodeRecipe((JavaVisitor<ExecutionContext>) v, condition);
}
@SuppressWarnings("unchecked")
public static Recipe nodeRecipe(Recipe r, Predicate<J> condition) {
return new NodeRecipe((JavaVisitor<ExecutionContext>) RecipeIntrospectionUtils.recipeVisitor(r), condition);
return new NodeRecipe(r.getVisitor(), condition);
}
private static class NodeRecipe extends Recipe {
private JavaVisitor<ExecutionContext> visitor;
private TreeVisitor<?, ExecutionContext> visitor;
private Predicate<J> condition;
public NodeRecipe(JavaVisitor<ExecutionContext> visitor, Predicate<J> condition) {
this.visitor = visitor;
public NodeRecipe(TreeVisitor<?,ExecutionContext> treeVisitor, Predicate<J> condition) {
this.visitor = treeVisitor;
this.condition = condition;
}
@@ -424,7 +422,7 @@ public class ORAstUtils {
}
@Override
protected TreeVisitor<?, ExecutionContext> getVisitor() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaVisitor<>() {
@Override
@@ -433,8 +431,8 @@ public class ORAstUtils {
J t = (J) tree;
if (condition.test(t)) {
makeVisitorNonTopLevel(visitor);
t = visitor.visit(t, ctx, getCursor());
for (TreeVisitor<J, ExecutionContext> v : getAfterVisitors(visitor)) {
t = (J) visitor.visit(t, ctx, getCursor());
for (TreeVisitor<?, ExecutionContext> v : getAfterVisitors(visitor)) {
doAfterVisit(v);
}
return t;
@@ -444,7 +442,12 @@ public class ORAstUtils {
}
};
}
}
@Override
public String getDescription() {
return "";
}
}
public static boolean isExceptionFromInterrupedThread(Throwable t) {

View File

@@ -17,7 +17,7 @@ import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.UnaryOperator;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -65,7 +65,7 @@ public abstract class ProjectParser {
javaParser.setClasspath(mainClasspath);
List<CompilationUnit> javaSources = ListUtils.map(javaParser.parseInputs(
() -> getInputs(ss.javaSources.stream()).iterator(), projectDir, ctx), addProvenance(projectProvenance));
() -> getInputs(ss.javaSources.stream()).iterator(), projectDir, ctx).collect(Collectors.toList()), addProvenance(projectProvenance));
JavaSourceSet javaSourceSet = ORAstUtils.addJavaSourceSet(javaSources, ss.name(),
mainClasspath);
sources.addAll(javaSources);
@@ -92,7 +92,7 @@ public abstract class ProjectParser {
.collect(Collectors.toList()),
projectDirectory,
ctx
), addProvenance(provenance)));
).collect(Collectors.toList()), addProvenance(provenance)));
sourceFiles.addAll(ListUtils.map(new YamlParser().parseInputs(
resources.stream()
@@ -100,7 +100,7 @@ public abstract class ProjectParser {
.collect(Collectors.toList()),
projectDirectory,
ctx
), addProvenance(provenance)));
).collect(Collectors.toList()), addProvenance(provenance)));
sourceFiles.addAll(ListUtils.map(new PropertiesParser().parseInputs(
resources.stream()
@@ -108,7 +108,7 @@ public abstract class ProjectParser {
.collect(Collectors.toList()),
projectDirectory,
ctx
), addProvenance(provenance)));
).collect(Collectors.toList()), addProvenance(provenance)));
sourceFiles.addAll(ListUtils.map(new PlainTextParser().parseInputs(
resources.stream()
@@ -116,7 +116,7 @@ public abstract class ProjectParser {
.collect(Collectors.toList()),
projectDirectory,
ctx
), addProvenance(provenance)));
).collect(Collectors.toList()), addProvenance(provenance)));
}
@@ -127,8 +127,8 @@ public abstract class ProjectParser {
return s;
}
private <S extends SourceFile> UnaryOperator<S> addProvenance(List<Marker> projectProvenance) {
return s -> {
private <S extends SourceFile> BiFunction<Integer, S, S> addProvenance(List<Marker> projectProvenance) {
return (i, s) -> {
if (projectProvenance != null) {
s = addProjectProvenance(s, projectProvenance);
}

View File

@@ -23,6 +23,7 @@ import java.util.Properties;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.openrewrite.ExecutionContext;
@@ -103,7 +104,7 @@ public class MavenIJavaProjectParser extends AbstractJavaProjectParser {
@Override
protected List<? extends SourceFile> parseBuildFiles(Path projectDir, ExecutionContext ctx) {
MavenParser mavenParser = mavenParserBuilder.build();
return mavenParser.parseInputs(() -> getInputs(Stream.of(Paths.get(jp.getProjectBuild().getBuildFile()))).iterator(), projectDir, ctx);
return mavenParser.parseInputs(() -> getInputs(Stream.of(Paths.get(jp.getProjectBuild().getBuildFile()))).iterator(), projectDir, ctx).collect(Collectors.toList());
}
private static ResolvedPom getModel(Xml.Document maven) {

View File

@@ -44,11 +44,10 @@ public class LoadUtilsTest {
@SuppressWarnings("unchecked")
@Test
public void createRecipeTest() throws Exception {
Recipe r = env.listRecipes().stream().filter(d -> "org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_0".equals(d.getName())).findFirst().orElse(null);
RecipeDescriptor recipeDescriptor = r.getDescriptor();
RecipeDescriptor recipeDescriptor = env.listRecipeDescriptors().stream().filter(d -> "org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_0".equals(d.getName())).findFirst().orElse(null);
assertNotNull(recipeDescriptor);
r = LoadUtils.createRecipe(recipeDescriptor, id -> {
Recipe r = LoadUtils.createRecipe(recipeDescriptor, id -> {
try {
return (Class<Recipe>) Class.forName(id);
} catch (ClassNotFoundException e) {

View File

@@ -113,9 +113,12 @@
<commons-codec-version>1.13</commons-codec-version>
<!-- Rewrite specific properties -->
<rewrite-version>7.41.0-SNAPSHOT</rewrite-version>
<rewrite-spring-version>4.37.0-SNAPSHOT</rewrite-spring-version>
<rewrite-gradle-tooling-api-version>0.9.0-SNAPSHOT</rewrite-gradle-tooling-api-version>
<rewrite-version>8.1.0-SNAPSHOT</rewrite-version>
<rewrite-spring-version>5.1.0-SNAPSHOT</rewrite-spring-version>
<rewrite-gradle-tooling-api-version>1.1.0-SNAPSHOT</rewrite-gradle-tooling-api-version>
<gradle-tooling.version>7.6</gradle-tooling.version>
<gradle-core.version>6.1.1</gradle-core.version>
<signing.skip>true</signing.skip>
<signing.alias>vmware</signing.alias>

View File

@@ -48,6 +48,7 @@ import org.openrewrite.Validated;
import org.openrewrite.config.DeclarativeRecipe;
import org.openrewrite.config.RecipeDescriptor;
import org.openrewrite.config.YamlResourceLoader;
import org.openrewrite.internal.InMemoryLargeSourceSet;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.tree.J.CompilationUnit;
import org.openrewrite.maven.MavenParser;
@@ -427,8 +428,8 @@ public class RewriteRecipeRepository implements ApplicationContextAware {
});
List<SourceFile> sources = projectParser.parse(absoluteProjectDir, new InMemoryExecutionContext());
progressTask.progressEvent("Computing changes...");
RecipeRun reciperun = r.run(sources, new InMemoryExecutionContext(e -> log.error("Recipe execution failed", e)));
List<Result> results = reciperun.getResults();
RecipeRun reciperun = r.run(new InMemoryLargeSourceSet(sources), new InMemoryExecutionContext(e -> log.error("Recipe execution failed", e)));
List<Result> results = reciperun.getChangeset().getAllResults();
return ORDocUtils.createWorkspaceEdit(absoluteProjectDir, server.getTextDocumentService(), results);
}

View File

@@ -27,14 +27,16 @@ import org.eclipse.lsp4j.TextDocumentEdit;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Parser.Input;
import org.openrewrite.Recipe;
import org.openrewrite.RecipeRun;
import org.openrewrite.Result;
import org.openrewrite.SourceFile;
import org.openrewrite.config.DeclarativeRecipe;
import org.openrewrite.internal.InMemoryLargeSourceSet;
import org.openrewrite.internal.RecipeIntrospectionUtils;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.J.CompilationUnit;
import org.openrewrite.marker.Range;
import org.slf4j.Logger;
@@ -121,9 +123,10 @@ public class RewriteRefactorings implements CodeActionResolver, QuickfixHandler
return null;
}
private WorkspaceEdit applyRecipe(Recipe r, IJavaProject project, List<J.CompilationUnit> cus) {
RecipeRun reciperun = r.run(cus);
List<Result> results = reciperun.getResults();
private WorkspaceEdit applyRecipe(Recipe r, IJavaProject project, List<CompilationUnit> cus) {
List<SourceFile> sources = cus.stream().map(cu -> (SourceFile) cu).collect(Collectors.toList());
RecipeRun reciperun = r.run(new InMemoryLargeSourceSet(sources), new InMemoryExecutionContext());
List<Result> results = reciperun.getChangeset().getAllResults();
List<Either<TextDocumentEdit, ResourceOperation>> edits = results.stream().filter(res -> res.getAfter() != null).map(res -> {
URI docUri = res.getAfter().getSourcePath().isAbsolute() ? res.getAfter().getSourcePath().toUri() : project.getLocationUri().resolve(res.getAfter().getSourcePath().toString());
TextDocument doc = server.getTextDocumentService().getLatestSnapshot(docUri.toASCIIString());

View File

@@ -102,8 +102,8 @@ public class SpringBootUpgrade {
if (version.getMajor() == targetVersion.getMajor() && version.getMinor() == targetVersion.getMinor()) {
// patch version upgrade - treat as pom versions only upgrade
recipe.doNext(new UpgradeDependencyVersion("org.springframework.boot", "*", version.getMajor() + "." + version.getMinor() + ".x", null, null, null));
recipe.doNext(new UpgradeParentVersion("org.springframework.boot", "spring-boot-starter-parent", version.getMajor() + "." + version.getMinor() + ".x", null, null));
recipe.getRecipeList().add(new UpgradeDependencyVersion("org.springframework.boot", "*", version.getMajor() + "." + version.getMinor() + ".x", null, null, null));
recipe.getRecipeList().add(new UpgradeParentVersion("org.springframework.boot", "spring-boot-starter-parent", version.getMajor() + "." + version.getMinor() + ".x", null, null));
} else /*if (version.getMajor() == targetVersion.getMajor())*/ {
List<String> recipedIds = createRecipeIdsChain(version.getMajor(), version.getMinor() + 1, targetVersion.getMajor(), targetVersion.getMinor(), versionsToRecipeId);
if (!recipedIds.isEmpty()) {
@@ -117,7 +117,7 @@ public class SpringBootUpgrade {
// getRecipeFromId(recipedIds.get(i)).ifPresent(recipe::doNext);
// }
// }
getRecipeFromId(recipeId).ifPresent(recipe::doNext);
getRecipeFromId(recipeId).ifPresent(r -> recipe.getRecipeList().add(r));
}
}