Skip to content

Upgrade openrewrite 7.35.0 #728

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

Merged
merged 42 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
c0476d8
refactor: Formatting, check marker exists before adding
fabapp2 Feb 10, 2023
94366a6
fix: Use nullsafe equals
fabapp2 Feb 10, 2023
c319eb3
refactor: Remove obsolete test and strange reference to the same
fabapp2 Feb 10, 2023
e7bf266
cleanup: Delete obsolete test
fabapp2 Feb 16, 2023
81683f8
doc: Add javadoc
fabapp2 Feb 16, 2023
d8a54db
dep: Bump Open Rewrite to 7.35.0
fabapp2 Feb 16, 2023
09353e3
OpenRewrite 7.35.0 uses snakeyaml 1.33
fabapp2 Feb 16, 2023
3ca8a26
fix: ResourceParser excludes parsers with no input
fabapp2 Feb 16, 2023
5528bdd
refactor: Improve adding dependencies by adding all at once instead o…
fabapp2 Feb 16, 2023
7ecb56c
refactor: Improve exception message
fabapp2 Feb 16, 2023
16623c1
enhance OpenRewriteMavenBuildFile to take and provide MavenBuildFileR…
fabapp2 Feb 16, 2023
794490b
refactor: Replace concatenated with multiline string
fabapp2 Feb 16, 2023
0fd37ab
test: Enabled test again
fabapp2 Feb 16, 2023
0452472
test: refactor and add assertions
fabapp2 Feb 16, 2023
eacb3a4
test: Refactor and add test
fabapp2 Feb 16, 2023
6f7ae6a
test: Fix test by replacing dummy with real dependencies
fabapp2 Feb 17, 2023
e742f44
fix: Filter local projects when resolving the paths to dependency jars
fabapp2 Feb 17, 2023
4f53710
refactor: Handle exception in print()
fabapp2 Feb 19, 2023
6f150bb
fix: Replace pom model refresh
fabapp2 Feb 19, 2023
887d3b7
fix: Set release and snapshot property for Maven repository
fabapp2 Feb 19, 2023
d06fffc
refactor: Replace deprecated method call
fabapp2 Feb 19, 2023
1f07b0c
doc: Fiddle with comments after update of asciidoclet
fabapp2 Feb 19, 2023
0c16a08
cleanup: Replace concatenated with multiline String
fabapp2 Feb 19, 2023
0d314f1
Adopt Api to 7.35.
fabapp2 Feb 20, 2023
3249c25
cleanup: Cleanup comment
fabapp2 Feb 20, 2023
9f396e5
cleanup: Remove unused imports
fabapp2 Feb 20, 2023
78e155d
refactor: Make JavaParser final
fabapp2 Feb 20, 2023
bb7bc4b
test: Add (disabled) test for adding dependencies
fabapp2 Feb 20, 2023
80ab67f
test: Add test for deletion of Maven property in multi module project
fabapp2 Feb 20, 2023
2653659
test: Add test for setting a property in a pom file
fabapp2 Feb 20, 2023
6af6f57
test: Add test for sorting poms in MavenProjectParser
fabapp2 Feb 20, 2023
abdc103
test: Add tests for GH issues 2601 and 2624
fabapp2 Feb 20, 2023
c4ed32e
test: Delete obsolete test
fabapp2 Feb 20, 2023
38a29a6
test: Fix test data
fabapp2 Feb 20, 2023
b75a9a3
test: Fix UpgradeDepenenciesMigrationTest by adding blank lines to ex…
fabapp2 Feb 20, 2023
48efd21
This is useful when a stateful JavParser has been created by Spring B…
fabapp2 Feb 20, 2023
0594307
test: Refactor add dependency test
fabapp2 Feb 20, 2023
a45b7a8
test: Set packaging to pom in test data
fabapp2 Feb 20, 2023
62b2d0c
fix: Fix Removal of redundant compiler plugins
fabapp2 Feb 20, 2023
059c5a6
fix: Use RewriteJavaParser and set changed classpath
fabapp2 Feb 20, 2023
497f5c4
test: Fix ConvertJaxRsAnnotationTest
fabapp2 Feb 20, 2023
18430c8
test: Fix by adding expected dependency (again)
fabapp2 Feb 20, 2023
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 @@ -16,6 +16,7 @@
package org.springframework.sbm;

import org.openrewrite.maven.AddPluginDependency;
import org.openrewrite.maven.UpdateMavenModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.sbm.build.impl.OpenRewriteMavenBuildFile;
Expand All @@ -25,6 +26,7 @@
import org.springframework.sbm.engine.recipe.Condition;
import org.springframework.sbm.engine.recipe.Recipe;
import org.springframework.sbm.spring.migration.actions.OpenRewriteRecipeAdapterAction;
import org.springframework.sbm.support.openrewrite.GenericOpenRewriteRecipe;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
Expand Down Expand Up @@ -69,7 +71,7 @@ Recipe recipeSpringBoot1To2Migration(RewriteRecipesRepository repo) {

r.doNext(new RemoveMavenPlugin("org.codehaus.mojo", "cobertura-maven-plugin"));
r.doNext(new AddPluginDependency("ro.isdc.wro4j", "wro4j-maven-plugin", "org.mockito", "mockito-core", "${mockito.version}"));
r.doNext(new OpenRewriteMavenBuildFile.RefreshPomModel());
r.doNext(new GenericOpenRewriteRecipe<>(() -> new UpdateMavenModel<>()));

AnyDeclaredDependencyExistMatchingRegex condition = new AnyDeclaredDependencyExistMatchingRegex();
condition.setDependencies(List.of("org\\.springframework\\.boot:.*:1\\..*"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void recipeUpdatesBootDependenciesAndParentVersion() throws IOException {

List<Resource> resources = List.of();

ProjectContext projectContext = contextInitializer.initProjectContext(Path.of("./testcode/boot-23-app/given"), resources, new RewriteExecutionContext());
ProjectContext projectContext = contextInitializer.initProjectContext(Path.of("./testcode/boot-23-app/given"), resources);
projectContext.getApplicationModules().getRootModule().getMainResourceSet().addStringResource("src/main/resources/data.sql", "# Empty file");


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static ProjectContext createProjectContextFromDir(String projectRootDir,
ProjectContextInitializer projectContextBuilder = ctx.getBean(ProjectContextInitializer.class);
ScanCommand scanCommand = ctx.getBean(ScanCommand.class);
List<Resource> resources = scanCommand.scanProjectRoot(to.toString());
ProjectContext projectContext = projectContextBuilder.initProjectContext(projectRoot, resources, new RewriteExecutionContext());
ProjectContext projectContext = projectContextBuilder.initProjectContext(projectRoot, resources);
projectContextHolder.setContext(projectContext);
},
beanClasses.toArray(new Class[]{})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.springframework.sbm.test;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.sbm.build.impl.MavenBuildFileRefactoringFactory;
import org.springframework.sbm.build.impl.MavenSettingsInitializer;
import org.springframework.sbm.build.impl.RewriteMavenParser;
import org.springframework.sbm.engine.context.ProjectContextHolder;
Expand All @@ -24,6 +25,7 @@
import org.springframework.sbm.java.impl.RewriteJavaParser;
import org.springframework.sbm.java.util.BasePackageCalculator;
import org.springframework.sbm.project.RewriteSourceFileWrapper;
import org.springframework.sbm.project.resource.ProjectResourceSetHolder;
import org.springframework.sbm.project.resource.SbmApplicationProperties;
import org.springframework.sbm.project.resource.ResourceHelper;
import org.springframework.sbm.search.recipe.actions.OpenRewriteJavaSearchAction;
Expand Down Expand Up @@ -73,7 +75,9 @@ private RecipeTestSupport() {
BasePackageCalculator.class,
ProjectContextHolder.class,
RewriteMavenParser.class,
MavenSettingsInitializer.class
MavenSettingsInitializer.class,
MavenBuildFileRefactoringFactory.class,
ProjectResourceSetHolder.class
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

Expand Down Expand Up @@ -87,7 +86,9 @@ public interface BuildFile extends ProjectResource {

void removeDependenciesInner(List<Dependency> dependencies);

List<Dependency> getDependencyManagement();
List<Dependency> getEffectiveDependencyManagement();

List<Dependency> getRequestedDependencyManagement();

List<Dependency> getRequestedManagedDependencies();

Expand Down Expand Up @@ -115,6 +116,9 @@ public interface BuildFile extends ProjectResource {

Path getMainResourceFolder();

/**
* Sets existing property or adds new property.
*/
void setProperty(String key, String value);

String getProperty(String key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.sbm.build.api;

import org.openrewrite.maven.MavenDownloadingException;
import org.openrewrite.maven.internal.MavenPomDownloader;
import org.openrewrite.maven.tree.GroupArtifactVersion;
import org.openrewrite.maven.tree.MavenRepository;
Expand All @@ -32,7 +33,7 @@
public class SpringManagedDependencies {

private static List<MavenRepository> SPRING_REPOSITORIES = List.of(
new MavenRepository("spring-release", "https://repo.spring.io/release", true, false, null, null)
new MavenRepository("spring-release", "https://repo.spring.io/release", "true", "false", true, null, null, null)
);

private List<org.openrewrite.maven.tree.Dependency> dependencies;
Expand All @@ -47,9 +48,13 @@ public static SpringManagedDependencies by(String groupId, String artifact, Stri
}

private SpringManagedDependencies(GroupArtifactVersion groupArtifactVersion){
dependencies = new MavenPomDownloader(Collections.emptyMap(), new RewriteExecutionContext())
.download(groupArtifactVersion, null, null, SPRING_REPOSITORIES)
.getDependencies();
try {
dependencies = new MavenPomDownloader(Collections.emptyMap(), new RewriteExecutionContext())
.download(groupArtifactVersion, null, null, SPRING_REPOSITORIES)
.getDependencies();
} catch (MavenDownloadingException e) {
throw new RuntimeException(e);
}
}

public Stream<Dependency> stream(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,43 @@
*/
package org.springframework.sbm.build.impl;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.openrewrite.Recipe;
import org.openrewrite.Result;
import org.openrewrite.maven.MavenParser;
import org.jetbrains.annotations.NotNull;
import org.openrewrite.*;
import org.openrewrite.marker.Markers;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.maven.MavenVisitor;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.xml.tree.Xml;
import org.springframework.sbm.openrewrite.RewriteExecutionContext;
import org.springframework.sbm.project.resource.ProjectResourceSet;
import org.springframework.sbm.project.resource.RewriteSourceFileHolder;
import org.springframework.sbm.support.openrewrite.GenericOpenRewriteRecipe;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* This class provides a facade to apply OpenRewrite {@code Recipe}s and {@code Visitor}s to the project Maven build files.
*
* @author Fabian Krüger
*/
@RequiredArgsConstructor
class MavenBuildFileRefactoring<P> {

private final RewriteSourceFileHolder<Xml.Document> pom;
public class MavenBuildFileRefactoring<T extends SourceFile> {
private final ProjectResourceSet projectResourceSet;
private final RewriteMavenParser mavenParser;

/**
* Applies the provided {@code Visitor}s to all Maven build files in the {@code ProjectContext}.
*
* The changes are immediately reflected in the wrapping {@code BuildFile}s.
*/
public void execute(MavenVisitor... visitors) {
List<Result> results = Arrays.stream(visitors)
.map(v -> new GenericOpenRewriteRecipe(() -> v))
Expand All @@ -43,32 +61,136 @@ public void execute(MavenVisitor... visitors) {
processResults(results);
}

public void execute(Recipe... visitors) {
for (Recipe recipe : visitors) {
/**
* Applies the provided {@code Recipe}s to all Maven build files in the {@code ProjectContext}.
*
* The changes are immediately reflected in the wrapping {@code BuildFile}s.
*/
public void execute(Recipe... recipes) {
for (Recipe recipe : recipes) {
List<Result> results = executeRecipe(recipe);
processResults(results);
}
}

/**
* Applies the provided {@code Recipe}s to the provided Maven build file.
*
* The changes are immediately reflected in the wrapping {@code BuildFile}.
* A caller must decide if refreshing the Pom files in {@code ProjectContext} is required after this method.
*/
public <V extends TreeVisitor<?, ExecutionContext>> void execute(RewriteSourceFileHolder<Xml.Document> resource, Recipe... recipes) {
for (Recipe recipe : recipes) {
List<Result> results = executeRecipe(recipe, resource);
processResults(results);
}
}

public void refreshPomModels() {
// store buildfiles and their index in project resource list
List<BuildFileWithIndex> buildFilesWithIndex = new ArrayList<>();
List<RewriteSourceFileHolder<? extends SourceFile>> projectResources = projectResourceSet.list();
for(RewriteSourceFileHolder<? extends SourceFile> sf : projectResources) {
if(isMavenBuildFile(sf)) {
int index = projectResources.indexOf(sf);
Xml.Document xmlDoc = (Xml.Document) sf.getSourceFile();
buildFilesWithIndex.add(new BuildFileWithIndex(index, (RewriteSourceFileHolder<Xml.Document>) sf));
}
}

// create parser inputs from buildfiles content
List<Parser.Input> parserInputs = buildFilesWithIndex
.stream()
.map(BuildFileWithIndex::getXmlDoc)
.map(m -> new Parser.Input(m.getSourcePath(), null, () -> new ByteArrayInputStream(
m.print().getBytes(StandardCharsets.UTF_8)), !Files.exists(m.getSourcePath())))
.collect(Collectors.toList());

// parse buildfiles
List<Xml.Document> newMavenFiles = mavenParser.parseInputs(parserInputs, null, new RewriteExecutionContext());

// replace new model in build files
newMavenFiles.stream()
.forEach(mf -> {
replaceModelInBuildFile(projectResources, buildFilesWithIndex, newMavenFiles, mf);
});
}

private void replaceModelInBuildFile(
List<RewriteSourceFileHolder<? extends SourceFile>> projectResources,
List<BuildFileWithIndex> buildFilesWithIndex,
List<Xml.Document> newMavenFiles,
Xml.Document mf
) {
// get index in list of build files
int indexInNewMavenFiles = newMavenFiles.indexOf(mf);
RewriteSourceFileHolder<Xml.Document> originalPom = buildFilesWithIndex.get(indexInNewMavenFiles).getXmlDoc();
int indexInProjectResources = projectResources.indexOf(originalPom);
// replace marker
Markers markers = originalPom.getSourceFile().getMarkers().removeByType(MavenResolutionResult.class);
MavenResolutionResult updatedModel = mf.getMarkers().findFirst(MavenResolutionResult.class).get();
markers = markers.addIfAbsent(updatedModel);
Xml.Document refreshedPom = originalPom.getSourceFile().withMarkers(markers);
RewriteSourceFileHolder<Xml.Document> rewriteSourceFileHolder = (RewriteSourceFileHolder<Xml.Document>) projectResources.get(indexInProjectResources);
rewriteSourceFileHolder.replaceWith(refreshedPom);
}

private boolean isMavenBuildFile(RewriteSourceFileHolder<? extends SourceFile> sf) {
return Xml.Document.class.isInstance(sf.getSourceFile()) && Xml.Document.class.cast(sf.getSourceFile()).getMarkers().findFirst(MavenResolutionResult.class).isPresent();
}

@Getter
class BuildFileWithIndex {
private final int index;
private final RewriteSourceFileHolder<Xml.Document> xmlDoc;

public BuildFileWithIndex(int index, RewriteSourceFileHolder<Xml.Document> xmlDoc) {

this.index = index;
this.xmlDoc = xmlDoc;
}
}

private List<Result> executeRecipe(Recipe recipe) {
List<Result> results = recipe.run(List.of(pom.getSourceFile()), new RewriteExecutionContext()).getResults();
List<Result> results = recipe.run(getDocumentsWrappedInOpenRewriteMavenBuildFile(), new RewriteExecutionContext()).getResults();
return results;
}

private List<Result> executeRecipe(Recipe recipe, RewriteSourceFileHolder<Xml.Document> resource) {
List<Result> results = recipe.run(List.of(resource.getSourceFile()), new RewriteExecutionContext()).getResults();
return results;
}

private List<Xml.Document> getDocumentsWrappedInOpenRewriteMavenBuildFile() {
return getOpenRewriteMavenBuildFiles()
.stream()
.map(bf -> bf.getSourceFile())
.collect(Collectors.toList());
}

@NotNull
private List<OpenRewriteMavenBuildFile> getOpenRewriteMavenBuildFiles() {
return this.projectResourceSet
.stream()
.filter(r -> OpenRewriteMavenBuildFile.class.isInstance(r))
.map(OpenRewriteMavenBuildFile.class::cast)
.collect(Collectors.toList());
}

private void processResults(List<Result> results) {
if (!results.isEmpty()) {
// FIXME: Works only on a single POM and does not apply to all other resources
pom.replaceWith((Xml.Document) results.get(0).getAfter());
// results.forEach(c -> processResult(c));
results.forEach(r -> {
if(!(r.getAfter() instanceof Xml.Document)) {
throw new RuntimeException("Return type of refactoring result is not Xml.Document but " + r.getAfter().getClass() + " with content: \n" + r.getAfter().printAll());
}
OpenRewriteMavenBuildFile openRewriteMavenBuildFile = getOpenRewriteMavenBuildFiles()
.stream()
.filter(bf -> bf.getSourceFile().getId().equals(r.getAfter().getId()))
.findFirst()
.orElseThrow(() -> new RuntimeException("Could not find a BuildFile that wraps Xml.Document with id '%s' in the Result.".formatted(r.getAfter().getId())));
openRewriteMavenBuildFile.replaceWith((Xml.Document) r.getAfter());
});
}
}

private void processResult(Result result) {
MavenParser parser = MavenParser
.builder()
.build();
Xml.Document wrappedMavenFile = parser.parse(result.getAfter().printAll()).get(0);
wrappedMavenFile = (Xml.Document) wrappedMavenFile.withSourcePath(pom.getSourceFile().getSourcePath());
pom.replaceWith(wrappedMavenFile);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2021 - 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.sbm.build.impl;

import lombok.RequiredArgsConstructor;
import org.openrewrite.xml.tree.Xml;
import org.springframework.sbm.project.resource.ProjectResourceSetHolder;
import org.springframework.stereotype.Component;

/**
* @author Fabian Krüger
*/
@Component
@RequiredArgsConstructor
public class MavenBuildFileRefactoringFactory {
private final ProjectResourceSetHolder projectResourceSetHolder;
private final RewriteMavenParser rewriteMavenParser;

public MavenBuildFileRefactoring createRefactoring() {
return new MavenBuildFileRefactoring<Xml.Document>(projectResourceSetHolder.getProjectResourceSet(), rewriteMavenParser);
}
}
Loading