gnodet-bot commented on code in PR #1144: URL: https://github.com/apache/maven-compiler-plugin/pull/1144#discussion_r4179621030
########## src/test/java/org/apache/maven/plugin/compiler/incremental/CompilerTestHelper.java: ########## @@ -0,0 +1,98 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.plugin.compiler.incremental; + +import javax.tools.JavaCompiler; +import javax.tools.StandardLocation; +import javax.tools.ToolProvider; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; + +/** + * Shared helper for incremental compilation tests. + */ +class CompilerTestHelper { + + static void writeSource(Path sourceDir, String packageName, String className, String source) throws IOException { + Path packageDir = sourceDir.resolve(packageName.replace('.', '/')); + Files.createDirectories(packageDir); + Files.writeString(packageDir.resolve(className + ".java"), source); + } + + /** + * Compiles all {@code .java} files under {@code sourceDir} into {@code outputDir} + * and returns the output directory. No ABI analysis — raw javac only. + */ + static Map<String, SourceFileAnalysis> compileAndAnalyze(Path sourceDir, Path outputDir) throws IOException { + Files.createDirectories(outputDir); + List<Path> sourceFiles; + try (Stream<Path> walk = Files.walk(sourceDir)) { + sourceFiles = + walk.filter(p -> p.toString().endsWith(".java")).sorted().toList(); + } + + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + try (var fm = compiler.getStandardFileManager(null, null, null)) { + fm.setLocation(StandardLocation.CLASS_OUTPUT, List.of(outputDir.toFile())); + var units = fm.getJavaFileObjectsFromPaths(sourceFiles); + var task = compiler.getTask(null, fm, null, null, null, units); + + if (!task.call()) { + throw new RuntimeException("Compilation failed"); + } + + // Build a minimal SourceFileAnalysis map from bytecode (no dep graph needed here) + Map<String, SourceFileAnalysis> results = new java.util.LinkedHashMap<>(); + for (Path sf : sourceFiles) { + try (Stream<Path> walk = Files.walk(outputDir)) { + walk.filter(p -> p.toString().endsWith(".class")).forEach(cf -> { + try { + var analysis = BytecodeAnalyzer.analyze(cf); + var sfa = new SourceFileAnalysis( + analysis.className(), + sf.toString(), + analysis.signatureTypes(), + analysis.implementationTypes(), + analysis.abiFingerprint(), + analysis.abiCanonical(), + analysis.annotationTypes(), Review Comment: 💡 **Low — Wasteful iteration in test helper.** The outer `for (Path sf : sourceFiles)` loop walks the entire output directory once per source file, but the inner walk doesn't filter by `sf` — every `.class` file is analyzed and mapped to `sf.toString()` regardless of which source file produced it. The `results` map deduplicates by `className` (last writer wins), so this doesn't cause test failures, but it means: 1. Class files are analyzed `O(sourceFiles × classFiles)` times instead of `O(classFiles)` once 2. Every type gets attributed to the *last* source file in the iteration, not the one that actually defines it Since this is test-only code and the `sourceFile` field isn't validated in these tests, this is cosmetic — but it would break if a test ever asserted `sourceFile` accuracy from this helper. ```suggestion // Build a minimal SourceFileAnalysis map from bytecode (no dep graph needed here) Map<String, SourceFileAnalysis> results = new java.util.LinkedHashMap<>(); try (Stream<Path> walk = Files.walk(outputDir)) { walk.filter(p -> p.toString().endsWith(".class")).forEach(cf -> { try { var analysis = BytecodeAnalyzer.analyze(cf); var sfa = new SourceFileAnalysis( analysis.className(), cf.toString(), analysis.signatureTypes(), analysis.implementationTypes(), analysis.abiFingerprint(), analysis.abiCanonical(), analysis.annotationTypes(), analysis.moduleName()); results.put(analysis.className(), sfa); } catch (IOException e) { // best effort } }); } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
