« Overriding Java Classes | Main | Drupal Event Module »

January 4, 2006

Overriding Java Classes: Part 2

I was right about haveing to use some bytecode engineering to override classes. I ended up using Apache's BCEL, which allowed me to load up a class from an InputStream, modify it, then dump the bytes for the ClassLoader to load up. It was suprisingly easy, I just had to iterate through the ConstantPool of the class and replace and instances of "java.io.File" and "java/io/File" with my override. However, I think there is a catch that I need to account for, external (or worse, user-entered data) ConstantPools and Java Reflection. Using BCEL I can catch any references for java.io.File, but I can't stop a user from trying to Class.forName("java.io.File") where the String ("java.io.File") is generated at run-time, aka after class loading. So, 90% solution, not bad.

Posted by Guy at January 4, 2006 8:07 PM