Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

runtime bytecode generation

 
Raees Uzhunnan
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


Thanks
Raees
[ October 07, 2007: Message edited by: RAEES UZHUNNAN ]
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The usual way would be for the dynamically generated class to implement some pre-known interface to which a newly created object can then be cast.
 
Raees Uzhunnan
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

interface BaseKey
{
string getKey()
boolean validateKey()
}

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);

String getKey()
{
Return ��+a+b+c;
}
Boolean validateKey()
{// validation logic here
Return true;
}
}

Raees
[ October 08, 2007: Message edited by: RAEES UZHUNNAN ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool. Sounds fun enough.

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 ]
 
Raees Uzhunnan
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you pls put an example
 
Raees Uzhunnan
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

<Key>
<Item>
<Name>a</Name>
<Validation>
<startlimit>0< startlimit >
<endlimit>10<endlimit>
</ Validation >
</Item>
<Item>
<Name>b</Name>
<Validation>
<Enum>
10,20,30
</Enum>
</ Validation >
</Item>
<Item>
<Name>c</Name>
<Validation>
<Length>
8
</ Length >
</ Validation >
</Item>
</Key>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic