Skip to content

Finish #803 #860

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 3 commits into from
Aug 5, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ private RecipeTestSupport() {
DefaultActionDeserializer.class,
RewriteJavaSearchActionDeserializer.class,
RewriteRecipeLoader.class,
RewriteRecipeRunner.class,
RewriteMigrationResultMerger.class,
RewriteSourceFileWrapper.class,
SbmRecipeLoader.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,26 @@
*/
package org.springframework.sbm.engine.context;

import lombok.Getter;
import lombok.Setter;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.Result;
import org.openrewrite.SourceFile;
import org.openrewrite.java.JavaParser;
import org.springframework.sbm.build.api.ApplicationModules;
import org.springframework.sbm.build.api.Module;
import org.springframework.sbm.build.api.BuildFile;
import org.springframework.sbm.build.api.Module;
import org.springframework.sbm.build.api.RootBuildFileFilter;
import org.springframework.sbm.build.filter.BuildFileProjectResourceFilter;
import org.springframework.sbm.engine.recipe.OpenRewriteSourceFilesFinder;
import org.springframework.sbm.engine.recipe.RewriteMigrationResultMerger;
import org.springframework.sbm.java.api.ProjectJavaSources;
import org.springframework.sbm.java.impl.ProjectJavaSourcesImpl;
import org.springframework.sbm.java.refactoring.JavaRefactoringFactory;
import org.springframework.sbm.java.util.BasePackageCalculator;
import org.springframework.sbm.project.resource.ProjectResourceSet;
import org.springframework.sbm.project.resource.filter.ProjectResourceFinder;
import lombok.Getter;
import lombok.Setter;

import java.nio.file.Path;
import java.util.List;
Expand All @@ -46,14 +51,16 @@ public class ProjectContext {
private String revision;
private final JavaParser javaParser;
private final ExecutionContext executionContext;
private final RewriteMigrationResultMerger resultMerger;

public ProjectContext(JavaRefactoringFactory javaRefactoringFactory, Path projectRootDirectory, ProjectResourceSet projectResources, BasePackageCalculator basePackageCalculator, JavaParser javaParser, ExecutionContext executionContext) {
public ProjectContext(JavaRefactoringFactory javaRefactoringFactory, Path projectRootDirectory, ProjectResourceSet projectResources, BasePackageCalculator basePackageCalculator, JavaParser javaParser, ExecutionContext executionContext, RewriteMigrationResultMerger resultMerger) {
this.projectRootDirectory = projectRootDirectory.toAbsolutePath();
this.projectResources = projectResources;
this.javaRefactoringFactory = javaRefactoringFactory;
this.basePackageCalculator = basePackageCalculator;
this.javaParser = javaParser;
this.executionContext = executionContext;
this.resultMerger = resultMerger;
}

public ProjectResourceSet getProjectResources() {
Expand Down Expand Up @@ -103,4 +110,9 @@ public ApplicationModules getApplicationModules() {
return new ApplicationModules(getModules());
}

public void apply(Recipe recipe) {
List<? extends SourceFile> rewriteSourceFiles = this.search(new OpenRewriteSourceFilesFinder());
List<Result> results = recipe.run(rewriteSourceFiles, executionContext).getResults();
resultMerger.mergeResults(this, results);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.openrewrite.java.JavaParser;
import org.springframework.sbm.build.api.BuildFile;
import org.springframework.sbm.build.filter.BuildFileProjectResourceFilter;
import org.springframework.sbm.engine.recipe.RewriteMigrationResultMerger;
import org.springframework.sbm.java.refactoring.JavaRefactoringFactory;
import org.springframework.sbm.java.impl.ClasspathRegistry;
import org.springframework.sbm.java.util.BasePackageCalculator;
Expand All @@ -41,14 +42,15 @@ public class ProjectContextFactory {
private final BasePackageCalculator basePackageCalculator;
private final JavaParser javaParser;
private final ExecutionContext executionContext;
private final RewriteMigrationResultMerger resultMerger;

@NotNull
public ProjectContext createProjectContext(Path projectDir, ProjectResourceSet projectResourceSet) {
projectResourceSetHolder.setProjectResourceSet(projectResourceSet);
applyProjectResourceWrappers(projectResourceSet);
List<BuildFile> buildFiles = new BuildFileProjectResourceFilter().apply(projectResourceSet);
ClasspathRegistry.initializeFromBuildFiles(buildFiles);
ProjectContext projectContext = new ProjectContext(javaRefactoringFactory, projectDir, projectResourceSet, basePackageCalculator, javaParser, executionContext);
ProjectContext projectContext = new ProjectContext(javaRefactoringFactory, projectDir, projectResourceSet, basePackageCalculator, javaParser, executionContext, resultMerger);
return projectContext;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ public class OpenRewriteDeclarativeRecipeAdapter extends AbstractAction {
@JsonIgnore
@Setter
private RewriteRecipeLoader rewriteRecipeLoader;
@JsonIgnore
@Autowired
@Setter
private RewriteRecipeRunner rewriteRecipeRunner;

public OpenRewriteDeclarativeRecipeAdapter() {
super(builder());
Expand All @@ -51,6 +47,6 @@ public boolean isApplicable(ProjectContext context) {
@Override
public void apply(ProjectContext context) {
Recipe recipe = rewriteRecipeLoader.createRecipe(openRewriteRecipe);
rewriteRecipeRunner.run(context, recipe);
context.apply(recipe);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,9 @@ public class OpenRewriteNamedRecipeAdapter extends AbstractAction {
@JsonIgnore
private RewriteRecipeLoader rewriteRecipeLoader;

@JsonIgnore
@Autowired
private RewriteRecipeRunner rewriteRecipeRunner;

@Override
public void apply(ProjectContext context) {
Recipe recipe = rewriteRecipeLoader.loadRewriteRecipe(openRewriteRecipeName);
rewriteRecipeRunner.run(context, recipe);
context.apply(recipe);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
RewritePlainTextParser.class,
RewriteMavenParser.class,
MavenSettingsInitializer.class,
RewriteMigrationResultMerger.class,
RewriteMavenArtifactDownloader.class,
JavaProvenanceMarkerFactory.class,
MavenConfigHandler.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.io.IOException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

@SpringBootTest(classes = {
RecipeParser.class,
Expand All @@ -43,7 +42,6 @@
ActionDeserializerRegistry.class,
DefaultActionDeserializer.class,
RewriteMigrationResultMerger.class,
RewriteRecipeRunner.class,
RewriteSourceFileWrapper.class,
CustomValidatorBean.class,
RewriteExecutionContext.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,20 @@ public class OpenRewriteDeclarativeRecipeAdapterTest {
@Mock
RewriteRecipeLoader rewriteRecipeLoader;

@Mock
RewriteRecipeRunner rewriteRecipeRunner;

@InjectMocks
OpenRewriteDeclarativeRecipeAdapter sut;

@Test
void testApply() {
String recipeDeclaration = "name: some-recipe";
sut.setOpenRewriteRecipe(recipeDeclaration);

org.openrewrite.Recipe recipe = mock(org.openrewrite.Recipe.class);
when(rewriteRecipeLoader.createRecipe(recipeDeclaration)).thenReturn(recipe);
ProjectContext context = mock(ProjectContext.class);

sut.apply(context);

verify(rewriteRecipeRunner).run(context, recipe);
verify(context).apply(recipe);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
ActionDeserializerRegistry.class,
DefaultActionDeserializer.class,
RewriteMigrationResultMerger.class,
RewriteRecipeRunner.class,
RewriteSourceFileWrapper.class,
RewriteRecipeLoader.class,
CustomValidatorBean.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,15 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.project.RewriteSourceFileWrapper;
import org.springframework.sbm.project.resource.ResourceHelper;
import org.springframework.sbm.project.resource.TestProjectContext;
import org.springframework.validation.beanvalidation.CustomValidatorBean;

import java.io.IOException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
public class OpenRewriteNamedRecipeAdapterTest {
@Mock
RewriteRecipeLoader rewriteRecipeLoader;

@Mock
RewriteRecipeRunner rewriteRecipeRunner;

@InjectMocks
OpenRewriteNamedRecipeAdapter sut;

Expand All @@ -57,7 +44,7 @@ void testApply() {

sut.apply(context);

verify(rewriteRecipeRunner).run(context, recipe);
verify(context).apply(recipe);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.springframework.sbm.engine.context.ProjectRootPathResolver;
import org.springframework.sbm.engine.git.GitSupport;
import org.springframework.sbm.engine.precondition.PreconditionVerifier;
import org.springframework.sbm.engine.recipe.RewriteMigrationResultMerger;
import org.springframework.sbm.java.impl.RewriteJavaParser;
import org.springframework.sbm.java.refactoring.JavaRefactoringFactoryImpl;
import org.springframework.sbm.java.util.BasePackageCalculator;
Expand Down Expand Up @@ -75,6 +76,7 @@
ProjectContextFactory.class,
MavenPomCacheProvider.class,
SbmApplicationProperties.class,
RewriteMigrationResultMerger.class,
PathScanner.class,
RewriteJavaParser.class,
RewritePlainTextParser.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.springframework.sbm.engine.recipe.OpenRewriteDeclarativeRecipeAdapter;
import org.springframework.sbm.engine.recipe.Recipe;
import org.springframework.sbm.engine.recipe.RewriteRecipeLoader;
import org.springframework.sbm.engine.recipe.RewriteRecipeRunner;
import org.springframework.sbm.java.JavaRecipeAction;
import org.springframework.sbm.java.impl.ClasspathRegistry;
import org.springframework.sbm.java.migration.actions.ReplaceTypeAction;
Expand All @@ -50,7 +49,7 @@ public class MigrateJaxRsRecipe {
private final Supplier<JavaParser> javaParserSupplier = () -> JavaParser.fromJavaVersion().classpath(ClasspathRegistry.getInstance().getCurrentDependencies()).build();

@Bean
public Recipe jaxRs(RewriteRecipeLoader rewriteRecipeLoader, RewriteRecipeRunner rewriteRecipeRunner) {
public Recipe jaxRs(RewriteRecipeLoader rewriteRecipeLoader) {
return Recipe.builder()
.name("migrate-jax-rs")
.order(60)
Expand Down Expand Up @@ -137,7 +136,6 @@ public Recipe jaxRs(RewriteRecipeLoader rewriteRecipeLoader, RewriteRecipeRunner
.condition(HasAnnotation.builder().annotation("org.springframework.web.bind.annotation.RequestParam").build())
.description("Adds required=false to all @RequestParam annotations")
.rewriteRecipeLoader(rewriteRecipeLoader)
.rewriteRecipeRunner(rewriteRecipeRunner)
.openRewriteRecipe(
"""
type: specs.openrewrite.org/v1beta/recipe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@
*/
package org.springframework.sbm.jee.jaxrs.recipes;

import org.springframework.sbm.engine.recipe.OpenRewriteDeclarativeRecipeAdapter;
import org.springframework.sbm.java.impl.RewriteJavaParser;
import org.springframework.sbm.project.resource.SbmApplicationProperties;
import org.springframework.sbm.test.RecipeTestSupport;
import org.junit.jupiter.api.Test;
import org.springframework.sbm.build.migration.actions.AddDependencies;
import org.springframework.sbm.engine.recipe.OpenRewriteDeclarativeRecipeAdapter;
import org.springframework.sbm.engine.recipe.Recipe;
import org.springframework.sbm.java.JavaRecipeAction;
import org.springframework.sbm.java.migration.actions.ReplaceTypeAction;
import org.springframework.sbm.jee.jaxrs.MigrateJaxRsRecipe;
import org.springframework.sbm.jee.jaxrs.actions.ConvertJaxRsAnnotations;
import org.junit.jupiter.api.Test;
import org.springframework.sbm.test.RecipeTestSupport;

import java.util.Optional;

Expand All @@ -34,7 +32,7 @@ public class BootifyJaxRsAnnotationsRecipeTest {
@Test
void test() {

Recipe jaxRsRecipe = new MigrateJaxRsRecipe().jaxRs(null, null);
Recipe jaxRsRecipe = new MigrateJaxRsRecipe().jaxRs(null);
Optional<Recipe> recipe = Optional.of(jaxRsRecipe);
RecipeTestSupport.assertThatRecipeExists(recipe);
RecipeTestSupport.assertThatRecipeHasActions(recipe,
Expand Down