views:

658

answers:

7

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

A: 

That should be difficult to realize. But you can create at runtime new classes and load them with a custom classloader. If you want to modify the code again, you have to reload the class.

Mnementh
that would be quite a factory method!
akf
A: 

You could always just use a dynamic language...

OOPMan
I think this question was posted out of boredom, so you answer might not be right.
01
-1 because question was explicit about Java
Peter Kofler
Java language or Java Virtual machine? There is a huge difference between to the two.
OOPMan
A: 

From BCEL:

The Byte Code Engineering Library is intended to give users a convenient possibility to analyze, create, and manipulate (binary) Java class files (those ending with .class). Classes are represented by objects which contain all the symbolic information of the given class: methods, fields and byte code instructions, in particular.

Jeremy Smyth
I really didn't get it Jeremy. Are you talking in the current context?
Rakesh Juyal
BCEL does one part of the work: it helps you to generate or manipulate new classes. The second thing is to load the new classes, that is built-in into Java: the reflection-API.
Mnementh
+1  A: 

I see these options for this purpose:

  • Generate the java source code and compile it with the external javac or the internal compiler tools (can't remember the name). And as you are responsible for the naming, just include a version count in the class name to avoid class loading anomalies.
  • Use the built in JavaScript engine support
  • Some scenarios can be solved using java Proxys

Edit: I once created a Java 1.4 program which took business rules from an existing legacy database, generated java files (basically implementations of a Predicate interface) with a bunch of println() from them and used the command line javac to compile them.

kd304
+1  A: 

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.

bendin
A: 

As an undergrad I got to work on the JikesRVM. It is a JVM implemented (mostly) in Java. At runtime it will JIT compile itself! It's a really cool piece of technology.

AgileJon
+6  A: 

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.

Brian Agnew
Instead of BCEL use objectweb ASM [http://asm.ow2.org/]
KitsuneYMG