Self modifying code in Java

Rakesh Juyal picture Rakesh Juyal · Jun 26, 2009 · Viewed 9.4k times · Source

Have you ever created or encountered a self modifying code in Java? If yes, then please post the link or simply post the code.

Answer

Brian Agnew picture Brian Agnew · Jun 26, 2009

Ignoring the world of grief you could be causing yourself via self-modifying code(!), it seems to me there are 3 options:

  1. use the inbuilt compiler support of Java 6 and write/recompile/reload classes
  2. use the Apache BCEL bytecode manipulation library to write your class directly
  3. make use of Java 6's inbuilt scripting support (or use Apache BSF) to write methods in your JVM scripting language of choice, and execute these

Of the three above, my initial choice (in the absence of requirements) would be to take a look at option 3. I suspect it's the least painful way to start. I've used all of the above - unfortunately I can't post links to client code.