Skip to content

upgrade to openrewrite 7.22.0 #174

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 1 commit into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/mvn-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
java-version: '11'
distribution: 'adopt'
- name: Build with Maven
run: mvn --update-snapshots -DskipITs verify
run: mvn --update-snapshots -DtrimStackTrace=false -Dsurefire.useFile=false -DskipITs verify
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
*/
package org.springframework.sbm;

import org.springframework.sbm.build.migration.conditions.AnyDependencyExistMatchingRegex;
import org.openrewrite.maven.AddPluginDependency;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.sbm.build.impl.OpenRewriteMavenBuildFile;
import org.springframework.sbm.build.migration.conditions.AnyDependencyExistMatchingRegex;
import org.springframework.sbm.build.migration.recipe.RemoveMavenPlugin;
import org.springframework.sbm.engine.recipe.Action;
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.maven.AddPluginDependency;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ void retrieveOpenRewriteRecipeFromClasspath() {
@Test
void getClasspathJars() {
String[] property = System.getProperty("java.class.path").split(System.getProperty("path.separator"));
Arrays.stream(property).forEach(System.out::println);
// Arrays.stream(property).forEach(System.out::println);
assertThat(property).isNotEmpty();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.sbm.engine.context.RewriteJavaSearchActionDeserializer;
import org.springframework.sbm.engine.recipe.*;
import org.springframework.sbm.java.impl.RewriteJavaParser;
import org.springframework.sbm.java.util.BasePackageCalculator;
import org.springframework.sbm.project.resource.SbmApplicationProperties;
import org.springframework.sbm.project.resource.ResourceHelper;
Expand Down Expand Up @@ -54,6 +55,7 @@ private RecipeTestSupport() {
RecipesBuilder.class,
ResourceHelper.class,
RecipeParser.class,
RewriteJavaParser.class,
CustomValidator.class,
ValidatorConfiguration.class,
YamlObjectMapperConfiguration.class,
Expand Down
17 changes: 12 additions & 5 deletions components/sbm-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-yaml</artifactId>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-json</artifactId>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-properties</artifactId>
Expand All @@ -69,6 +73,14 @@
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-java-11</artifactId>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-protobuf</artifactId>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-hcl</artifactId>
</dependency>
<!-- TODO: bind to profile, currently not in use -->
<!-- <dependency>-->
<!-- <groupId>org.mapdb</groupId>-->
Expand Down Expand Up @@ -139,11 +151,6 @@
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
*/
package org.springframework.sbm.build.api;

import org.openrewrite.java.JavaParser;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.springframework.sbm.build.impl.JavaSourceSetImpl;
import org.springframework.sbm.build.impl.MavenBuildFileUtil;
import org.springframework.sbm.build.impl.OpenRewriteMavenBuildFile;
import org.springframework.sbm.java.api.JavaSource;
import org.springframework.sbm.java.api.JavaSourceLocation;
Expand All @@ -27,7 +30,6 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.openrewrite.SourceFile;
import org.openrewrite.maven.tree.Modules;

import java.nio.file.Path;
import java.util.ArrayList;
Expand All @@ -49,6 +51,7 @@ public class ApplicationModule {
private final ProjectResourceSet projectResourceSet;
private final JavaRefactoringFactory javaRefactoringFactory;
private final BasePackageCalculator basePackageCalculator;
private final JavaParser javaParser;

public JavaSourceLocation getBaseJavaSourceLocation() {
return getMainJavaSourceSet().getJavaSourceLocation();
Expand All @@ -60,7 +63,8 @@ public JavaSourceLocation getBaseTestJavaSourceLocation() {

public JavaSourceSet getTestJavaSourceSet() {
Path testJavaPath = Path.of("src/test/java");
return new JavaSourceSetImpl(projectResourceSet, projectRootDir, modulePath, testJavaPath, javaRefactoringFactory, basePackageCalculator);
// FIXME: #7 JavaParser
return new JavaSourceSetImpl(projectResourceSet, projectRootDir, modulePath, testJavaPath, javaRefactoringFactory, basePackageCalculator, javaParser);
}

public List<? extends JavaSource> getMainJavaSources() {
Expand All @@ -76,7 +80,7 @@ public List<? extends JavaSource> getTestJavaSources() {
public JavaSourceSet getMainJavaSourceSet() {
Path mainJavaPath = Path.of("src/main/java");
// return new JavaSourceSetImpl(projectResourceSet, projectRootDir.resolve(modulePath).resolve(mainJavaPath), javaRefactoringFactory);
return new JavaSourceSetImpl(projectResourceSet, projectRootDir, modulePath, mainJavaPath, javaRefactoringFactory, basePackageCalculator);
return new JavaSourceSetImpl(projectResourceSet, projectRootDir, modulePath, mainJavaPath, javaRefactoringFactory, basePackageCalculator, javaParser);
}

private List<JavaSource> cast(List<RewriteSourceFileHolder<? extends SourceFile>> filter) {
Expand Down Expand Up @@ -109,19 +113,20 @@ public ResourceSet getMainResourceSet() {
}

public List<ApplicationModule> getModules() {
Optional<Modules> modulesMarker = ((OpenRewriteMavenBuildFile) buildFile).getPom().getMarkers().findFirst(Modules.class);
if (modulesMarker.isPresent()) {
return modulesMarker.get()
.getModules()
Optional<MavenResolutionResult> mavenResolution = MavenBuildFileUtil.findMavenResolution(((OpenRewriteMavenBuildFile) buildFile).getSourceFile());
List<MavenResolutionResult> modulesMarker = mavenResolution.get().getModules();
if ( ! modulesMarker.isEmpty()) {
return modulesMarker
.stream()
.map(m -> new ApplicationModule(
m.getName(),
m.getPom().getGav().toString(),
this.buildFile,
projectRootDir,
modulePath,
projectResourceSet,
javaRefactoringFactory,
basePackageCalculator
basePackageCalculator,
javaParser
))
.collect(Collectors.toList());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/
package org.springframework.sbm.build.api;

import org.springframework.sbm.build.impl.OpenRewriteMavenBuildFile;
import org.jetbrains.annotations.NotNull;
import org.openrewrite.maven.tree.Modules;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.springframework.sbm.build.impl.MavenBuildFileUtil;
import org.springframework.sbm.build.impl.OpenRewriteMavenBuildFile;

import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -60,43 +61,43 @@ public ApplicationModule getModule(String name) {
}

public List<ApplicationModule> getModules(ApplicationModule module) {
Optional<Modules> modulesMarker = ((OpenRewriteMavenBuildFile) module.getBuildFile()).getPom().getMarkers().findFirst(Modules.class);
if (modulesMarker.isPresent()) {
return getModulesForMarkers(modulesMarker.get());
MavenResolutionResult mavenResolutionResult = MavenBuildFileUtil.findMavenResolution(((OpenRewriteMavenBuildFile) module.getBuildFile()).getSourceFile()).get();
List<MavenResolutionResult> modulesMarker = mavenResolutionResult.getModules();
if (!modulesMarker.isEmpty()) {
return filterModulesContainingMavens(modulesMarker);
} else {
return new ArrayList<>();
}
}

@NotNull
private List<ApplicationModule> getModulesForMarkers(Modules modulesMarker) {
List<String> collect = modulesMarker.getModules().stream()
.map(m -> m.getGroupId() + ":" + m.getArtifactId())
private List<ApplicationModule> filterModulesContainingMavens(List<MavenResolutionResult> modulesMarker) {
List<String> collect = modulesMarker.stream()
.map(m -> m.getPom().getGroupId() + ":" + m.getPom().getArtifactId())
.collect(Collectors.toList());

List<ApplicationModule> modules = this.modules.stream()
return modules.stream()
.filter(module -> {
String groupAndArtifactId = module.getBuildFile().getGroupId() + ":" + module.getBuildFile().getArtifactId();
return collect.contains(groupAndArtifactId);
})
.collect(Collectors.toList());
return modules;
}

public List<ApplicationModule> getTopmostApplicationModules() {
List<ApplicationModule> topmostModules = new ArrayList<>();
modules.forEach(module -> {
// is jar
if ("jar".equals(module.getBuildFile().getPackaging())) {
if ("jar".equals(module.getBuildFile().getPackaging())) { // FIXME: other types could be topmost too, e.g. 'war'
// no other pom depends on this pom in its dependency section
if (noOtherPomDependsOn(module.getBuildFile())) {
// has no parent or parent has packaging pom
ParentDeclaration parentPomDeclaration = module.getBuildFile().getParentPomDeclaration();
if (parentPomDeclaration == null) {
Optional<ParentDeclaration> parentPomDeclaration = module.getBuildFile().getParentPomDeclaration();
if (parentPomDeclaration.isEmpty()) {
topmostModules.add(module);
} else if (isDeclaredInProject(parentPomDeclaration) && isPackagingOfPom(parentPomDeclaration)) {
} else if (isDeclaredInProject(parentPomDeclaration.get()) && isPackagingOfPom(parentPomDeclaration.get())) {
topmostModules.add(module);
} else if (!isDeclaredInProject(parentPomDeclaration)) {
} else if (!isDeclaredInProject(parentPomDeclaration.get())) {
topmostModules.add(module);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
*/
package org.springframework.sbm.build.api;

import org.springframework.sbm.project.resource.ProjectResource;
import org.openrewrite.maven.tree.Pom;
import org.openrewrite.maven.tree.Scope;
import org.springframework.sbm.project.resource.ProjectResource;

import java.nio.file.Path;
import java.util.List;
Expand All @@ -26,9 +25,14 @@

public interface BuildFile extends ProjectResource {

List<Dependency> getDeclaredDependencies();
List<Dependency> getDeclaredDependencies(Scope... scopes);

Set<Pom.Dependency> getEffectiveDependencies(Scope scope);
/**
* Returns any available dependency (declared or transitive) with given scope.
*/
Set<Dependency> getEffectiveDependencies(Scope scope);

Set<Dependency> getEffectiveDependencies();

/**
* Check if any declared dependency matches any of the given regex.
Expand All @@ -46,8 +50,16 @@ public interface BuildFile extends ProjectResource {

boolean hasExactDeclaredDependency(Dependency dependency);

/**
* Add a dependency to the build file and reparse Java sources.
*
* Always prefer {@link #addDependencies(List)} instead of looping this method.
*/
void addDependency(Dependency dependency);

/**
* Add a list of dependencies to the build file and reparse Java sources.
*/
void addDependencies(List<Dependency> dependencies);

void removeDependencies(List<Dependency> dependencies);
Expand Down Expand Up @@ -89,6 +101,9 @@ public interface BuildFile extends ProjectResource {

String print();

/**
* Get the packaging type for this BuildFile.
*/
String getPackaging();

void setPackaging(String packaging);
Expand All @@ -113,7 +128,7 @@ public interface BuildFile extends ProjectResource {

void upgradeParentVersion(String version);

ParentDeclaration getParentPomDeclaration();
Optional<ParentDeclaration> getParentPomDeclaration();

Optional<String> getName();

Expand All @@ -125,4 +140,7 @@ public interface BuildFile extends ProjectResource {
*/
void excludeDependencies(List<Dependency> excludedDependencies);

void addRepository(RepositoryDefinition repository);

List<RepositoryDefinition> getRepositories();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.springframework.sbm.build.api.JavaSourceSet;
import org.springframework.sbm.java.api.JavaSource;
import org.springframework.sbm.java.api.JavaSourceLocation;
import org.springframework.sbm.java.impl.JavaParserFactory;
import org.springframework.sbm.java.impl.OpenRewriteJavaSource;
import org.springframework.sbm.java.refactoring.JavaRefactoringFactory;
import org.springframework.sbm.java.util.BasePackageCalculator;
Expand All @@ -43,10 +42,12 @@ public class JavaSourceSetImpl implements JavaSourceSet {
private final Path sourceSetRoot;
private final JavaRefactoringFactory javaRefactoringFactory;
private final BasePackageCalculator basePackageCalculator;
private final JavaParser javaParser;

public JavaSourceSetImpl(ProjectResourceSet projectResourceSet, Path projectRootDir, Path modulePath, Path mainJavaPath, JavaRefactoringFactory javaRefactoringFactory, BasePackageCalculator basePackageCalculator) {
public JavaSourceSetImpl(ProjectResourceSet projectResourceSet, Path projectRootDir, Path modulePath, Path mainJavaPath, JavaRefactoringFactory javaRefactoringFactory, BasePackageCalculator basePackageCalculator, JavaParser javaParser) {
this.projectResourceSet = projectResourceSet;
this.basePackageCalculator = basePackageCalculator;
this.javaParser = javaParser;
this.sourceSetRoot = projectRootDir.resolve(modulePath).resolve(mainJavaPath);
this.filter = (r) -> {
return r.getAbsolutePath().getParent().normalize().toString().startsWith(sourceSetRoot.toString());
Expand All @@ -60,7 +61,7 @@ public JavaSourceSetImpl(ProjectResourceSet projectResourceSet, Path projectRoot
@Override
@Deprecated(forRemoval = true)
public JavaSource addJavaSource(Path projectRoot, Path sourceFolder, String sourceCode, String packageName) {
JavaParser javaParser = JavaParserFactory.getCurrentJavaParser();
// FIXME: #7 JavaParser
javaParser.reset();
List<J.CompilationUnit> compilationUnits = javaParser.parse(sourceCode);
J.CompilationUnit parsedCompilationUnit = compilationUnits.get(0);
Expand All @@ -70,7 +71,7 @@ public JavaSource addJavaSource(Path projectRoot, Path sourceFolder, String sour
throw new RuntimeException("The Java class you tried to add already lives here: '" + sourceFilePath + "'.");
} else {
J.CompilationUnit compilationUnit = parsedCompilationUnit.withSourcePath(sourceFilePath);
OpenRewriteJavaSource addedSource = new OpenRewriteJavaSource(projectRoot, compilationUnit, javaRefactoringFactory.createRefactoring(compilationUnit));
OpenRewriteJavaSource addedSource = new OpenRewriteJavaSource(projectRoot, compilationUnit, javaRefactoringFactory.createRefactoring(compilationUnit), javaParser);
addedSource.markChanged();
projectResourceSet.add(addedSource);
return addedSource;
Expand All @@ -79,7 +80,7 @@ public JavaSource addJavaSource(Path projectRoot, Path sourceFolder, String sour

@Override
public List<JavaSource> addJavaSource(Path projectRoot, Path sourceFolder, String... sourceCodes) {
JavaParser javaParser = JavaParserFactory.getCurrentJavaParser();
// FIXME: #7 JavaParser
javaParser.reset();

List<J.CompilationUnit> compilationUnits = javaParser.parse(sourceCodes);
Expand All @@ -91,7 +92,7 @@ public List<JavaSource> addJavaSource(Path projectRoot, Path sourceFolder, Strin
Path sourceFilePath = sourceFolder.resolve(sourceFileName);
if(!Files.exists(sourceFilePath)) {
J.CompilationUnit compilationUnit = cu.withSourcePath(sourceFilePath);
OpenRewriteJavaSource addedSource = new OpenRewriteJavaSource(projectRoot, compilationUnit, javaRefactoringFactory.createRefactoring(compilationUnit));
OpenRewriteJavaSource addedSource = new OpenRewriteJavaSource(projectRoot, compilationUnit, javaRefactoringFactory.createRefactoring(compilationUnit), javaParser);
addedSource.markChanged();
projectResourceSet.add(addedSource);
addedSources.add(addedSource);
Expand Down
Loading