Dynamic code manipulation and generation
3 posts in topic
Flat View  Flat View

Posted By:   roger_tong
Posted On:   Wednesday, May 30, 2001 11:24 PM

Is there a way to dynamically generate code in a class? In other words, may I optionally add some methods or some lines of code, or drop some methods or some lines of code to a class during runtime?

Re: Dynamic code manipulation and generation

Posted By:   Erin_Mulder  
Posted On:   Wednesday, September 25, 2002 11:23 AM

There are a number of dynamic code generation possibilities, but it's hard to recommend one without a better idea of your problem. Possible solutions:

  1. Use dynamic proxies. These allow you to create on-the-fly interface implementations which can adapt to various method calls, etc. Very often, they're used to wrap a real implementation class and change the behavior in some way.

  2. Generate and compile source files at runtime. Lots of app servers do this when you deploy enterprise applications, but it's pretty slow, and you'll need to drop your classloader to be able to change an already loaded class.

  3. Generate byte code in memory at runtime. This is much faster than option 2, but requires the use of a byte-code library like BCEL and can be a lot of effort if you're doing something complex. Probably only appropriate where speed is critical or your input is some sort of meta-language.

You can view/download useful slides on this topic at:

Re: Codes generation

Posted By:   Luigi_Viggiano  
Posted On:   Thursday, May 31, 2001 01:34 AM

This is not provided from Java Language itself.

To do that, you'll need:

  1. A tool to manage bytecode (like Jikes Bytecode Toolkit for example)
  2. To force class reload after your changes on bytecode of your class.

For the (2) you may need to define and use your own ClassLoader to not cache your specific "dynamic" classes. For example you can make a ClassLoader able to load a class from a byte array, and with a specific bytecode tool you can make modification to this byte array before to reload the class.

Re: Codes generation

Posted By:   Dermot_Hennessy  
Posted On:   Thursday, May 31, 2001 01:23 AM

This is a little off the point, but may be of interest to you in any case. I do not think that runtime compilation is possible (others can correct me if I'm wrong). It might be possible to do what you suggest in a roundabout way by using delegation to different classes chosen at runtime (but most likely known at compile time) which offer different implementations of certain methods - you would probably use reflection here. (You might be able to combine reflection and a properties file containing the names of the delegate classes to add more dynamism).

That's by way of introduction to my 'off-topic' point. You can put debug code into your classes for test purposes by doing a check against a public static final variable (e.g. called trace) which is set to true initially.


//... In member declaration

public static final boolean trace = true;

//... In methods

if (trace)
// System.out.println("Trace: blaah blaah");

If you later set this variable to false and recompile, most JDKs will optimise the compilation and remove the debug code blocks from the generated code.

Hope this helps,

About | Sitemap | Contact