gnodet commented on code in PR #1217: URL: https://github.com/apache/maven-compiler-plugin/pull/1217#discussion_r4792533772
########## src/test/java/org/apache/maven/plugin/compiler/ByteCodeTransformerTest.java: ########## @@ -0,0 +1,178 @@ +/* + * 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.HashMap; +import java.util.Map; +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 org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.ModuleVisitor; +import org.objectweb.asm.Opcodes; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +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}. + * The way that the Maven project is configured for the Compiler Plugin, + * Surefire does <em>not</em> prepends {@code META-INF/versions/22/} on the test classpath. + * Therefore, this test exercises ASM-based implementation. + * + * @see <a href="https://issues.apache.org/jira/browse/MCOMPILER-542">MCOMPILER-542</a> + */ +public class ByteCodeTransformerTest { + + private static final Log LOG = mock(Log.class); + + @Test + public void returnsNullForNonModuleClass(@TempDir Path tempDir) throws Exception { + byte[] bytes = compileRegularClass(tempDir); + assertNull( + ByteCodeTransformer.patchJdkModuleVersion(bytes, "21", LOG), + "Regular class has no ModuleAttribute -> null"); + } + + @Test + public void patchesJdkRequiresVersionToTarget(@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); Review Comment: Important: Test assumes ASM implementation is always loaded but doesn't verify multi-release JAR works. The comment (line 46-48) says the test exercises the ASM implementation because Surefire doesn't load META-INF/versions/22/, which is true during mvn test. But there's no integration test verifying that the packaged JAR correctly loads the java22 no-op implementation on JDK 22+ and the ASM implementation on JDK 17-21. **Failure scenario:** Multi-release JAR is misconfigured (e.g., missing Multi-Release: true in MANIFEST.MF, wrong directory structure) → java22 implementation never loads on JDK 22+ → ASM patch incorrectly applied on all JDKs → no test catches it because unit tests only run pre-packaging. **Fix:** Add an integration test that runs the packaged plugin JAR on both JDK 17-21 and JDK 22+ to verify multi-release behavior. _Claude Code on behalf of Guillaume Nodet_ ########## pom.xml: ########## @@ -196,6 +228,54 @@ 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 24+ 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 code, and runs on any JVM >= 17. + --> + <version>[24,)</version> + </requireJavaVersion> + </rules> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + <sources> + <!-- + Main sources target JDK 17 (the minimum runtime version). + The JDK 22+ and 24+ override are compiled separately into + META-INF/versions/<version>/ to form a proper multi-release JAR. + --> + <source> + <directory>src/main/java</directory> + <targetVersion>17</targetVersion> + </source> + <source> + <directory>src/main/java22</directory> Review Comment: Nit (optional): Sources section only defines java22, not java24, but enforcer assumes both. The <sources> section defines targetVersion 17 (main) and 22 (java22), with java24 commented out. This is inconsistent with the enforcer requiring JDK 24+ and the comments throughout the POM describing java24 sources as if they exist. **Failure scenario:** Build on JDK 24+ succeeds because java24 is optional → developer expects java24 MR-JAR override to exist → confused when only java22 exists, or assumes they need to implement it. **Fix:** Either uncomment java24 sources and implement the classfile API work, or update enforcer to require JDK 22+ and update all comments to reflect that java24 is future work. _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]
