From 6d4255ff94281583bb69b0a4096b2b78313d39da Mon Sep 17 00:00:00 2001 From: Michael Keppler Date: Thu, 16 Nov 2023 21:03:37 +0100 Subject: [PATCH] Avoid calling parsers when there are no source files With empty lists of main or test, Java or Kotlin files avoid invoking the parsers completely. That's more performant and avoids any bad side effects during initialization of the parsers. Fixes #658. --- .../maven/MavenMojoProjectParser.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java b/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java index c0ba74af..bbfb5355 100644 --- a/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java +++ b/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java @@ -323,13 +323,13 @@ private Stream processMainSources( javaParserBuilder.classpath(dependencies).typeCache(typeCache); kotlinParserBuilder.classpath(dependencies).typeCache(new JavaTypeCache()); - Stream cus = Stream.of(javaParserBuilder) - .map(JavaParser.Builder::build) - .flatMap(parser -> parser.parse(mainJavaSources, baseDir, ctx)); + Stream cus = mainJavaSources.isEmpty() ? Stream.empty() + : Stream.of(javaParserBuilder).map(JavaParser.Builder::build) + .flatMap(parser -> parser.parse(mainJavaSources, baseDir, ctx)); - Stream kcus = Stream.of(kotlinParserBuilder) - .map(KotlinParser.Builder::build) - .flatMap(parser -> parser.parse(mainKotlinSources, baseDir, ctx)); + Stream kcus = mainKotlinSources.isEmpty() ? Stream.empty() + : Stream.of(kotlinParserBuilder).map(KotlinParser.Builder::build) + .flatMap(parser -> parser.parse(mainKotlinSources, baseDir, ctx)); List mainProjectProvenance = new ArrayList<>(projectProvenance); mainProjectProvenance.add(sourceSet("main", dependencies, typeCache)); @@ -382,13 +382,13 @@ private Stream processTestSources( alreadyParsed.addAll(testKotlinSources); - Stream cus = Stream.of(javaParserBuilder) - .map(JavaParser.Builder::build) - .flatMap(parser -> parser.parse(testJavaSources, baseDir, ctx)); + Stream cus = testJavaSources.isEmpty() ? Stream.empty() + : Stream.of(javaParserBuilder).map(JavaParser.Builder::build) + .flatMap(parser -> parser.parse(testJavaSources, baseDir, ctx)); - Stream kcus = Stream.of(kotlinParserBuilder) - .map(KotlinParser.Builder::build) - .flatMap(parser -> parser.parse(testKotlinSources, baseDir, ctx)); + Stream kcus = testKotlinSources.isEmpty() ? Stream.empty() + : Stream.of(kotlinParserBuilder).map(KotlinParser.Builder::build) + .flatMap(parser -> parser.parse(testKotlinSources, baseDir, ctx)); List markers = new ArrayList<>(projectProvenance); markers.add(sourceSet("test", testDependencies, typeCache));