Skip to content

Commit 0b8fc33

Browse files
committed
Avoid building Java & Kotlin parsers when possible
Also simplify logic related to log statements, markers & return value
1 parent 27c17a8 commit 0b8fc33

File tree

1 file changed

+29
-37
lines changed

1 file changed

+29
-37
lines changed

src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java

+29-37
Original file line numberDiff line numberDiff line change
@@ -323,36 +323,34 @@ private Stream<SourceFile> processMainSources(
323323
javaParserBuilder.classpath(dependencies).typeCache(typeCache);
324324
kotlinParserBuilder.classpath(dependencies).typeCache(new JavaTypeCache());
325325

326-
Stream<? extends SourceFile> cus = mainJavaSources.isEmpty() ? Stream.empty()
327-
: Stream.of(javaParserBuilder).map(JavaParser.Builder::build)
328-
.flatMap(parser -> parser.parse(mainJavaSources, baseDir, ctx));
326+
Stream<SourceFile> parsedJava = Stream.empty();
327+
if (!mainJavaSources.isEmpty()) {
328+
parsedJava = javaParserBuilder.build().parse(mainJavaSources, baseDir, ctx);
329+
logDebug(mavenProject, "Scanned " + mainJavaSources.size() + " java source files in main scope.");
330+
}
329331

330-
Stream<? extends SourceFile> kcus = mainKotlinSources.isEmpty() ? Stream.empty()
331-
: Stream.of(kotlinParserBuilder).map(KotlinParser.Builder::build)
332-
.flatMap(parser -> parser.parse(mainKotlinSources, baseDir, ctx));
332+
Stream<SourceFile> parsedKotlin = Stream.empty();
333+
if (!mainKotlinSources.isEmpty()) {
334+
parsedKotlin = kotlinParserBuilder.build().parse(mainKotlinSources, baseDir, ctx);
335+
logDebug(mavenProject, "Scanned " + mainKotlinSources.size() + " kotlin source files in main scope.");
336+
}
333337

334338
List<Marker> mainProjectProvenance = new ArrayList<>(projectProvenance);
335339
mainProjectProvenance.add(sourceSet("main", dependencies, typeCache));
336340

337-
Stream<SourceFile> parsedJava = cus.map(addProvenance(baseDir, mainProjectProvenance, generatedSourcePaths));
338-
logDebug(mavenProject, "Scanned " + mainJavaSources.size() + " java source files in main scope.");
339-
340-
Stream<SourceFile> parsedKotlin = kcus.map(addProvenance(baseDir, mainProjectProvenance, generatedSourcePaths));
341-
logDebug(mavenProject, "Scanned " + mainKotlinSources.size() + " kotlin source files in main scope.");
342-
343341
//Filter out any generated source files from the returned list, as we do not want to apply the recipe to the
344342
//generated files.
345343
Path buildDirectory = baseDir.relativize(Paths.get(mavenProject.getBuild().getDirectory()));
346-
Stream<SourceFile> sourceFiles = Stream.concat(parsedJava, parsedKotlin).filter(s -> !s.getSourcePath().startsWith(buildDirectory));
344+
Stream<SourceFile> sourceFiles = Stream.concat(parsedJava, parsedKotlin)
345+
.filter(s -> !s.getSourcePath().startsWith(buildDirectory))
346+
.map(addProvenance(baseDir, mainProjectProvenance, generatedSourcePaths));
347347

348+
// Any resources parsed from "main/resources" should also have the main source set added to them.
348349
int sourcesParsedBefore = alreadyParsed.size();
349350
Stream<SourceFile> parsedResourceFiles = resourceParser.parse(mavenProject.getBasedir().toPath().resolve("src/main/resources"), alreadyParsed)
350351
.map(addProvenance(baseDir, mainProjectProvenance, null));
351-
352352
logDebug(mavenProject, "Scanned " + (alreadyParsed.size() - sourcesParsedBefore) + " resource files in main scope.");
353-
// Any resources parsed from "main/resources" should also have the main source set added to them.
354-
sourceFiles = Stream.concat(sourceFiles, parsedResourceFiles);
355-
return sourceFiles;
353+
return Stream.concat(sourceFiles, parsedResourceFiles);
356354
}
357355

358356
private Stream<SourceFile> processTestSources(
@@ -379,35 +377,29 @@ private Stream<SourceFile> processTestSources(
379377
// scan Kotlin files
380378
String kotlinSourceDir = getKotlinDirectory(mavenProject.getBuild().getSourceDirectory());
381379
List<Path> testKotlinSources = (kotlinSourceDir != null) ? listKotlinSources(mavenProject.getBasedir().toPath().resolve(kotlinSourceDir)) : Collections.emptyList();
382-
383380
alreadyParsed.addAll(testKotlinSources);
384381

385-
Stream<? extends SourceFile> cus = testJavaSources.isEmpty() ? Stream.empty()
386-
: Stream.of(javaParserBuilder).map(JavaParser.Builder::build)
387-
.flatMap(parser -> parser.parse(testJavaSources, baseDir, ctx));
382+
Stream<SourceFile> parsedJava = Stream.empty();
383+
if (!testJavaSources.isEmpty()) {
384+
parsedJava = javaParserBuilder.build().parse(testJavaSources, baseDir, ctx);
385+
logDebug(mavenProject, "Scanned " + testJavaSources.size() + " java source files in test scope.");
386+
}
388387

389-
Stream<? extends SourceFile> kcus = testKotlinSources.isEmpty() ? Stream.empty()
390-
: Stream.of(kotlinParserBuilder).map(KotlinParser.Builder::build)
391-
.flatMap(parser -> parser.parse(testKotlinSources, baseDir, ctx));
388+
Stream<SourceFile> parsedKotlin = Stream.empty();
389+
if (!testKotlinSources.isEmpty()) {
390+
parsedKotlin = kotlinParserBuilder.build().parse(testKotlinSources, baseDir, ctx);
391+
logDebug(mavenProject, "Scanned " + testKotlinSources.size() + " kotlin source files in main scope.");
392+
}
392393

393394
List<Marker> markers = new ArrayList<>(projectProvenance);
394395
markers.add(sourceSet("test", testDependencies, typeCache));
395396

396-
Stream<SourceFile> parsedJava = cus.map(addProvenance(baseDir, markers, null));
397-
logDebug(mavenProject, "Scanned " + testJavaSources.size() + " java source files in test scope.");
398-
399-
Stream<SourceFile> parsedKotlin = kcus.map(addProvenance(baseDir, markers, null));
400-
logDebug(mavenProject, "Scanned " + testKotlinSources.size() + " kotlin source files in main scope.");
401-
402-
Stream<SourceFile> sourceFiles = Stream.concat(parsedJava, parsedKotlin);
403-
404397
// Any resources parsed from "test/resources" should also have the test source set added to them.
405398
int sourcesParsedBefore = alreadyParsed.size();
406-
Stream<SourceFile> parsedResourceFiles = resourceParser.parse(mavenProject.getBasedir().toPath().resolve("src/test/resources"), alreadyParsed)
407-
.map(addProvenance(baseDir, markers, null));
399+
Stream<SourceFile> parsedResourceFiles = resourceParser.parse(mavenProject.getBasedir().toPath().resolve("src/test/resources"), alreadyParsed);
408400
logDebug(mavenProject, "Scanned " + (alreadyParsed.size() - sourcesParsedBefore) + " resource files in test scope.");
409-
sourceFiles = Stream.concat(sourceFiles, parsedResourceFiles);
410-
return sourceFiles;
401+
return Stream.concat(Stream.concat(parsedJava, parsedKotlin), parsedResourceFiles)
402+
.map(addProvenance(baseDir, markers, null));
411403
}
412404

413405
@Nullable
@@ -504,7 +496,7 @@ public Map<MavenProject, Xml.Document> parseMaven(List<MavenProject> mavenProjec
504496
if (document instanceof Xml.Document) {
505497
projectMap.put(mavenProject, (Xml.Document) document);
506498
} else if (document instanceof ParseError) {
507-
logError(mavenProject, "Parse error in Maven Project File '" + path + "': "+ document);
499+
logError(mavenProject, "Parse error in Maven Project File '" + path + "': " + document);
508500
}
509501
}
510502
}

0 commit comments

Comments
 (0)