3 Replies Latest reply on Jun 8, 2005 10:19 AM by Shigeru Chiba

    how to insert an 'assert' instruction ?

    Nadir H Newbie

      Hello everyone,

      I am a beginner in the modification of java bytecode and i want to insert a 'assert(false)' instruction in the body of one method.

      My main question is : if you had to add an 'assert(false)' instruction to a class file how would you proceed with Javassist ?

      Here is what i tried so far but unfortunately it didn't work :
      At first i tried with CtMethod.insertAfter("assert(false)";) method to simply add it to the body of one method but you can't insert an 'assert' with these methods (the 'assert' instruction is not recognized by javassist).

      So in order to discover the "bytecode pattern" of the 'assert(false)' instruction I used a CodeIterator instance and I went trough the bytecode of this simple method :

      public static int func(int param) {
       assert(false);
       return 10 + param;
       }

      I finally isolate the following bytes (using the byteAt() method) for the 'assert(false)' instruction :
      byteAt index 0 : 178 GETSTATIC
      byteAt index 1 : 0
      byteAt index 2 : 9 T_SHORT
      byteAt index 3 : 154 IFNE
      byteAt index 4 : 0
      byteAt index 5 : 11 FCONST_O
      byteAt index 6 : 187 NEW
      byteAt index 7 : 0
      byteAt index 8 : 10 LCONST_1 or T_INT
      byteAt index 9 : 89 DUP
      byteAt index 10 : 183 INVOKESPECIAL
      byteAt index 11 : 0
      byteAt index 12 : 11 T_LONG
      byteAt index 13 : 191 ATHROW

      Then i tried to add this pattern byte by byte to my class file using CodeIterator.insert() and unfortunately it correctly add the bytes to the class file but it didn't result in an 'assert(false)' instruction...

      What am I doing wrong here ? Thank you very much for any help !