gnodet commented on code in PR #1165: URL: https://github.com/apache/maven-compiler-plugin/pull/1165#discussion_r4792585038
########## src/main/java/org/apache/maven/plugin/compiler/incremental/ProcessorClassification.java: ########## @@ -0,0 +1,169 @@ +/* + * 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 java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +/** + * Discovers annotation processor classifications from the processor classpath. + * + * <p>Reads classification files from two locations in each classpath entry + * (directory or JAR), checked in order: + * <ol> + * <li>{@code META-INF/maven/compiler/incremental.annotation.processors}</li> + * <li>{@code META-INF/gradle/incremental.annotation.processors} + * (Gradle-compatible, for ecosystem reuse)</li> + * </ol> + * + * <p>File format: one processor per line, {@code fully.qualified.Name,TYPE} + * where TYPE is {@code ISOLATING} or {@code AGGREGATING}. Empty lines and + * lines starting with {@code #} are ignored. + * + * <p>Processors not found in any classification file are classified as + * {@link ProcessorType#UNKNOWN}. + * + * @see ProcessorType + */ +public class ProcessorClassification { + + private static final String MAVEN_COMPILER_RESOURCE = "META-INF/maven/compiler/incremental.annotation.processors"; + private static final String GRADLE_RESOURCE = "META-INF/gradle/incremental.annotation.processors"; + + private final Map<String, ProcessorType> classifications = new HashMap<>(); + + /** + * Creates a classification by scanning the given classpath entries for + * processor classification files. + * + * @param processorPath classpath entries (directories or JARs) to scan + */ + public ProcessorClassification(List<Path> processorPath) { + if (processorPath != null) { + for (Path entry : processorPath) { + loadFromEntry(entry); + } + } + } + + /** + * Returns the classification for the given processor class name. + * + * @param processorClassName fully qualified processor class name + * @return the declared type, or {@link ProcessorType#UNKNOWN} if not declared + */ + public ProcessorType classify(String processorClassName) { + return classifications.getOrDefault(processorClassName, ProcessorType.UNKNOWN); + } + + /** + * Returns the worst (most conservative) classification across all given processors. + * UNKNOWN > AGGREGATING > ISOLATING. + */ + public ProcessorType worstCase(List<String> processorClassNames) { + if (processorClassNames == null || processorClassNames.isEmpty()) { + return ProcessorType.ISOLATING; + } + ProcessorType worst = ProcessorType.ISOLATING; + for (String name : processorClassNames) { + ProcessorType type = classify(name); + if (type == ProcessorType.UNKNOWN) { + return ProcessorType.UNKNOWN; + } + if (type == ProcessorType.AGGREGATING) { + worst = ProcessorType.AGGREGATING; + } + } + return worst; + } + + /** + * Returns the full classification map (for testing/debugging). + */ + public Map<String, ProcessorType> getClassifications() { + return Map.copyOf(classifications); + } + + private void loadFromEntry(Path entry) { + if (!Files.exists(entry)) { + return; + } + if (Files.isDirectory(entry)) { + loadFromDirectory(entry); + } else if (isJarFile(entry)) { + loadFromJar(entry); + } + } + + private void loadFromDirectory(Path dir) { + loadFile(dir.resolve(MAVEN_COMPILER_RESOURCE)); + loadFile(dir.resolve(GRADLE_RESOURCE)); + } + + private void loadFromJar(Path jarPath) { + try (FileSystem fs = FileSystems.newFileSystem(jarPath)) { + loadFile(fs.getPath(MAVEN_COMPILER_RESOURCE)); + loadFile(fs.getPath(GRADLE_RESOURCE)); + } catch (IOException ignored) { + // Skip unreadable JARs + } + } + + private void loadFile(Path file) { + if (!Files.exists(file)) { + return; + } + try (var reader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) { + String line; + while ((line = reader.readLine()) != null) { + line = line.strip(); + if (line.isEmpty() || line.startsWith("#")) { + continue; + } + int comma = line.indexOf(','); + if (comma > 0 && comma < line.length() - 1) { + String name = line.substring(0, comma).strip(); + String typeStr = line.substring(comma + 1).strip().toUpperCase(Locale.ROOT); + try { + ProcessorType type = ProcessorType.valueOf(typeStr); + // Don't overwrite — first classification wins (maven/compiler before gradle) + classifications.putIfAbsent(name, type); Review Comment: **Clarification needed**: The `putIfAbsent` ensures first-wins semantics within a single classpath entry (maven/compiler before gradle), but what about across multiple classpath entries? If the processor path has two JARs: - `jar1.jar` with `ProcessorX,ISOLATING` - `jar2.jar` with `ProcessorX,AGGREGATING` Which wins? The current code will use whichever JAR is processed first (order of `processorPath` list), but this should be documented explicitly in the class javadoc. Suggested addition: ```java * <p>If the same processor appears in multiple classification files across * different classpath entries, the first occurrence (in classpath order) wins. * Within a single entry, {@code META-INF/maven/compiler/...} takes precedence * over {@code META-INF/gradle/...}. ``` _Claude Code on behalf of Guillaume Nodet_ -- 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]
