gnodet commented on code in PR #1144: URL: https://github.com/apache/maven-compiler-plugin/pull/1144#discussion_r4131851122
########## 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: Valid concern — ASM version churn is a recurring tax. However, the ClassFile API (`java.lang.classfile`) was only finalized in JDK 24 (JEP 484), and the plugin targets JDK 17 (`<javaVersion>17</javaVersion>`). We can't use it as the primary implementation today. That said, the fallback-with-warning approach you suggest is feasible: we could detect JDK 24+ at runtime and prefer the ClassFile API, falling back to ASM on older JDKs (with a warning). This would be a natural follow-up once the ClassFile API has more adoption. Note that ASM is already a dependency of the plugin — `ByteCodeTransformer` uses it for the JDK-8318913 workaround (module-info.class patching). So this PR doesn't introduce a new dependency, it just adds another consumer. For the immediate concern (ASM not supporting newer class file versions): ASM 9.x supports up to JDK 24 class files, and the `BytecodeAnalyzer` only reads structural metadata (no code transformation), so it's relatively resilient to format changes. But I agree that migrating to ClassFile API when possible is the right direction. Happy to add a TODO/issue tracking this, or if you feel strongly, I can implement the dual-path (ClassFile API preferred, ASM fallback) in this PR — though it would add complexity. -- 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]
