<p>a colleague pointed me the other day to <a href="http://jakarta.apache.org/bcel/" rel="nofollow noreferrer">BCEL</a> 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 <a href="https://blog.codinghorror.com/monkeypatching-for-humans/" rel="nofollow noreferrer">codinghorror post on monkey-patching</a> 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?</p>

[{'answer_id': 42477, 'author': 'wvdschel', 'author_id': 2018, 'author_profile': 'https://Stackoverflow.com/users/2018', 'pm_score': 2, 'selected': True, 'text': "<p>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.</p>\n"}, {'answer_id': 42745, 'author': 'morais', 'author_id': 2846, 'author_profile': 'https://Stackoverflow.com/users/2846', 'pm_score': 2, 'selected': False, 'text': "<p>From BCEL's FAQ:</p>\n\n<blockquote>\n  <p>Q: Can I create or modify classes\n  dynamically with BCEL?</p>\n  \n  <p>A: BCEL contains useful classes in the\n  util package, namely ClassLoader and\n  JavaWrapper.Take a look at the\n  ProxyCreator example.</p>\n</blockquote>\n\n<p>But monkeypatching is... uhm... controversial, and you probably shouldn't use it if your language doesn't support it.</p>\n\n<p>If you have a good use case for it, may I suggest embbededing Jython?</p>\n"}, {'answer_id': 43445, 'author': 'Georgy Bolyuba', 'author_id': 4052, 'author_profile': 'https://Stackoverflow.com/users/4052', 'pm_score': 0, 'selected': False, 'text': '<p>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).</p>\n'}, {'answer_id': 2196272, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>See this realworld example: <a href="http://jawk.sourceforge.net/compiler.php" rel="nofollow noreferrer">Jawk  - Compiler Module</a>. BCEL is useful for "compilation" ur custom language.</p>\n'}, {'answer_id': 14424643, 'author': 'v6ak', 'author_id': 228114, 'author_profile': 'https://Stackoverflow.com/users/228114', 'pm_score': 0, 'selected': False, 'text': '<p>BCEL does not support monkey patching, it just manipulates with bytecode and possibly loads it in a custom classloader. However you <strong>can implement monkeypatching on JVM</strong> using library like BCEL and Java agent. The Java agent (loaded by -javaagent argument) can access the Instrumentation API and modify loaded classes. It is not hard to implement it via some bridges.</p>\n\n<p>But remember:</p>\n\n<ul>\n<li>I am not sure if having to use -javaagent is something you want.</li>\n<li>In any language, monkey patching can lead to badly predictable behavior.</li>\n<li>You can modify a method. In theory, you can also add some method, but you need to compile the project against modified (patched) classes. I think this would cause a lot of pain and it is not worth of it. There are alternative languages that support it (e.g. Groovy) or suppport something similar (e.g. implicit conversions in Scala).</li>\n<li>It is better to design your API well than to use monkey patching. It may be rather useful for third party libraries.</li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/42452', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/292/']