Skip to content

Commit

Permalink
Lazier instantiation of JavaParser
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Jun 19, 2023
1 parent 0494e84 commit 57c1067
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,9 @@ private Stream<SourceFile> processMainSources(
JavaTypeCache typeCache = new JavaTypeCache();
javaParserBuilder.typeCache(typeCache);

JavaParser javaParser = javaParserBuilder.build();
Stream<? extends SourceFile> cus = javaParser.parse(mainJavaSources, baseDir, ctx);
Stream<? extends SourceFile> cus = Stream.of(javaParserBuilder)
.map(JavaParser.Builder::build)
.flatMap(parser -> parser.parse(mainJavaSources, baseDir, ctx));

List<Marker> mainProjectProvenance = new ArrayList<>(projectProvenance);
mainProjectProvenance.add(sourceSet("main", dependencies, typeCache));
Expand All @@ -297,7 +298,7 @@ private Stream<SourceFile> processMainSources(
return cu.withMarkers(markers);
});
Stream<SourceFile> parsedJava = cus.map(addProvenance(baseDir, mainProjectProvenance, generatedSourcePaths));
logDebug(mavenProject, "Parsed " + mainJavaSources.size() + " java source files in main scope.");
logDebug(mavenProject, "Scanned " + mainJavaSources.size() + " java source files in main scope.");

//Filter out any generated source files from the returned list, as we do not want to apply the recipe to the
//generated files.
Expand All @@ -308,7 +309,7 @@ private Stream<SourceFile> processMainSources(
Stream<SourceFile> parsedResourceFiles = resourceParser.parse(mavenProject.getBasedir().toPath().resolve("src/main/resources"), alreadyParsed)
.map(addProvenance(baseDir, mainProjectProvenance, null));

logDebug(mavenProject, "Parsed " + (alreadyParsed.size() - sourcesParsedBefore) + " resource files in main scope.");
logDebug(mavenProject, "Scanned " + (alreadyParsed.size() - sourcesParsedBefore) + " resource files in main scope.");
// Any resources parsed from "main/resources" should also have the main source set added to them.
sourceFiles = Stream.concat(sourceFiles, parsedResourceFiles);
return sourceFiles;
Expand All @@ -334,22 +335,23 @@ private Stream<SourceFile> processTestSources(
List<Path> testJavaSources = listJavaSources(mavenProject.getBuild().getTestSourceDirectory());
alreadyParsed.addAll(testJavaSources);

JavaParser javaParser = javaParserBuilder.build();
Stream<SourceFile> cus = javaParser.parse(testJavaSources, baseDir, ctx);
Stream<? extends SourceFile> cus = Stream.of(javaParserBuilder)
.map(JavaParser.Builder::build)
.flatMap(parser -> parser.parse(testJavaSources, baseDir, ctx));

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

Stream<SourceFile> parsedJava = cus.map(addProvenance(baseDir, markers, null));

logDebug(mavenProject, "Parsed " + testJavaSources.size() + " java source files in test scope.");
logDebug(mavenProject, "Scanned " + testJavaSources.size() + " java source files in test scope.");
Stream<SourceFile> sourceFiles = parsedJava;

// Any resources parsed from "test/resources" should also have the test source set added to them.
int sourcesParsedBefore = alreadyParsed.size();
Stream<SourceFile> parsedResourceFiles = resourceParser.parse(mavenProject.getBasedir().toPath().resolve("src/test/resources"), alreadyParsed)
.map(addProvenance(baseDir, markers, null));
logDebug(mavenProject, "Parsed " + (alreadyParsed.size() - sourcesParsedBefore) + " resource files in test scope.");
logDebug(mavenProject, "Scanned " + (alreadyParsed.size() - sourcesParsedBefore) + " resource files in test scope.");
sourceFiles = Stream.concat(sourceFiles, parsedResourceFiles);
return sourceFiles;
}
Expand Down

0 comments on commit 57c1067

Please sign in to comment.