2

Is there any way to write a loaded Java object into a .class file or is there any other type of file that can easily be read to represent an instance's properties.

For example, CGLIB will create an proxy bean which extends another, i really want to export this enhanced bean out to a file to see how it was enhanced.

3
  • You can use reflection APIs to analyze the java runtime object instance docs.oracle.com/javase/tutorial/reflect Commented Feb 2, 2016 at 6:47
  • cglib source code - github.com/cglib/cglib Commented Feb 2, 2016 at 6:48
  • You can serialize java objects into json string or stream and deserialize using gson. Commented Feb 2, 2016 at 6:52

2 Answers 2

3

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 enter image description here

  4. choose tools and you can save .class file,that is what you want enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Maybe it's worth to mention that the saved class files are stored in the current directory as your/package/TheClass.class.
2

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.

7 Comments

Thanks, so forget object instance. Is there any way to just write enhanced class generated by generateClass() out into .class file?
Pass a ClassWriter as an argument. Afterwards, call toByteArray on the writer and a byte[] array representing the class file is returned.
But you need a ClassReader first, however ClassReader constructor need class byte[] data. But what i have is just Class<?> aClass = CglibEnhancedProxy.class. I don't know how to convert aClass to byte[].
Not a ClassReader, you need a ClassWriter - note the Writer.
Oh, i understand it. But it seems doesn't do much help. Because this question is linked to A Spring AOP Strange Problem, So it looks hard to get the enhancer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.