16
16
package example .recipe ;
17
17
18
18
19
+ import freemarker .template .Configuration ;
19
20
import org .openrewrite .*;
20
21
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 ;
21
26
import org .springframework .rewrite .parser .JavaParserBuilder ;
27
+ import org .springframework .rewrite .parser .maven .MavenSettingsInitializer ;
28
+ import org .springframework .rewrite .parser .maven .RewriteMavenArtifactDownloader ;
22
29
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 ;
23
34
import org .springframework .sbm .engine .context .ProjectContext ;
24
35
import org .springframework .sbm .engine .context .ProjectContextFactory ;
36
+ import org .springframework .sbm .engine .recipe .RewriteRecipeLoader ;
25
37
import org .springframework .sbm .java .JavaSourceProjectResourceWrapper ;
26
38
import org .springframework .sbm .java .refactoring .JavaRefactoringFactory ;
27
39
import org .springframework .sbm .java .refactoring .JavaRefactoringFactoryImpl ;
28
40
import org .springframework .sbm .java .util .BasePackageCalculator ;
41
+ import org .springframework .sbm .jee .jaxrs .MigrateJaxRsRecipe ;
29
42
import org .springframework .sbm .jee .jaxrs .actions .ConvertJaxRsAnnotations ;
30
43
import org .springframework .sbm .project .resource .ProjectResourceSetHolder ;
31
44
import org .springframework .sbm .project .resource .ProjectResourceWrapper ;
@@ -78,103 +91,35 @@ public TreeVisitor<?, ExecutionContext> getScanner(List<SourceFile> acc) {
78
91
}
79
92
80
93
@ 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);
83
96
84
97
// create the required classes
85
98
initBeans (executionContext );
86
99
87
100
// 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 ();
89
102
90
103
// 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");
92
105
93
106
// Create the SBM resource set abstraction
94
107
ProjectResourceSet projectResourceSet = projectResourceSetFactory .create (baseDir , sourceFiles );
95
108
// Create the SBM ProjectContext
96
109
ProjectContext pc = projectContextFactory .createProjectContext (baseDir , projectResourceSet );
97
110
98
111
// 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 );
100
116
101
117
// Merge back result
102
118
List <? extends SourceFile > modifiedNodes = merge (sourceFiles , pc .getProjectResources ());
103
119
104
120
return modifiedNodes ;
105
121
}
106
122
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
-
178
123
private List <? extends SourceFile > merge (List <SourceFile > nodes , ProjectResourceSet projectResources ) {
179
124
// merge the changed results into the given list and return the result
180
125
ArrayList <SourceFile > merged = new ArrayList <>();
@@ -195,23 +140,35 @@ private int findPosition(List<SourceFile> merged, RewriteSourceFileHolder<? exte
195
140
196
141
197
142
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);
215
172
}
216
173
217
174
}
0 commit comments