gnodet commented on code in PR #1158: URL: https://github.com/apache/maven-compiler-plugin/pull/1158#discussion_r4605925912
########## 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 the root {@code ByteCodeTransformer} using the standard + * {@code java.lang.classfile} API instead of ASM, removing the ASM dependency + * from the bytecode transformation path on modern JVMs. + * + * <p>This class is loaded automatically by the JVM on JDK 24+ via the + * multi-release JAR mechanism ({@code META-INF/versions/24/}). On JDK 17–23, + * the root implementation backed by ASM is used instead. + * + * @see <a href="https://bugs.openjdk.org/browse/JDK-8318913">JDK-8318913</a> + */ +final class ByteCodeTransformer { + private ByteCodeTransformer() {} + + /** + * JDK-8318913 workaround: Patch module-info.class to set the java release version for java/jdk modules. + * This patch is needed only for Java versions older than 22. + * + * <p>This implementation uses {@code java.lang.classfile} (JDK 24+ standard API) instead of ASM. + * + * @param originalBytecode the byte code to patch + * @return the patched byte code, or {@code null} if no change is needed + * + * @see <a href="https://issues.apache.org/jira/browse/MCOMPILER-542">MCOMPILER-542</a> + * @see <a href="https://bugs.openjdk.org/browse/JDK-8318913">JDK-8318913</a> + */ + static byte[] patchJdkModuleVersion(byte[] originalBytecode, String javaVersion, Log log) { Review Comment: Good catch on the motivation question. The JDK 24+ override doesn't exist just to paper over the pre-22 bug — it's here to avoid ASM entirely on modern JVMs. The root `ByteCodeTransformer` uses ASM to parse and rewrite the module-info class; that works, but it means ASM is on the hot path for every MRJAR build on JDK 17–23. By overriding the method in the `java24` source set with an implementation backed by `java.lang.classfile`, we get the same semantics with no third-party dependency on JDK 24+. The method still needs to do real work (patch the `requires` version attributes) — it just does it differently. Throwing `AssertionError` would mean the patching is silently skipped for all JDK 24+ users, which is incorrect. -- 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]
