Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Class serialization

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Life just hasn't been the same since the volcano erupted and now the air is full of tiny ads.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic