I have a requirement to create a composite key from JMS message properties; the properties that need to be used for key is in a XML configuration file. At run time I have to read this configuration file and then create the composite key from the incoming JMS message. I can do this various ways. But I am looking for bytecode generation at runtime; can any one help or point me in right direction
The Javassist library (on SourceForge, part of the JBoss project) is very useful for generating classes at runtime. [ October 08, 2007: Message edited by: Ulf Dittmer ]
I'm curious about what happens next. You create a key object of a dynamically created class and ... who uses it and how? The class cannot be known by any other part of the system at compile time, right?
Yes , he is correct !. The idea is to populate key data and validation logic at runtime as per the configuration !. For achieving this we will define and interface like below (eg
and then provide the specialization based on client selection of the key attributes from the configuration file at runtime. The psedo code for new generated class will be like the below
Class NewKey implements BaseKey { Int a; Int b; Int c; NewKey(a,b,c);
Edit: I was on the run out the door before. This particular example seems simple enough to do with a generic class that concatenates some fields or creates an ordered list of name-value-pairs. Seems like any metadata that you use to generate code in the class - like the validate - could just as well be interpreted at runtime in the generic class.
Is an on-the-fly class really worth the trouble? [ October 08, 2007: Message edited by: Stan James ]
I agree with generics approach; but not for complex ideas like below; Even though I did not mention much about the validate function before; bulk of my code generation is going to benefit there. For example;
if I have a configuration like below ( again this is pseudo ); I wan to implement the code in the fly and this rules can change based on client deployment