• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dynamically create a class at runtime  RSS feed

 
Ajay Dhar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to take a pojo, which may contain simple or complex datatypes like arraylists and convert it into a "string bean". This string bean must contain string equivalents for the attributes in the pojo.

For example, the pojo could be:


The "string bean" would then be:


I need to do this conversion at runtime.

I tried using the byte code manipulation library Javassist to do this and I am facing an issue. I can dynamically convert a class which contains simple types and Strings to a "string bean", however, javassist doesn't seem to let me generate a class with an array or arraylist field. Does anyone know how to dynamically generate arrays and arraylists using Javassist or some other approach? More broadly, does anyone know a good way to dynamically generate classes which include array and arraylist fields.

I have started researching some other byte code manipulation libraries like ASM and BCEL but they seem to be a little more difficult to use than Javassist.

Any guidance is appreciated.

Thanks,
Ajay
 
Aditya Karnad
Greenhorn
Posts: 16
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simplest approach would be to override the toString methods of the objects within your pojo (and that of your pojo itself...) in which case you won't have to bother making a string bean.

You may have thought of this already, so sorry if I'm not contributing anything here.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why won't a Map do?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!