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


##########
src/main/java24/org/apache/maven/plugin/compiler/ByteCodeTransformer.java:
##########
@@ -0,0 +1,113 @@
+/*
+ * 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;
+
+import java.lang.classfile.Attributes;
+import java.lang.classfile.ClassFile;
+import java.lang.classfile.attribute.ModuleAttribute;
+import java.lang.classfile.attribute.ModuleRequireInfo;
+import java.util.ArrayList;
+import java.util.HashSet;
+
+import org.apache.maven.api.plugin.Log;
+
+/**
+ * JDK 24+ override of {@link ByteCodeTransformer} using the standard

Review Comment:
   🔵 **Still present from previous review — Nit:** `{@link 
ByteCodeTransformer}` points to this class itself (both the root and the 
versioned override share the same FQCN in an MRJAR). Javadoc tools will render 
this as a link to the same class. Consider rephrasing to `{@code 
ByteCodeTransformer}` since the reader can't navigate to the root variant 
through Javadoc anyway.



##########
src/test/java/org/apache/maven/plugin/compiler/ByteCodeTransformerTest.java:
##########
@@ -0,0 +1,138 @@
+/*
+ * 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;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.spi.ToolProvider;
+
+import org.apache.maven.api.plugin.Log;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+
+/**
+ * Tests for {@link ByteCodeTransformer#patchJdkModuleVersion}.
+ *
+ * <p>On JDK 24+, surefire prepends {@code META-INF/versions/24/} on the test 
classpath,
+ * so this test exercises the {@code java.lang.classfile}-backed 
implementation.
+ * On JDK 17-23, it exercises the ASM fallback.
+ *
+ * @see <a 
href="https://issues.apache.org/jira/browse/MCOMPILER-542";>MCOMPILER-542</a>
+ */
+class ByteCodeTransformerTest {
+
+    private static final Log LOG = mock(Log.class);
+
+    @Test
+    void returnsNullForNonModuleClass(@TempDir Path tempDir) throws Exception {
+        byte[] bytes = compileRegularClass(tempDir);
+        assertNull(
+                ByteCodeTransformer.patchJdkModuleVersion(bytes, "21", LOG),
+                "Regular class has no ModuleAttribute -> null");
+    }
+
+    @Test
+    void patchesOrSkipsModuleWithJdkRequires(@TempDir Path tempDir) throws 
Exception {
+        byte[] bytes = compileModuleInfo(
+                tempDir, "jdkRequires", "module com.example { requires 
java.base; requires java.logging; }");
+        byte[] result = ByteCodeTransformer.patchJdkModuleVersion(bytes, "21", 
LOG);
+        // If the compiler emitted version attributes on requires (JDK 21+ 
behaviour),
+        // patchJdkModuleVersion patches them and returns non-null non-empty 
bytes.
+        // If not, it returns null (nothing to patch). Both outcomes are 
correct.
+        if (result != null) {
+            assertTrue(result.length > 0, "Patched result must be non-empty");
+            assertTrue(
+                    result[0] == (byte) 0xCA
+                            && result[1] == (byte) 0xFE
+                            && result[2] == (byte) 0xBA
+                            && result[3] == (byte) 0xBE,
+                    "Result must be a valid class file (CAFEBABE magic)");
+        }

Review Comment:
   🔵 **Still present from previous review:** Both 
`patchesOrSkipsModuleWithJdkRequires` and `patchedBytesAreValidClassFile` only 
verify CAFEBABE magic on the output. Neither asserts that the patched version 
string is actually `"21"`. The tests prove the output is a valid classfile but 
not that the patching *worked*.
   
   Consider re-parsing the result with `ClassFile.of().parse()` and checking 
that `java.logging` / `java.base` requires entries carry version `"21"` instead 
of the original JDK version.



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