Skip to content

Commit

Permalink
It all works
Browse files Browse the repository at this point in the history
  • Loading branch information
fabapp2 committed Aug 6, 2024
1 parent 54db42d commit 98dbbb1
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 359 deletions.
1 change: 1 addition & 0 deletions components/jaxrs-recipes/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@
<!-- <version>2.1.0</version>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->

</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,29 @@
package example.recipe;


import freemarker.template.Configuration;
import org.openrewrite.*;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.maven.MavenSettings;
import org.openrewrite.maven.cache.LocalMavenArtifactCache;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.rewrite.parser.JavaParserBuilder;
import org.springframework.rewrite.parser.maven.MavenSettingsInitializer;
import org.springframework.rewrite.parser.maven.RewriteMavenArtifactDownloader;
import org.springframework.rewrite.resource.*;
import org.springframework.rewrite.scopes.ProjectMetadata;
import org.springframework.sbm.build.impl.MavenBuildFileRefactoringFactory;
import org.springframework.sbm.build.impl.RewriteMavenParser;
import org.springframework.sbm.build.resource.BuildFileResourceWrapper;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.engine.context.ProjectContextFactory;
import org.springframework.sbm.engine.recipe.RewriteRecipeLoader;
import org.springframework.sbm.java.JavaSourceProjectResourceWrapper;
import org.springframework.sbm.java.refactoring.JavaRefactoringFactory;
import org.springframework.sbm.java.refactoring.JavaRefactoringFactoryImpl;
import org.springframework.sbm.java.util.BasePackageCalculator;
import org.springframework.sbm.jee.jaxrs.MigrateJaxRsRecipe;
import org.springframework.sbm.jee.jaxrs.actions.ConvertJaxRsAnnotations;
import org.springframework.sbm.project.resource.ProjectResourceSetHolder;
import org.springframework.sbm.project.resource.ProjectResourceWrapper;
Expand Down Expand Up @@ -78,103 +91,35 @@ public TreeVisitor<?, ExecutionContext> getScanner(List<SourceFile> acc) {
}

@Override
public Collection<? extends SourceFile> generate(List<SourceFile> acc, ExecutionContext executionContext) {
Collection<? extends SourceFile> generate = super.generate(acc, executionContext);
public Collection<? extends SourceFile> generate(List<SourceFile> generate, ExecutionContext executionContext) {
// Collection<? extends SourceFile> generate = super.generate(acc, executionContext);

// create the required classes
initBeans(executionContext);

// transform nodes to SourceFiles
List<SourceFile> sourceFiles = acc.stream().filter(SourceFile.class::isInstance).map(SourceFile.class::cast).toList();
List<SourceFile> sourceFiles = generate.stream().filter(SourceFile.class::isInstance).map(SourceFile.class::cast).toList();

// FIXME: base dir calculation is fake
Path baseDir = Path.of("/Users/fkrueger/projects/spring-boot-migrator/components/jaxrs-recipes/testcode/jee/jaxrs/bootify-jaxrs/given").toAbsolutePath().normalize(); //executionContext.getMessage("base.dir");
Path baseDir = Path.of(".").resolve("testcode/jee/jaxrs/bootify-jaxrs/given").toAbsolutePath().normalize(); //executionContext.getMessage("base.dir");

// Create the SBM resource set abstraction
ProjectResourceSet projectResourceSet = projectResourceSetFactory.create(baseDir, sourceFiles);
// Create the SBM ProjectContext
ProjectContext pc = projectContextFactory.createProjectContext(baseDir, projectResourceSet);

// Execute the SBM Action = the JAXRS Recipe
new ConvertJaxRsAnnotations().apply(pc);
// new ConvertJaxRsAnnotations().apply(pc);

RewriteRecipeLoader recipeLoader = new RewriteRecipeLoader();
new MigrateJaxRsRecipe().jaxRs(recipeLoader).apply(pc);

// Merge back result
List<? extends SourceFile> modifiedNodes = merge(sourceFiles, pc.getProjectResources());

return modifiedNodes;
}

// @Override
// public List<Recipe> getRecipeList() {
// List<Recipe> recipeList = new ArrayList<>();
// recipeList.add(new GenericOpenRewriteRecipe<>(() -> new TreeVisitor<Tree, ExecutionContext>() {
// @Override
// public void visit(@Nullable List<? extends Tree> nodes, ExecutionContext executionContext) {
// super.visit(nodes, executionContext);
// }
//
// @Override
// public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext executionContext) {
// return super.visit(tree, executionContext);
// }
//
// @Override
// public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext executionContext, Cursor parent) {
// return super.visit(tree, executionContext, parent);
// }
// }));
// return recipeList;
// }

// @Override
// public TreeVisitor<?, ExecutionContext> getVisitor() {
// return new TreeVisitor<Tree, ExecutionContext>() {
//
// private ProjectResourceSetFactory projectResourceSetFactory;
// private ProjectContextFactory projectContextFactory;
//
// @Override
// public Tree visitNonNull(Tree tree, ExecutionContext executionContext) {
// return super.visitNonNull(tree, executionContext);
// }
//
// @Override
// public boolean isAcceptable(SourceFile sourceFile, ExecutionContext executionContext) {
// return true;
// }
//
// @Override
// public void visit(@Nullable List<? extends Tree> nodes, ExecutionContext executionContext) {
//
// super.visit(nodes, executionContext);
//
// // create the required classes
// initBeans(executionContext);
//
// // transform nodes to SourceFiles
// List<SourceFile> sourceFiles = nodes.stream().filter(SourceFile.class::isInstance).map(SourceFile.class::cast).toList();
//
// // FIXME: base dir calculation is fake
// Path baseDir = executionContext.getMessage("base.dir");
//
// // Create the SBM resource set abstraction
// ProjectResourceSet projectResourceSet = projectResourceSetFactory.create(baseDir, sourceFiles);
// // Create the SBM ProjectContext
// ProjectContext pc = projectContextFactory.createProjectContext(baseDir, projectResourceSet);
//
// // Execute the SBM Action = the JAXRS Recipe
// new ConvertJaxRsAnnotations().apply(pc);
//
// // Merge back result
// List<? extends Tree> modifiedNodes = merge(nodes, pc.getProjectResources());
//
// // Process other
// super.visit(modifiedNodes, executionContext);
// }
//
// };
// }

private List<? extends SourceFile> merge(List<SourceFile> nodes, ProjectResourceSet projectResources) {
// merge the changed results into the given list and return the result
ArrayList<SourceFile> merged = new ArrayList<>();
Expand All @@ -195,23 +140,35 @@ private int findPosition(List<SourceFile> merged, RewriteSourceFileHolder<? exte


private void initBeans(ExecutionContext executionContext) {
RewriteSourceFileWrapper sourceFileWrapper = new RewriteSourceFileWrapper();
SbmApplicationProperties sbmApplicationProperties = new SbmApplicationProperties();
JavaParserBuilder parserBuilder = new JavaParserBuilder();
List<ProjectResourceWrapper> projectResourceWrappers = new ArrayList<>();
RewriteMigrationResultMerger merger = new RewriteMigrationResultMerger(sourceFileWrapper);
ProjectResourceSetHolder holder = new ProjectResourceSetHolder(executionContext, merger);
JavaRefactoringFactory refactoringFactory = new JavaRefactoringFactoryImpl(holder, executionContext);
projectResourceWrappers.add(new JavaSourceProjectResourceWrapper(refactoringFactory, parserBuilder, executionContext));


projectResourceSetFactory = new ProjectResourceSetFactory(new RewriteMigrationResultMerger(sourceFileWrapper), sourceFileWrapper, executionContext);
ProjectResourceWrapperRegistry registry = new ProjectResourceWrapperRegistry(projectResourceWrappers);
BasePackageCalculator calculator = new BasePackageCalculator(sbmApplicationProperties);

ProjectResourceSetFactory resourceSetFactory = new ProjectResourceSetFactory(merger, sourceFileWrapper, executionContext);

projectContextFactory = new ProjectContextFactory(registry, holder, refactoringFactory, calculator, parserBuilder, executionContext, merger, resourceSetFactory);
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext("org.springframework.freemarker", "org.springframework.sbm", "org.springframework.rewrite");
ctx.register(Configuration.class);
this.projectContextFactory = ctx.getBean(ProjectContextFactory.class);
this.projectResourceSetFactory = ctx.getBean(ProjectResourceSetFactory.class);
// RewriteSourceFileWrapper sourceFileWrapper = new RewriteSourceFileWrapper();
// SbmApplicationProperties sbmApplicationProperties = new SbmApplicationProperties();
// JavaParserBuilder parserBuilder = new JavaParserBuilder();
// List<ProjectResourceWrapper> projectResourceWrappers = new ArrayList<>();
// RewriteMigrationResultMerger merger = new RewriteMigrationResultMerger(sourceFileWrapper);
// ProjectResourceSetHolder holder = new ProjectResourceSetHolder(executionContext, merger);
// JavaRefactoringFactory refactoringFactory = new JavaRefactoringFactoryImpl(holder, executionContext);
// projectResourceWrappers.add(new JavaSourceProjectResourceWrapper(refactoringFactory, parserBuilder, executionContext));
// ProjectMetadata projectMetadata = new ProjectMetadata();
// MavenBuildFileRefactoringFactory buildFileRefactoringFactory = new MavenBuildFileRefactoringFactory(holder, new RewriteMavenParser(new MavenSettingsInitializer(executionContext, projectMetadata), executionContext), executionContext);
// projectResourceWrappers.add(new BuildFileResourceWrapper(
// event -> System.out.println(event),
// buildFileRefactoringFactory,
// executionContext,
// new RewriteMavenArtifactDownloader(new LocalMavenArtifactCache(Path.of(System.getProperty("user.dir")).resolve(".m2/repository")), new MavenSettings(), t -> {throw new RuntimeException(t);}))
// );
//
//
// projectResourceSetFactory = new ProjectResourceSetFactory(new RewriteMigrationResultMerger(sourceFileWrapper), sourceFileWrapper, executionContext);
// ProjectResourceWrapperRegistry registry = new ProjectResourceWrapperRegistry(projectResourceWrappers);
// BasePackageCalculator calculator = new BasePackageCalculator(sbmApplicationProperties);
//
// ProjectResourceSetFactory resourceSetFactory = new ProjectResourceSetFactory(merger, sourceFileWrapper, executionContext);
//
// projectContextFactory = new ProjectContextFactory(registry, holder, refactoringFactory, calculator, parserBuilder, executionContext, merger, resourceSetFactory);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2021 - 2023 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;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* @author Fabian Krüger
*/
@Configuration
public class FreemarkerConfiguration {
@Bean
public freemarker.template.Configuration configuration() {
return new freemarker.template.Configuration();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2021 - 2023 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;

import org.springframework.sbm.engine.recipe.UserInteractions;
import org.springframework.stereotype.Component;

@Component
public class UserInteractionsDummy implements UserInteractions {
@Override
public boolean askUserYesOrNo(String question) {
return true;
}

@Override
public String askForInput(String question) {
return "answer";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ public String getDescription() {

@Override
public boolean evaluate(ProjectContext context) {
return true;
// context.getProjectJavaSources().asStream()
// .flatMap(js -> js.getTypes().stream())
// .anyMatch(t -> t.hasAnnotation(annotation));
return context.getProjectJavaSources().asStream()
.flatMap(js -> js.getTypes().stream())
.anyMatch(t -> t.hasAnnotation(annotation));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ public Recipe jaxRs(RewriteRecipeLoader rewriteRecipeLoader) {
.condition(HasImportStartingWith.builder().value("javax.ws.rs").description("Any class has import starting with javax.ws.rs").build())
.actions(List.of(

AddDependencies.builder()
.dependencies(
List.of(
Dependency.builder().groupId("org.springframework.boot").artifactId("spring-boot-starter-web").version("2.3.4.RELEASE").build()
)
)
.description("Add spring-boot-starter-web dependency to build file.")
.condition(NoExactDependencyExist.builder().dependency(Dependency.builder().groupId("org.springframework.boot").artifactId("spring-boot-starter-web").build()).build())
.build(),
// AddDependencies.builder()
// .dependencies(
// List.of(
// Dependency.builder().groupId("org.springframework.boot").artifactId("spring-boot-starter-web").version("2.3.4.RELEASE").build()
// )
// )
// .description("Add spring-boot-starter-web dependency to build file.")
// .condition(NoExactDependencyExist.builder().dependency(Dependency.builder().groupId("org.springframework.boot").artifactId("spring-boot-starter-web").build()).build())
// .build(),

ConvertJaxRsAnnotations.builder()
.condition(HasTypeAnnotation.builder().annotation("javax.ws.rs.Path").build())
Expand Down
Loading

0 comments on commit 98dbbb1

Please sign in to comment.