Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Self modifying code in Java [closed]

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

like image 341
Rakesh Juyal Avatar asked Feb 03 '23 10:02

Rakesh Juyal


2 Answers

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.

like image 155
Brian Agnew Avatar answered Feb 06 '23 16:02

Brian Agnew


You can write (Java) code that generates new classes (byte code) at runtime using a library like bcel. That's not quite the same as self-modifying code. I suspect self-modifying code is not something the JVM supports.

For an example of generating new code at runtime, have a look at the source code of clojure.

like image 20
bendin Avatar answered Feb 06 '23 15:02

bendin