package demo; import java.lang.classfile.ClassElement; import java.lang.classfile.ClassFile; import java.lang.classfile.ClassModel; import java.lang.classfile.CodeElement; import java.lang.classfile.FieldModel; import java.lang.classfile.Instruction; import java.lang.classfile.MethodModel; import java.lang.classfile.attribute.SourceFileAttribute; import java.lang.classfile.instruction.InvokeDynamicInstruction; import java.lang.classfile.instruction.InvokeInstruction; import java.lang.reflect.AccessFlag; import java.util.ArrayList; import java.util.List; import java.util.TreeMap; import java.util.stream.Collectors; /** Reading a class with java.lang.classfile: ClassFile.of().parse(...) and walking the model. */ public class ParseDemo { /** One line per method: flags name descriptor. Shared with the ASM comparison, which must print the same lines. */ static List methodLines(ClassModel cm) { List lines = new ArrayList<>(); for (MethodModel m : cm.methods()) { lines.add(flags(m.flags().flags()) + m.methodName().stringValue() + m.methodType().stringValue()); } return lines; } static String flags(java.util.Set fs) { return fs.stream().map(f -> f.name().toLowerCase()).sorted().collect(Collectors.joining(",")) + " "; } /** Calls per "owner.name" in all method bodies, using a pattern-matching switch over code elements. */ static TreeMap invokeCounts(ClassModel cm) { TreeMap counts = new TreeMap<>(); for (MethodModel m : cm.methods()) { m.code().ifPresent(code -> { for (CodeElement e : code) { switch (e) { case InvokeInstruction i -> counts.merge( i.owner().asInternalName() + "." + i.name().stringValue(), 1, Integer::sum); case InvokeDynamicInstruction i -> counts.merge( "indy:" + i.name().stringValue(), 1, Integer::sum); default -> { } } } }); } return counts; } public static void main(String[] args) throws Exception { byte[] bytes = Bytes.of(Target.class); ClassModel cm = ClassFile.of().parse(bytes); System.out.println("== header =="); System.out.println("bytes : " + bytes.length); System.out.println("this : " + cm.thisClass().asInternalName()); System.out.println("super : " + cm.superclass().orElseThrow().asInternalName()); System.out.println("version : " + cm.majorVersion() + "." + cm.minorVersion() + " (ClassFile.JAVA_25_VERSION = " + ClassFile.JAVA_25_VERSION + ")"); System.out.println("flags : " + flags(cm.flags().flags())); System.out.println("constant pool : " + cm.constantPool().size() + " slots"); System.out.println("\n== fields =="); for (FieldModel f : cm.fields()) { System.out.println(flags(f.flags().flags()) + f.fieldName().stringValue() + " " + f.fieldType().stringValue()); } System.out.println("\n== methods =="); methodLines(cm).forEach(System.out::println); System.out.println("\n== class elements, in file order (the model is a stream of elements) =="); for (ClassElement ce : cm) { String d = switch (ce) { case MethodModel m -> "method " + m.methodName().stringValue(); case FieldModel f -> "field " + f.fieldName().stringValue(); case SourceFileAttribute a -> "SourceFile=" + a.sourceFile().stringValue(); default -> ce.getClass().getSimpleName().replaceAll("Impl$", ""); }; System.out.println(" " + d); } System.out.println("\n== fib(I)I, instruction by instruction =="); MethodModel fib = cm.methods().stream().filter(m -> m.methodName().equalsString("fib")).findFirst().orElseThrow(); for (CodeElement e : fib.code().orElseThrow()) { if (e instanceof Instruction i) { String extra = e instanceof InvokeInstruction inv ? " " + inv.owner().asInternalName() + "." + inv.name().stringValue() + inv.type().stringValue() : ""; System.out.println(" " + i.opcode() + extra); } } System.out.println("\n== invoke targets in the whole class =="); TreeMap counts = invokeCounts(cm); counts.forEach((k, v) -> System.out.println(" " + v + " x " + k)); System.out.println("\n== a JDK class from the runtime image: java.lang.String =="); byte[] sb = Bytes.jdk("java.base", "java/lang/String"); ClassModel string = ClassFile.of().parse(sb); System.out.println("bytes " + sb.length + ", major " + string.majorVersion() + ", methods " + string.methods().size() + ", fields " + string.fields().size() + ", interfaces " + string.interfaces().size()); Check.that(cm.majorVersion() == ClassFile.JAVA_25_VERSION, "Target.class is class-file version 69 (compiled with --release 25)"); Check.that(cm.methods().size() == 8, "Target has 8 methods (constructor + 7)"); Check.that(counts.getOrDefault("demo/Target.fib", 0) == 2, "fib calls itself twice"); Check.that(counts.containsKey("indy:makeConcatWithConstants"), "greet() uses invokedynamic string concatenation"); Check.finish(); } }