• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Modifying the Code at run time

 
dinesh Venkatesan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ajay,

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

thanks,
dinesh
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
dinesh Venkatesan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jeroen!!!
Thanks for your suggestions.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
dinesh Venkatesan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ilja,

Please look at the following snippet:



Thanks for your reply.
regards,
dinesh.
 
Ricky Clarkson
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
dinesh Venkatesan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look up on reflection and classloading.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic