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

Modifying the Code at run time

dinesh Venkatesan
Ranch Hand

Joined: Oct 12, 2006
Posts: 134
Hi All,

This could be a very silly question yet i would like to clarify with you.
I am having this doubt for a long time: is it possible to modify the currently executing program at run time. For example, in C and C++ we have conditional preprocessor directive. Does Java has something like that.

I got this question while trying to solve the following scenario:

try {
// Some code
}catch(Exception e) {
//Handled the Exception. Now i would like to redirect the control from
//here to again inside the try block with the modified code that caused
//the Exception.
}


Thanks in Advance and Sorry if the question is too silly!!!
dinesh.
Ajay Singh
Ranch Hand

Joined: Dec 13, 2006
Posts: 182

For example, in C and C++ we have conditional preprocessor directive.

conditional preprocessor director doesn't modify the code at runtime. It modifies at compile time.
dinesh Venkatesan
Ranch Hand

Joined: Oct 12, 2006
Posts: 134
Hi Ajay,

Thanks for pointing it. Could you give suggestion on the problem i specified.

thanks,
dinesh
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
You can't use goto in Java, so you can't jump back into the try block from the catch block.
You might be able to create new code on the fly using libraries like BCEL, but it's not something you want to do (makes testing almost impossible for example) as those libraries aren't meant for what you think you need to do.


42
dinesh Venkatesan
Ranch Hand

Joined: Oct 12, 2006
Posts: 134
Thank you Jeroen!!!
Thanks for your suggestions.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Dinesh, can you tell us more about what kind of modifications you are thinking of?

Depending on the requirements, there could be quite a number of differently complex solutions: simple recursion with parameters, the Strategy design pattern, use of a scripting language (Groovy?), byte code manipulation etc. pp.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
dinesh Venkatesan
Ranch Hand

Joined: Oct 12, 2006
Posts: 134
Hi Ilja,

Please look at the following snippet:



Thanks for your reply.
regards,
dinesh.
Ricky Clarkson
Ranch Hand

Joined: Jul 27, 2006
Posts: 131
Do you mean that you want to, while your program is running, edit its source by hand and have the changes be used? You can do that from within your IDE via the JVM's hotswap facility, but this is a bit limited. Frameworks such as OSGi aim to make this feature work all the time, but the simple answer is, at the moment, no.

If you want that, go for a language/runtime that explicitly supports it, rather than adds it in as an afterthought, such as Common Lisp.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Taking a hint from the C++ preprocessor reference, I guess you don't mind having a code based solution. Ilja suggested strategies. Something like this might do:

The DefaultHandler might display exceptions while a SilentHandler just ignores them, and a VerboseHandler displays and logs them. You'd have to plug in the right handler at runtime. This both better than a preprocessor because you don't have to recompile to turn logging on, and worse because you have to do a bit of runtime configuration.

Also Google up on Aspect Oriented Programming or AOP. AOP can insert new code, like logging, at any point, like every catch clause. If you want the same solution everywhere with no code changes, that can get you there. It's like an 11 on the complexity & learning curve, though.

Either of those help?
[ May 06, 2007: Message edited by: Stan James ]

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
dinesh Venkatesan
Ranch Hand

Joined: Oct 12, 2006
Posts: 134
Hi Guys,

Thanks! You people have opened lot of new ways to think. I will do some analysis on these ideas and will come back again.

regards,
dinesh.
Hung Tang
Ranch Hand

Joined: Feb 14, 2002
Posts: 148
Look up on reflection and classloading.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Modifying the Code at run time
 
Similar Threads
Is Java really faster than C++?
Interview questions
Exceptions doubt?
String array of main
Map vs. HashMap for hashes