javajvmjava-bytecode-asmbytecode-manipulationjvm-bytecode

Unpacking class file (bytes) into ASM ClassNode


Given a class file in the form of an array of bytes, how do you unpack it into an ASM ClassNode with its collection of MethodNodes? The PDF documentation doesn't seem to say – it seems to assume you will only be doing the reverse, creating a new class – and none of the constructors or methods in the Java doc seem applicable, unless I am missing something?


Solution

  • ClassNode extends ClassVisitor, so you may pass an instance to any method accepting ClassVisitor.

    ClassNode classNode = new ClassNode();
    new ClassReader(bytes).accept(classNode, 0);