0 Replies Latest reply on Apr 8, 2009 3:55 AM by jchrist

    reflection and java.lang.VerifyError

    jchrist

      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) {
      }
      
      


      Now, if I comment out the meth.invoke line, it runs just fine, but that defeats the point of the whole exercise. Again, if I insert this code into the original class source things work as expected. I only get the VerifyError when the method is inserted via javassist.

      So, any suggestions? Has anyone gotten reflection to work inside inserted code to the point where they can call methods via reflection?

      Thanks.