write java object into class file

情到浓时终转凉″ 提交于 2019-12-06 11:15:42

For cglib, instances can only be serialized and only if the instance's method interceptors support serialization. There is no other way.

In order to get hold of a cglib-generated class file, you can call the

void generateClass(ClassVisitor v)

method. This method can take an ASM ClassWriter which can after calling the method emitt a byte array representing the class file. This class file by itself does however not help you much as cglib needs to initialize the class explicitly, e.g. inject the callback handlers into the class's fields. This is done inside of the library. However, with the class file at hand and with debug-mode introspection, you can add the pieces and understand how your enhanced class works.

Finally, if I can recommend you an alternative to cglib where all this is easier, have a look at Byte Buddy which has a straight-foward API for extracting a class file and also offers so-called LoadedTypeInitializers in parallel to this class file. The latter initializers contain any setup logic for a class and are easy to read. Also, they are themselfs serializable.

to use HSDB tool in JDK

  1. keep your program running
  2. run HSDB tool:

    java -classpath "$JAVA_HOME/lib/sa-jdi.jar" sun.jvm.hotspot.HSDB

  3. input the program pid

  4. choose tools and you can save .class file,that is what you want

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!