• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Class serialization  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello people,

is there any solution how to serialize the class in runtime ?

For example, if I have class A and I want to serialize new A().getClass(), then I get something, but not exactly the class bytes.

I do testing of a custom classloader that does bytecode modification. I want to compare the initial class and loaded class.

Daniel.
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serialising the Class object gives you data entirely unrelated to the bytecode of the class.

As far as I know, there is no general way to retrieve the bytecode of an arbitrary class, when it has already been loaded.

If all class loading is done via a custom ClassLoader, then you can save the bytecode for each class when you load it. Then you can retrieve that at any time later.
 
Daniel Travin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I know that there is no API to get byte[] from a java.lang.Class, but maybe someone knows a different approach. for example using asm or cglib ?
 
Java Cowboy
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serialization is about saving and loading the state of objects and does not have anything to do with writing or reading *.class files.

You have already answered your own question (asm, cglib). Since Java 5, Java also has an API to instrument classes at runtime: see the API documentation of the package java.lang.instrument.
[ November 08, 2007: Message edited by: Jesper Young ]
 
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!