From 9a9428fbd211b79cebdec18a68b830e16434ade3 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 28 Nov 2023 11:29:31 +0100 Subject: [PATCH] Avoid building Java & Kotlin parsers when possible (#665) Also simplify logic related to log statements, markers & return value --- .../maven/MavenMojoProjectParser.java | 66 ++++++++----------- 1 file changed, 29 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java b/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java index e8f3dc22..4c0b9ae9 100644 --- a/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java +++ b/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java @@ -323,36 +323,34 @@ private Stream processMainSources( javaParserBuilder.classpath(dependencies).typeCache(typeCache); kotlinParserBuilder.classpath(dependencies).typeCache(new JavaTypeCache()); - Stream cus = mainJavaSources.isEmpty() ? Stream.empty() - : Stream.of(javaParserBuilder).map(JavaParser.Builder::build) - .flatMap(parser -> parser.parse(mainJavaSources, baseDir, ctx)); + Stream parsedJava = Stream.empty(); + if (!mainJavaSources.isEmpty()) { + parsedJava = javaParserBuilder.build().parse(mainJavaSources, baseDir, ctx); + logDebug(mavenProject, "Scanned " + mainJavaSources.size() + " java source files in main scope."); + } - Stream kcus = mainKotlinSources.isEmpty() ? Stream.empty() - : Stream.of(kotlinParserBuilder).map(KotlinParser.Builder::build) - .flatMap(parser -> parser.parse(mainKotlinSources, baseDir, ctx)); + Stream parsedKotlin = Stream.empty(); + if (!mainKotlinSources.isEmpty()) { + parsedKotlin = kotlinParserBuilder.build().parse(mainKotlinSources, baseDir, ctx); + logDebug(mavenProject, "Scanned " + mainKotlinSources.size() + " kotlin source files in main scope."); + } List mainProjectProvenance = new ArrayList<>(projectProvenance); mainProjectProvenance.add(sourceSet("main", dependencies, typeCache)); - Stream parsedJava = cus.map(addProvenance(baseDir, mainProjectProvenance, generatedSourcePaths)); - logDebug(mavenProject, "Scanned " + mainJavaSources.size() + " java source files in main scope."); - - Stream parsedKotlin = kcus.map(addProvenance(baseDir, mainProjectProvenance, generatedSourcePaths)); - logDebug(mavenProject, "Scanned " + mainKotlinSources.size() + " kotlin 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. Path buildDirectory = baseDir.relativize(Paths.get(mavenProject.getBuild().getDirectory())); - Stream sourceFiles = Stream.concat(parsedJava, parsedKotlin).filter(s -> !s.getSourcePath().startsWith(buildDirectory)); + Stream sourceFiles = Stream.concat(parsedJava, parsedKotlin) + .filter(s -> !s.getSourcePath().startsWith(buildDirectory)) + .map(addProvenance(baseDir, mainProjectProvenance, generatedSourcePaths)); + // Any resources parsed from "main/resources" should also have the main source set added to them. int sourcesParsedBefore = alreadyParsed.size(); Stream parsedResourceFiles = resourceParser.parse(mavenProject.getBasedir().toPath().resolve("src/main/resources"), alreadyParsed) .map(addProvenance(baseDir, mainProjectProvenance, null)); - 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; + return Stream.concat(sourceFiles, parsedResourceFiles); } private Stream processTestSources( @@ -379,35 +377,29 @@ private Stream processTestSources( // scan Kotlin files String kotlinSourceDir = getKotlinDirectory(mavenProject.getBuild().getSourceDirectory()); List testKotlinSources = (kotlinSourceDir != null) ? listKotlinSources(mavenProject.getBasedir().toPath().resolve(kotlinSourceDir)) : Collections.emptyList(); - alreadyParsed.addAll(testKotlinSources); - Stream cus = testJavaSources.isEmpty() ? Stream.empty() - : Stream.of(javaParserBuilder).map(JavaParser.Builder::build) - .flatMap(parser -> parser.parse(testJavaSources, baseDir, ctx)); + Stream parsedJava = Stream.empty(); + if (!testJavaSources.isEmpty()) { + parsedJava = javaParserBuilder.build().parse(testJavaSources, baseDir, ctx); + logDebug(mavenProject, "Scanned " + testJavaSources.size() + " java source files in test scope."); + } - Stream kcus = testKotlinSources.isEmpty() ? Stream.empty() - : Stream.of(kotlinParserBuilder).map(KotlinParser.Builder::build) - .flatMap(parser -> parser.parse(testKotlinSources, baseDir, ctx)); + Stream parsedKotlin = Stream.empty(); + if (!testKotlinSources.isEmpty()) { + parsedKotlin = kotlinParserBuilder.build().parse(testKotlinSources, baseDir, ctx); + logDebug(mavenProject, "Scanned " + testKotlinSources.size() + " kotlin source files in main scope."); + } List markers = new ArrayList<>(projectProvenance); markers.add(sourceSet("test", testDependencies, typeCache)); - Stream parsedJava = cus.map(addProvenance(baseDir, markers, null)); - logDebug(mavenProject, "Scanned " + testJavaSources.size() + " java source files in test scope."); - - Stream parsedKotlin = kcus.map(addProvenance(baseDir, markers, null)); - logDebug(mavenProject, "Scanned " + testKotlinSources.size() + " kotlin source files in main scope."); - - Stream sourceFiles = Stream.concat(parsedJava, parsedKotlin); - // Any resources parsed from "test/resources" should also have the test source set added to them. int sourcesParsedBefore = alreadyParsed.size(); - Stream parsedResourceFiles = resourceParser.parse(mavenProject.getBasedir().toPath().resolve("src/test/resources"), alreadyParsed) - .map(addProvenance(baseDir, markers, null)); + Stream parsedResourceFiles = resourceParser.parse(mavenProject.getBasedir().toPath().resolve("src/test/resources"), alreadyParsed); logDebug(mavenProject, "Scanned " + (alreadyParsed.size() - sourcesParsedBefore) + " resource files in test scope."); - sourceFiles = Stream.concat(sourceFiles, parsedResourceFiles); - return sourceFiles; + return Stream.concat(Stream.concat(parsedJava, parsedKotlin), parsedResourceFiles) + .map(addProvenance(baseDir, markers, null)); } @Nullable @@ -504,7 +496,7 @@ public Map parseMaven(List mavenProjec if (document instanceof Xml.Document) { projectMap.put(mavenProject, (Xml.Document) document); } else if (document instanceof ParseError) { - logError(mavenProject, "Parse error in Maven Project File '" + path + "': "+ document); + logError(mavenProject, "Parse error in Maven Project File '" + path + "': " + document); } } }