Skip to content

Commit 98dbbb1

Browse files
committed
It all works
1 parent 54db42d commit 98dbbb1

File tree

10 files changed

+196
-359
lines changed

10 files changed

+196
-359
lines changed

components/jaxrs-recipes/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@
212212
<!-- <version>2.1.0</version>-->
213213
<!-- <scope>test</scope>-->
214214
<!-- </dependency>-->
215+
215216
</dependencies>
216217
<build>
217218
<plugins>

components/jaxrs-recipes/src/main/java/example/recipe/SbmAdapterRecipe.java

Lines changed: 50 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,29 @@
1616
package example.recipe;
1717

1818

19+
import freemarker.template.Configuration;
1920
import org.openrewrite.*;
2021
import org.openrewrite.internal.lang.Nullable;
22+
import org.openrewrite.maven.MavenSettings;
23+
import org.openrewrite.maven.cache.LocalMavenArtifactCache;
24+
import org.springframework.context.ApplicationContext;
25+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2126
import org.springframework.rewrite.parser.JavaParserBuilder;
27+
import org.springframework.rewrite.parser.maven.MavenSettingsInitializer;
28+
import org.springframework.rewrite.parser.maven.RewriteMavenArtifactDownloader;
2229
import org.springframework.rewrite.resource.*;
30+
import org.springframework.rewrite.scopes.ProjectMetadata;
31+
import org.springframework.sbm.build.impl.MavenBuildFileRefactoringFactory;
32+
import org.springframework.sbm.build.impl.RewriteMavenParser;
33+
import org.springframework.sbm.build.resource.BuildFileResourceWrapper;
2334
import org.springframework.sbm.engine.context.ProjectContext;
2435
import org.springframework.sbm.engine.context.ProjectContextFactory;
36+
import org.springframework.sbm.engine.recipe.RewriteRecipeLoader;
2537
import org.springframework.sbm.java.JavaSourceProjectResourceWrapper;
2638
import org.springframework.sbm.java.refactoring.JavaRefactoringFactory;
2739
import org.springframework.sbm.java.refactoring.JavaRefactoringFactoryImpl;
2840
import org.springframework.sbm.java.util.BasePackageCalculator;
41+
import org.springframework.sbm.jee.jaxrs.MigrateJaxRsRecipe;
2942
import org.springframework.sbm.jee.jaxrs.actions.ConvertJaxRsAnnotations;
3043
import org.springframework.sbm.project.resource.ProjectResourceSetHolder;
3144
import org.springframework.sbm.project.resource.ProjectResourceWrapper;
@@ -78,103 +91,35 @@ public TreeVisitor<?, ExecutionContext> getScanner(List<SourceFile> acc) {
7891
}
7992

8093
@Override
81-
public Collection<? extends SourceFile> generate(List<SourceFile> acc, ExecutionContext executionContext) {
82-
Collection<? extends SourceFile> generate = super.generate(acc, executionContext);
94+
public Collection<? extends SourceFile> generate(List<SourceFile> generate, ExecutionContext executionContext) {
95+
// Collection<? extends SourceFile> generate = super.generate(acc, executionContext);
8396

8497
// create the required classes
8598
initBeans(executionContext);
8699

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

90103
// FIXME: base dir calculation is fake
91-
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");
104+
Path baseDir = Path.of(".").resolve("testcode/jee/jaxrs/bootify-jaxrs/given").toAbsolutePath().normalize(); //executionContext.getMessage("base.dir");
92105

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

98111
// Execute the SBM Action = the JAXRS Recipe
99-
new ConvertJaxRsAnnotations().apply(pc);
112+
// new ConvertJaxRsAnnotations().apply(pc);
113+
114+
RewriteRecipeLoader recipeLoader = new RewriteRecipeLoader();
115+
new MigrateJaxRsRecipe().jaxRs(recipeLoader).apply(pc);
100116

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

104120
return modifiedNodes;
105121
}
106122

107-
// @Override
108-
// public List<Recipe> getRecipeList() {
109-
// List<Recipe> recipeList = new ArrayList<>();
110-
// recipeList.add(new GenericOpenRewriteRecipe<>(() -> new TreeVisitor<Tree, ExecutionContext>() {
111-
// @Override
112-
// public void visit(@Nullable List<? extends Tree> nodes, ExecutionContext executionContext) {
113-
// super.visit(nodes, executionContext);
114-
// }
115-
//
116-
// @Override
117-
// public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext executionContext) {
118-
// return super.visit(tree, executionContext);
119-
// }
120-
//
121-
// @Override
122-
// public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext executionContext, Cursor parent) {
123-
// return super.visit(tree, executionContext, parent);
124-
// }
125-
// }));
126-
// return recipeList;
127-
// }
128-
129-
// @Override
130-
// public TreeVisitor<?, ExecutionContext> getVisitor() {
131-
// return new TreeVisitor<Tree, ExecutionContext>() {
132-
//
133-
// private ProjectResourceSetFactory projectResourceSetFactory;
134-
// private ProjectContextFactory projectContextFactory;
135-
//
136-
// @Override
137-
// public Tree visitNonNull(Tree tree, ExecutionContext executionContext) {
138-
// return super.visitNonNull(tree, executionContext);
139-
// }
140-
//
141-
// @Override
142-
// public boolean isAcceptable(SourceFile sourceFile, ExecutionContext executionContext) {
143-
// return true;
144-
// }
145-
//
146-
// @Override
147-
// public void visit(@Nullable List<? extends Tree> nodes, ExecutionContext executionContext) {
148-
//
149-
// super.visit(nodes, executionContext);
150-
//
151-
// // create the required classes
152-
// initBeans(executionContext);
153-
//
154-
// // transform nodes to SourceFiles
155-
// List<SourceFile> sourceFiles = nodes.stream().filter(SourceFile.class::isInstance).map(SourceFile.class::cast).toList();
156-
//
157-
// // FIXME: base dir calculation is fake
158-
// Path baseDir = executionContext.getMessage("base.dir");
159-
//
160-
// // Create the SBM resource set abstraction
161-
// ProjectResourceSet projectResourceSet = projectResourceSetFactory.create(baseDir, sourceFiles);
162-
// // Create the SBM ProjectContext
163-
// ProjectContext pc = projectContextFactory.createProjectContext(baseDir, projectResourceSet);
164-
//
165-
// // Execute the SBM Action = the JAXRS Recipe
166-
// new ConvertJaxRsAnnotations().apply(pc);
167-
//
168-
// // Merge back result
169-
// List<? extends Tree> modifiedNodes = merge(nodes, pc.getProjectResources());
170-
//
171-
// // Process other
172-
// super.visit(modifiedNodes, executionContext);
173-
// }
174-
//
175-
// };
176-
// }
177-
178123
private List<? extends SourceFile> merge(List<SourceFile> nodes, ProjectResourceSet projectResources) {
179124
// merge the changed results into the given list and return the result
180125
ArrayList<SourceFile> merged = new ArrayList<>();
@@ -195,23 +140,35 @@ private int findPosition(List<SourceFile> merged, RewriteSourceFileHolder<? exte
195140

196141

197142
private void initBeans(ExecutionContext executionContext) {
198-
RewriteSourceFileWrapper sourceFileWrapper = new RewriteSourceFileWrapper();
199-
SbmApplicationProperties sbmApplicationProperties = new SbmApplicationProperties();
200-
JavaParserBuilder parserBuilder = new JavaParserBuilder();
201-
List<ProjectResourceWrapper> projectResourceWrappers = new ArrayList<>();
202-
RewriteMigrationResultMerger merger = new RewriteMigrationResultMerger(sourceFileWrapper);
203-
ProjectResourceSetHolder holder = new ProjectResourceSetHolder(executionContext, merger);
204-
JavaRefactoringFactory refactoringFactory = new JavaRefactoringFactoryImpl(holder, executionContext);
205-
projectResourceWrappers.add(new JavaSourceProjectResourceWrapper(refactoringFactory, parserBuilder, executionContext));
206-
207-
208-
projectResourceSetFactory = new ProjectResourceSetFactory(new RewriteMigrationResultMerger(sourceFileWrapper), sourceFileWrapper, executionContext);
209-
ProjectResourceWrapperRegistry registry = new ProjectResourceWrapperRegistry(projectResourceWrappers);
210-
BasePackageCalculator calculator = new BasePackageCalculator(sbmApplicationProperties);
211-
212-
ProjectResourceSetFactory resourceSetFactory = new ProjectResourceSetFactory(merger, sourceFileWrapper, executionContext);
213-
214-
projectContextFactory = new ProjectContextFactory(registry, holder, refactoringFactory, calculator, parserBuilder, executionContext, merger, resourceSetFactory);
143+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext("org.springframework.freemarker", "org.springframework.sbm", "org.springframework.rewrite");
144+
ctx.register(Configuration.class);
145+
this.projectContextFactory = ctx.getBean(ProjectContextFactory.class);
146+
this.projectResourceSetFactory = ctx.getBean(ProjectResourceSetFactory.class);
147+
// RewriteSourceFileWrapper sourceFileWrapper = new RewriteSourceFileWrapper();
148+
// SbmApplicationProperties sbmApplicationProperties = new SbmApplicationProperties();
149+
// JavaParserBuilder parserBuilder = new JavaParserBuilder();
150+
// List<ProjectResourceWrapper> projectResourceWrappers = new ArrayList<>();
151+
// RewriteMigrationResultMerger merger = new RewriteMigrationResultMerger(sourceFileWrapper);
152+
// ProjectResourceSetHolder holder = new ProjectResourceSetHolder(executionContext, merger);
153+
// JavaRefactoringFactory refactoringFactory = new JavaRefactoringFactoryImpl(holder, executionContext);
154+
// projectResourceWrappers.add(new JavaSourceProjectResourceWrapper(refactoringFactory, parserBuilder, executionContext));
155+
// ProjectMetadata projectMetadata = new ProjectMetadata();
156+
// MavenBuildFileRefactoringFactory buildFileRefactoringFactory = new MavenBuildFileRefactoringFactory(holder, new RewriteMavenParser(new MavenSettingsInitializer(executionContext, projectMetadata), executionContext), executionContext);
157+
// projectResourceWrappers.add(new BuildFileResourceWrapper(
158+
// event -> System.out.println(event),
159+
// buildFileRefactoringFactory,
160+
// executionContext,
161+
// new RewriteMavenArtifactDownloader(new LocalMavenArtifactCache(Path.of(System.getProperty("user.dir")).resolve(".m2/repository")), new MavenSettings(), t -> {throw new RuntimeException(t);}))
162+
// );
163+
//
164+
//
165+
// projectResourceSetFactory = new ProjectResourceSetFactory(new RewriteMigrationResultMerger(sourceFileWrapper), sourceFileWrapper, executionContext);
166+
// ProjectResourceWrapperRegistry registry = new ProjectResourceWrapperRegistry(projectResourceWrappers);
167+
// BasePackageCalculator calculator = new BasePackageCalculator(sbmApplicationProperties);
168+
//
169+
// ProjectResourceSetFactory resourceSetFactory = new ProjectResourceSetFactory(merger, sourceFileWrapper, executionContext);
170+
//
171+
// projectContextFactory = new ProjectContextFactory(registry, holder, refactoringFactory, calculator, parserBuilder, executionContext, merger, resourceSetFactory);
215172
}
216173

217174
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2021 - 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.sbm;
17+
18+
import org.springframework.context.annotation.Bean;
19+
import org.springframework.context.annotation.Configuration;
20+
21+
/**
22+
* @author Fabian Krüger
23+
*/
24+
@Configuration
25+
public class FreemarkerConfiguration {
26+
@Bean
27+
public freemarker.template.Configuration configuration() {
28+
return new freemarker.template.Configuration();
29+
}
30+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2021 - 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.sbm;
17+
18+
import org.springframework.sbm.engine.recipe.UserInteractions;
19+
import org.springframework.stereotype.Component;
20+
21+
@Component
22+
public class UserInteractionsDummy implements UserInteractions {
23+
@Override
24+
public boolean askUserYesOrNo(String question) {
25+
return true;
26+
}
27+
28+
@Override
29+
public String askForInput(String question) {
30+
return "answer";
31+
}
32+
}

components/jaxrs-recipes/src/main/java/org/springframework/sbm/java/migration/conditions/HasTypeAnnotation.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ public String getDescription() {
3535

3636
@Override
3737
public boolean evaluate(ProjectContext context) {
38-
return true;
39-
// context.getProjectJavaSources().asStream()
40-
// .flatMap(js -> js.getTypes().stream())
41-
// .anyMatch(t -> t.hasAnnotation(annotation));
38+
return context.getProjectJavaSources().asStream()
39+
.flatMap(js -> js.getTypes().stream())
40+
.anyMatch(t -> t.hasAnnotation(annotation));
4241
}
4342

4443
}

components/jaxrs-recipes/src/main/java/org/springframework/sbm/jee/jaxrs/MigrateJaxRsRecipe.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ public Recipe jaxRs(RewriteRecipeLoader rewriteRecipeLoader) {
5555
.condition(HasImportStartingWith.builder().value("javax.ws.rs").description("Any class has import starting with javax.ws.rs").build())
5656
.actions(List.of(
5757

58-
AddDependencies.builder()
59-
.dependencies(
60-
List.of(
61-
Dependency.builder().groupId("org.springframework.boot").artifactId("spring-boot-starter-web").version("2.3.4.RELEASE").build()
62-
)
63-
)
64-
.description("Add spring-boot-starter-web dependency to build file.")
65-
.condition(NoExactDependencyExist.builder().dependency(Dependency.builder().groupId("org.springframework.boot").artifactId("spring-boot-starter-web").build()).build())
66-
.build(),
58+
// AddDependencies.builder()
59+
// .dependencies(
60+
// List.of(
61+
// Dependency.builder().groupId("org.springframework.boot").artifactId("spring-boot-starter-web").version("2.3.4.RELEASE").build()
62+
// )
63+
// )
64+
// .description("Add spring-boot-starter-web dependency to build file.")
65+
// .condition(NoExactDependencyExist.builder().dependency(Dependency.builder().groupId("org.springframework.boot").artifactId("spring-boot-starter-web").build()).build())
66+
// .build(),
6767

6868
ConvertJaxRsAnnotations.builder()
6969
.condition(HasTypeAnnotation.builder().annotation("javax.ws.rs.Path").build())

0 commit comments

Comments
 (0)