• 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
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

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
Sheriff
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 ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!