I'm using javassist to add some functionality to a third-party program.
I'm trying to insert some code into the constructor of my target class which calls methods elsewhere in the program to set fields in the target class.
An object passed to my target constructor has a private field whose members are the members I want to call. Since the field is private, I can't call the members directly. I'm therefore using standard java reflection to get access to the field's methods and to call them.
If I take the original target class source, add the code at the end of the constructor and run it works just fine. However, if I use code insertion
via javassist insert the *same* code at the end of the constructor, I get an error:
Java.lang.VerifyError:
com.companyX.moduleY.MyTargetClass, method signature(LcompanyX/moduleY/DataClass;)V Expecting to find integer on stack.
Here's the code I'm inserting:
try { java.lang.reflect.Field f=this.dataField.getClass().getDeclaredField("privateData"); f.setAccessible(true); Object o=f.get(this.dataField); java.lang.reflect.Method meth=o.getClass().getDeclaredMethod("getRSquared",(Class[])null); meth.setAccessible(true); this.rSquare=(Float)meth.invoke(o,(Object[])null); } except (Exception e) { }