Topic
  • 1 reply
  • Latest Post - ‏2009-02-23T20:25:13Z by SystemAdmin
SystemAdmin
SystemAdmin
235 Posts

Pinned topic Insert a line of code in existing byte code

‏2009-02-23T10:45:18Z |
I have an existing jar. The jar file may contain instantiations of some class, say, ClassA.
Now I want to insert a line of code just after the instantiation:
E.g:
Initial code:
ClassA objA = new ClassA();
objA.dispose();
Modified code:
ClassA objA = new ClassA();
objA.doInstrumentation();
objA.dispose();

Also there could be situations where some class, say ClassB that extends ClassA is instatiated.
Even in such case I need to identify such a situation and insert objB.doInstrumentation.

Now another big catch is that at compile time the method doInstrumentation() does not exist at all.

Any ideas how to achieve this.
Thanks already.
Suraj
Updated on 2009-02-23T20:25:13Z at 2009-02-23T20:25:13Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    235 Posts

    Re: Insert a line of code in existing byte code

    ‏2009-02-23T20:25:13Z  
    You could create a custom class loader which processes the byte code using a library like Objectweb's ASM to add the line of code if the class extends ClassA. I assume if ClassC extends ClassB extends ClassA you need this as well.