Converting a given class (let's say, java.lang.Object) to a byte array. Is it possible?

后端 未结 4 1404
栀梦
栀梦 2021-01-13 17:13

Given that class loaders accept to take as input a byte array of a given class, returning a Class, I wonder whether it is possible to do the reverse, t

4条回答
  •  长情又很酷
    2021-01-13 17:52

    Here's something to try with BCEL:

    JavaClass clazz = Repository.lookupClass("foo.bar.YourClass");
    byte[] bytes = clazz.getBytes();
    

    I didn't know the answer prior to reading your question, but I knew I should check the following libraries, because they support the Java class file format:

    • BCEL
    • javassist
    • asm

    If the above doesn't work, look for something else in these libraries.

提交回复
热议问题