@@ -174,19 +174,15 @@ public void merge() {
174
174
private void mergeSpringBootApplicationClassAnnotations () throws IOException {
175
175
logger .debug ("Looking for @SpringBootApplication in directory " + this .toMergeProjectPath .toFile ());
176
176
Optional <File > springBootApplicationFile = RootPackageFinder
177
- .findSpringBootApplicationFile (this .toMergeProjectPath .toFile ());
177
+ .findSpringBootApplicationFile (this .toMergeProjectPath .toFile ());
178
178
179
179
if (springBootApplicationFile .isPresent ()) {
180
+ Consumer <Throwable > onError = e -> logger .error ("error in javaParser execution" , e );
181
+ List <SourceFile > mergeCompilationUnits = parseJavaFile (springBootApplicationFile .get ().toPath (), onError );
182
+
180
183
CollectAnnotationAndImportInformationRecipe annotationImportRecipe = new CollectAnnotationAndImportInformationRecipe ();
181
- Consumer <Throwable > onError = e -> {
182
- logger .error ("error in javaParser execution" , e );
183
- };
184
184
InMemoryExecutionContext executionContext = new InMemoryExecutionContext (onError );
185
- List <Path > paths = new ArrayList <>();
186
- paths .add (springBootApplicationFile .get ().toPath ());
187
- JavaParser javaParser = new Java17Parser .Builder ().build ();
188
- List <SourceFile > compilationUnits = javaParser .parse (paths , null , executionContext ).toList ();
189
- annotationImportRecipe .run (new InMemoryLargeSourceSet (compilationUnits ), executionContext );
185
+ runRecipeOnFile (annotationImportRecipe , mergeCompilationUnits , executionContext );
190
186
191
187
List <Annotation > declaredAnnotations = annotationImportRecipe .getDeclaredAnnotations ();
192
188
List <String > declaredImports = annotationImportRecipe .getDeclaredImports ();
@@ -197,7 +193,6 @@ private void mergeSpringBootApplicationClassAnnotations() throws IOException {
197
193
continue ;
198
194
}
199
195
for (String declaredImport : declaredImports ) {
200
- // get the import statement that matches the annotation
201
196
if (declaredImport .contains (declaredAnnotation .getSimpleName ())) {
202
197
annotationImportMap .put (declaredAnnotation .toString (), declaredImport );
203
198
}
@@ -206,22 +201,20 @@ private void mergeSpringBootApplicationClassAnnotations() throws IOException {
206
201
207
202
logger .debug ("Looking for @SpringBootApplication in directory " + this .currentProjectPath .toFile ());
208
203
Optional <File > currentSpringBootApplicationFile = RootPackageFinder
209
- .findSpringBootApplicationFile (this .currentProjectPath .toFile ());
204
+ .findSpringBootApplicationFile (this .currentProjectPath .toFile ());
210
205
if (currentSpringBootApplicationFile .isPresent ()) {
206
+ List <SourceFile > currentCompilationUnits = parseJavaFile (
207
+ currentSpringBootApplicationFile .get ().toPath (), onError );
211
208
executionContext = new InMemoryExecutionContext (onError );
212
- paths = new ArrayList <>();
213
- paths .add (currentSpringBootApplicationFile .get ().toPath ());
214
- javaParser = new Java17Parser .Builder ().build ();
215
- compilationUnits = javaParser .parse (paths , null , executionContext ).toList ();
209
+
216
210
for (Entry <String , String > annotationImportEntry : annotationImportMap .entrySet ()) {
217
211
String annotation = annotationImportEntry .getKey ();
218
212
String importStatement = annotationImportEntry .getValue ();
213
+
219
214
AddImport addImport = new AddImport (importStatement , null , false );
220
215
AddImportRecipe addImportRecipe = new AddImportRecipe (addImport );
221
- List <Result > results = addImportRecipe
222
- .run (new InMemoryLargeSourceSet (compilationUnits ), executionContext )
223
- .getChangeset ()
224
- .getAllResults ();
216
+ List <Result > results = runRecipeOnFile (addImportRecipe , currentCompilationUnits , executionContext );
217
+
225
218
updateSpringApplicationClass (currentSpringBootApplicationFile .get ().toPath (), results );
226
219
227
220
AttributedStringBuilder sb = new AttributedStringBuilder ();
@@ -235,6 +228,21 @@ private void mergeSpringBootApplicationClassAnnotations() throws IOException {
235
228
}
236
229
}
237
230
231
+ private List <SourceFile > parseJavaFile (Path filePath , Consumer <Throwable > onError ) {
232
+ InMemoryExecutionContext executionContext = new InMemoryExecutionContext (onError );
233
+ List <Path > paths = new ArrayList <>();
234
+ paths .add (filePath );
235
+ JavaParser javaParser = new Java17Parser .Builder ().build ();
236
+ return javaParser .parse (paths , null , executionContext ).toList ();
237
+ }
238
+
239
+ private List <Result > runRecipeOnFile (Recipe recipe , List <SourceFile > compilationUnits ,
240
+ InMemoryExecutionContext executionContext ) {
241
+ return recipe .run (new InMemoryLargeSourceSet (compilationUnits ), executionContext )
242
+ .getChangeset ()
243
+ .getAllResults ();
244
+ }
245
+
238
246
private void injectAnnotation (Path pathToFile , String annotation ) {
239
247
try {
240
248
List <String > lines = Files .readAllLines (pathToFile );
0 commit comments