views:

330

answers:

4

a colleague pointed me the other day to BCEL which , as best I can tell from his explanation and a quick read, a way to modify at run time the byte code. My first thought was that it sounded dangerous, and my second thought was that it sounded cool. Then I gave it some more thought and I recalled the codinghorror post on monkey-patching and realized that this was basically the same thing. Has anyone ever used BCEL for anything practical? Am I right that this is basically run time monkey patching, or am I missing something?

A: 

It's a bit more low-level than classic monkey patching, and from what I read, the classes already loaded into the VM are not updated. It only supports saving it to class files again, not modifying run time classes.

wvdschel
+1  A: 

From BCEL's FAQ:

Q: Can I create or modify classes dynamically with BCEL?

A: BCEL contains useful classes in the util package, namely ClassLoader and JavaWrapper.Take a look at the ProxyCreator example.

But monkeypatching is... uhm... controversial, and you probably should use it if your language doesn't support it.

If you have a good use case for it, may I suggest embbededing Jython?

morais
A: 

You might look at it as monkey patching. I prefer not to use it (maybe I never faced a good use case for it?), but be familiar with it (to have an idea how Spring and Hibenrate use it and why).

Georgy Bolyuba
A: 

See this realworld example: Jawk - Compiler Module. BCEL is useful for "compilation" ur custom language.