Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.

Commit 0697f5f

Browse files
author
ritvik
committed
Extracted methods to eliminate duplicated logic and improve clarity
1 parent 29e9f69 commit 0697f5f

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

src/main/java/org/springframework/cli/merger/ProjectMerger.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -174,19 +174,15 @@ public void merge() {
174174
private void mergeSpringBootApplicationClassAnnotations() throws IOException {
175175
logger.debug("Looking for @SpringBootApplication in directory " + this.toMergeProjectPath.toFile());
176176
Optional<File> springBootApplicationFile = RootPackageFinder
177-
.findSpringBootApplicationFile(this.toMergeProjectPath.toFile());
177+
.findSpringBootApplicationFile(this.toMergeProjectPath.toFile());
178178

179179
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+
180183
CollectAnnotationAndImportInformationRecipe annotationImportRecipe = new CollectAnnotationAndImportInformationRecipe();
181-
Consumer<Throwable> onError = e -> {
182-
logger.error("error in javaParser execution", e);
183-
};
184184
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);
190186

191187
List<Annotation> declaredAnnotations = annotationImportRecipe.getDeclaredAnnotations();
192188
List<String> declaredImports = annotationImportRecipe.getDeclaredImports();
@@ -197,7 +193,6 @@ private void mergeSpringBootApplicationClassAnnotations() throws IOException {
197193
continue;
198194
}
199195
for (String declaredImport : declaredImports) {
200-
// get the import statement that matches the annotation
201196
if (declaredImport.contains(declaredAnnotation.getSimpleName())) {
202197
annotationImportMap.put(declaredAnnotation.toString(), declaredImport);
203198
}
@@ -206,22 +201,20 @@ private void mergeSpringBootApplicationClassAnnotations() throws IOException {
206201

207202
logger.debug("Looking for @SpringBootApplication in directory " + this.currentProjectPath.toFile());
208203
Optional<File> currentSpringBootApplicationFile = RootPackageFinder
209-
.findSpringBootApplicationFile(this.currentProjectPath.toFile());
204+
.findSpringBootApplicationFile(this.currentProjectPath.toFile());
210205
if (currentSpringBootApplicationFile.isPresent()) {
206+
List<SourceFile> currentCompilationUnits = parseJavaFile(
207+
currentSpringBootApplicationFile.get().toPath(), onError);
211208
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+
216210
for (Entry<String, String> annotationImportEntry : annotationImportMap.entrySet()) {
217211
String annotation = annotationImportEntry.getKey();
218212
String importStatement = annotationImportEntry.getValue();
213+
219214
AddImport addImport = new AddImport(importStatement, null, false);
220215
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+
225218
updateSpringApplicationClass(currentSpringBootApplicationFile.get().toPath(), results);
226219

227220
AttributedStringBuilder sb = new AttributedStringBuilder();
@@ -235,6 +228,21 @@ private void mergeSpringBootApplicationClassAnnotations() throws IOException {
235228
}
236229
}
237230

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+
238246
private void injectAnnotation(Path pathToFile, String annotation) {
239247
try {
240248
List<String> lines = Files.readAllLines(pathToFile);

0 commit comments

Comments
 (0)