• 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 ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Dynamically create a class at runtime

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.

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.
Posts: 67447
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why won't a Map do?
Have you no shame? Have you no decency? Have you no tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic