Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization forum!
  • 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
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

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 ]
 
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!