gnodet commented on code in PR #1144:
URL: 
https://github.com/apache/maven-compiler-plugin/pull/1144#discussion_r4134437391


##########
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` — same pattern as 
`BytecodeAnalyzer.selectAnalyzer()` — to avoid a compile-time dependency on the 
JDK 24+ class.



-- 
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]

Reply via email to