gnodet commented on code in PR #1144:
URL:
https://github.com/apache/maven-compiler-plugin/pull/1144#discussion_r4179219244
##########
pom.xml:
##########
@@ -192,6 +192,34 @@ under the License.
</plugin>
</plugins>
</pluginManagement>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <executions>
+ <execution>
+ <!--
+ Compile sources that require java.lang.classfile (JDK 24+)
separately
+ with release=24, so the main sources stay at the baseline Java
version.
+ The compiled classes land alongside the main classes in the
output directory.
+ BytecodeAnalyzer loads ClassfileClassAnalyzer reflectively at
runtime when
+ the running JVM is JDK 24 or later; AsmClassAnalyzer is used
otherwise.
+ -->
+ <id>compile-java24</id>
+ <goals>
+ <goal>compile</goal>
+ </goals>
+ <phase>compile</phase>
+ <configuration>
+ <compileSourceRoots>
+
<compileSourceRoot>${project.basedir}/src/main/java24</compileSourceRoot>
+ </compileSourceRoots>
+ <release>24</release>
Review Comment:
Fixed in 950b860. Moved the `compile-java24` execution into a `java24+`
profile with `<jdk>[24,)</jdk>` activation. On JDK < 24, the execution is
skipped entirely.
##########
src/test/java/org/apache/maven/plugin/compiler/incremental/BytecodeAnalyzerTest.java:
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.nio.file.Files;
+import java.nio.file.Path;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class BytecodeAnalyzerTest {
+
+ @TempDir
+ Path sourceDir;
+
+ @TempDir
+ Path outputDir;
+
+ @Test
+ void descriptorToReadableVoid() {
+ assertEquals("void", BytecodeAnalyzer.descriptorToReadable("V"));
+ }
+
+ @Test
+ void descriptorToReadableBoolean() {
+ assertEquals("boolean", BytecodeAnalyzer.descriptorToReadable("Z"));
+ }
+
+ @Test
+ void descriptorToReadableByte() {
+ assertEquals("byte", BytecodeAnalyzer.descriptorToReadable("B"));
+ }
+
+ @Test
+ void descriptorToReadableChar() {
+ assertEquals("char", BytecodeAnalyzer.descriptorToReadable("C"));
+ }
+
+ @Test
+ void descriptorToReadableShort() {
+ assertEquals("short", BytecodeAnalyzer.descriptorToReadable("S"));
+ }
+
+ @Test
+ void descriptorToReadableInt() {
+ assertEquals("int", BytecodeAnalyzer.descriptorToReadable("I"));
+ }
+
+ @Test
+ void descriptorToReadableLong() {
+ assertEquals("long", BytecodeAnalyzer.descriptorToReadable("J"));
+ }
+
+ @Test
+ void descriptorToReadableFloat() {
+ assertEquals("float", BytecodeAnalyzer.descriptorToReadable("F"));
+ }
+
+ @Test
+ void descriptorToReadableDouble() {
+ assertEquals("double", BytecodeAnalyzer.descriptorToReadable("D"));
+ }
+
+ @Test
+ void descriptorToReadableObject() {
+ assertEquals("java.lang.String",
BytecodeAnalyzer.descriptorToReadable("Ljava/lang/String;"));
+ }
+
+ @Test
+ void descriptorToReadableArray() {
+ assertEquals("int[]", BytecodeAnalyzer.descriptorToReadable("[I"));
+ assertEquals("java.lang.String[]",
BytecodeAnalyzer.descriptorToReadable("[Ljava/lang/String;"));
+ }
+
+ @Test
+ void descriptorToReadableEmpty() {
+ assertEquals("", BytecodeAnalyzer.descriptorToReadable(""));
+ }
+
+ @Test
+ void parseParamsMultiple() {
+ assertEquals("java.lang.String, int",
BytecodeAnalyzer.parseParams("(Ljava/lang/String;I)V"));
+ }
+
+ @Test
+ void parseParamsEmpty() {
+ assertEquals("", BytecodeAnalyzer.parseParams("()V"));
+ }
+
+ @Test
+ void parseReturnType() {
+ assertEquals("java.lang.String",
BytecodeAnalyzer.parseReturn("(I)Ljava/lang/String;"));
+ assertEquals("void", BytecodeAnalyzer.parseReturn("()V"));
+ assertEquals("int", BytecodeAnalyzer.parseReturn("()I"));
+ }
+
+ @Test
+ void analyzePathProducesCorrectClassName() throws Exception {
+ CompilerTestHelper.writeSource(
+ sourceDir,
+ "test",
+ "Hello",
+ "package test; public class Hello { public String greet() {
return \"hi\"; } }");
+ CompilerTestHelper.compileAndAnalyze(sourceDir, outputDir);
+
+ Path classFile = outputDir.resolve("test/Hello.class");
+ assertTrue(Files.exists(classFile));
+
+ var analysis = BytecodeAnalyzer.analyze(classFile);
+ assertEquals("test.Hello", analysis.className());
+ assertNotNull(analysis.abiFingerprint());
+ assertNotNull(analysis.abiCanonical());
+ }
+
+ @Test
+ void analyzeBytesEquivalentToAnalyzePath() throws Exception {
+ CompilerTestHelper.writeSource(
+ sourceDir, "test", "Hello", "package test; public class Hello
{ public int value() { return 42; } }");
+ CompilerTestHelper.compileAndAnalyze(sourceDir, outputDir);
+
+ Path classFile = outputDir.resolve("test/Hello.class");
+ var fromPath = BytecodeAnalyzer.analyze(classFile);
+ var fromBytes =
BytecodeAnalyzer.analyze(Files.readAllBytes(classFile));
+
+ assertEquals(fromPath.className(), fromBytes.className());
+ assertEquals(fromPath.abiFingerprint(), fromBytes.abiFingerprint());
+ assertEquals(fromPath.abiCanonical(), fromBytes.abiCanonical());
+ assertEquals(fromPath.referencedTypes(), fromBytes.referencedTypes());
+ }
+
+ /**
+ * When running on JDK 24+, {@link BytecodeAnalyzer} uses {@link
ClassfileClassAnalyzer}
+ * instead of {@link AsmClassAnalyzer}. Both must produce identical
results for the same
+ * class file. This test cross-validates the two implementations against
each other.
+ */
+ @Test
+ void asmAndClassfileAnalyzersProduceIdenticalResults() throws Exception {
+ CompilerTestHelper.writeSource(sourceDir, "test", "Subject", """
+ package test;
+ public class Subject implements Runnable {
+ public static final String CONST = "hello";
+ private int secret = 42;
+ public String greet(int x, java.util.List<String> items) {
return CONST + x; }
+ @Override public void run() { greet(1, null); }
+ protected static int helper(byte b) throws
java.io.IOException { return b; }
+ }
+ """);
+ CompilerTestHelper.compileAndAnalyze(sourceDir, outputDir);
+
+ byte[] bytes =
Files.readAllBytes(outputDir.resolve("test/Subject.class"));
+
+ var asmResult = new AsmClassAnalyzer().analyze(bytes);
+ var cfResult = new ClassfileClassAnalyzer().analyze(bytes);
Review Comment:
Fixed in 950b860. Added `@EnabledForJreRange(min = JAVA_24)` and switched to
reflective instantiation of `ClassfileClassAnalyzer` to avoid a compile-time
dependency on the JDK 24+ class.
##########
pom.xml:
##########
@@ -192,6 +192,60 @@ under the License.
</plugin>
</plugins>
</pluginManagement>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-enforcer-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>enforce-java-build-version</id>
+ <goals>
+ <goal>enforce</goal>
+ </goals>
+ <configuration>
+ <rules>
+ <requireJavaVersion>
+ <!--
+ Building this plugin requires JDK 25+ so that the
java.lang.classfile
+ classes in src/main/java24/ can always be compiled (with
release=24).
+ The compiled plugin JAR targets JDK 17 for main sources
and JDK 24
+ for the classfile-backed bytecode analyzer, and runs on
any JVM >= 17.
Review Comment:
Fixed in 9a5c56d. CI workflow now uses `ff-jdk: '25'` and `jdk-matrix:
'["25"]'` to match the enforcer `[25,)` requirement.
##########
src/main/java/org/apache/maven/plugin/compiler/ToolExecutor.java:
##########
@@ -915,6 +919,201 @@ private static boolean removeFirsts(Deque<Path> paths,
Integer count) {
}
}
+ /**
+ * Compiles using the ABI-fingerprint incremental strategy. This method
handles the full
+ * lifecycle: determining what to compile, running javac with the analysis
TaskListener,
+ * cascading on ABI changes, and persisting state.
+ *
+ * @param compiler the compiler
+ * @param configuration the options to give to the Java compiler
+ * @param mojo the MOJO for configuration access
+ * @throws IOException if an error occurred while reading or writing a file
+ * @throws MojoException if the compilation failed
+ */
+ void compileWithAbiIncremental(JavaCompiler compiler, final Options
configuration, final AbstractCompilerMojo mojo)
+ throws IOException {
+ if (compiler instanceof ForkedTool) {
+ logger.warn("ABI incremental strategy is not supported with forked
compilation."
+ + " Falling back to full compilation.");
+ }
+ var abiBuild = new AbiIncrementalBuild(outputDirectory);
Review Comment:
Fixed in e79f971. Calling `abiBuild.invalidate()` right after construction
when `ForkedTool` is detected, so `initialize()` deletes the stale state file
and returns all files — making the full compilation fallback correct.
##########
src/main/java/org/apache/maven/plugin/compiler/incremental/BytecodeAnalyzer.java:
##########
@@ -0,0 +1,366 @@
+/*
+ * 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.Files;
+import java.nio.file.Path;
+import java.security.MessageDigest;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+
+import org.objectweb.asm.AnnotationVisitor;
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.ClassVisitor;
+import org.objectweb.asm.FieldVisitor;
+import org.objectweb.asm.MethodVisitor;
+import org.objectweb.asm.Opcodes;
+import org.objectweb.asm.Type;
+
+/**
+ * Analyzes compiled {@code .class} files using ASM to extract type references
+ * and compute bytecode-level ABI fingerprints.
+ *
+ * <p>Type references are collected from the class hierarchy, field/method
+ * descriptors, exception types, annotations, and bytecode instructions
+ * ({@code new}, {@code checkcast}, {@code instanceof}, field/method owners).
+ *
+ * <p>The bytecode ABI reflects erased types (no generics) and is computed from
+ * non-private, non-synthetic fields and methods.
+ *
+ * @see AbiExtractor
+ * @see DependencyScanner
+ */
+public class BytecodeAnalyzer {
+
+ public record ClassAnalysis(
+ String className, String abiFingerprint, String abiCanonical,
Set<String> referencedTypes) {}
+
+ public static ClassAnalysis analyze(Path classFile) throws IOException {
+ return analyze(Files.readAllBytes(classFile));
+ }
+
+ public static ClassAnalysis analyze(byte[] classBytes) {
Review Comment:
Done in 35bb1d2 + 950b860 + the current state. The plugin is now a proper
multi-release JAR: `java.lang.classfile` is used on JDK 24+, with ASM as
fallback on older JDKs. The JDK 24+ implementation lives in `src/main/java24/`,
and `BytecodeAnalyzer.selectAnalyzer()` picks the right implementation
reflectively at runtime.
--
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]