wood burning stoves 2.0*
The moose likes Java in General and the fly likes runtime bytecode generation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "runtime bytecode generation" Watch "runtime bytecode generation" New topic
Author

runtime bytecode generation

Raees Uzhunnan
Ranch Hand

Joined: Aug 15, 2002
Posts: 126
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 ]

Sun Certified Enterprise Architect
Java Technology Blog
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
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 ]

Ping & DNS - my free Android networking tools app
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
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

Joined: Aug 15, 2002
Posts: 126
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

Joined: Jan 29, 2003
Posts: 8791
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

Joined: Aug 15, 2002
Posts: 126
Can you pls put an example
Raees Uzhunnan
Ranch Hand

Joined: Aug 15, 2002
Posts: 126
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>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: runtime bytecode generation