How define class after byte code transofrmation with ASM (class file version 0.0)

后端 未结 1 572
谎友^
谎友^ 2021-01-16 08:51

I cannot load a class after byte code modification with ASM library.

Here it is identity transformer and I expect to get modified array with the same size as bytes o

1条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-16 09:51

    Those bytes correspond to 0xcafeba which matches the Java class file magic number, 0xcafebabe.

    The problem here is that new ClassWriter(reader, 0) does not do what you presumably believe it does; see the API documentation:

    classReader - the ClassReader used to read the original class. It will be used to copy the entire constant pool from the original class and also to copy other fragments of original bytecode where applicable.

    We still need to actually have the writer visit the reader via ClassReader.accept, as follows:

    reader.accept(writer, 0);
    

    As a side note, you don't need IOUtils.toByteArary as ClassReader has a constructor taking InputStream.

    0 讨论(0)
提交回复
热议问题