3 Replies Latest reply on Dec 9, 2004 10:11 AM by Shigeru Chiba

    javassist-3.0RC1: replace(

    Reza Hay Newbie

      Hello,
      I have tried the following code, based on the tutorial:

      =================================================
      public class Test {

      public static void main(String[] args) throws Exception {
      ClassPool pool = ClassPool.getDefault();
      pool.insertClassPath("some-path");
      CtClass cc = pool.get("nl.vu.cs.iids.MyPoint");
      CtMethod sMethod = cc.getDeclaredMethod("setTest1");
      sMethod.instrument(new ExprEditor() {
      public void edit(MethodCall m) throws CannotCompileException {
      System.out.println("========> " + m.getMethodName() + " line: " + m.getLineNumber());
      m.replace("{}");
      //m.replace("{ $1 = 0; newX = $1 + 50.2; $_ = $proceed($$); System.out.println(\"This is added line.\"); }");
      }
      });
      }
      }
      ===============================================
      when I run the program I get the following error:

      ========> toString line: 28
      javassist.CannotCompileException: the resulting value is not stored in $_
      at javassist.expr.Expr.checkResultValue(Expr.java:170)
      at javassist.expr.MethodCall.replace(MethodCall.java:209)
      at nl.vu.cs.iids.Test$1.edit(Test.java:32)
      at javassist.expr.ExprEditor.doit(ExprEditor.java:118)
      at javassist.CtBehavior.instrument(CtBehavior.java:362)
      at nl.vu.cs.iids.Test.main(Test.java:29)
      Exception in thread "main"

      ===============================================
      As I know, when we want to remove a method-call expression we can just give the "{}" source text to the replace method. Am I right?

      Thanks in advance
      Reza

        • 1. 3855800
          Shigeru Chiba Expert

          Is there a CVS tag to use to get the revision of the source corresponding to release 1.0.0? I was going to fix a bug I found and didn't want to deal with any new bugs that might be in development.

          • 2. Re: javassist-3.0RC1: replace("{}") doesn't work.
            Reza Hay Newbie

            I'm sorry. I don't understand your answer. I try to say that replace("{}") doesn't work with javassist-3.0RC1. Which release should I use?

            Thanks.

            • 3. Re: javassist-3.0RC1: replace(
              Shigeru Chiba Expert

               

              As I know, when we want to remove a method-call expression we can just give the "{}" source text to the replace method. Am I right?


              No. You cannot remove a method-call expression. I cannot come up
              with any reasonable algorithm for removing it. For example, suppose
              you have a *statement* like this:

              obj.bar(obj.foo(), 1);


              obj is some object. bar is a method taking two parameters.
              foo is a method returning an int value. If you remove this method
              call expression "obj.foo()" in the statement above,

              obj.bar(<removed>, 1);


              How do I fill the removed parameter in the above statement?

              Since I don't know an algorithm for filling the removed term,
              Javassist does not allow you to remove a method call expression.
              If you know it, can you tell me? I would like to implement that
              algorithm...