3 Replies Latest reply on Jun 9, 2003 1:09 AM by Ravi Pal

    Logging Example

    chris_grind Newbie

      I'm having trouble using Javassist for something I thought would be straightforward. Am I missing something obvious or is this a current limitation?

      I'm trying to instrumentate some methods to determine how long they take to execute. So at the beginning of the method I need a line such as:
      long start = System.currentTimeMillis();

      then at the end of the method I want to print out how long the method took, such as:
      System.out.println(System.currentTimeMillis() - start);

      It doesn't seem like I can do this using Javassist unless I want to manipulate the bytecodes directly.

      TIA - Chris

        • 1. Re: Logging Example
          Shigeru Chiba Expert

          You're right.

          Since the before advice and the after advice belong
          to separate scopes, "start" cannot be shared among
          them. Fixing this problem is in my Todo list but, sorry,
          I haven't done it. :<

          A quick fix is to make "start" be a static field.

          • 2. Re: Logging Example
            Ravi Pal Newbie

            There is a way to achieve the same in Javassist though:

            You can use ExpEditor class to modify the method body and hence you could use the same Object /variable without declaring static.

            e.g :

            new ExprEditor() {
            public void edit(MethodCall m) throws CannotCompileException{

            "long start = System.currentMillis...();"+
            "$_ = $proceed($$);"+




            • 3. Re: Logging Example
              Ravi Pal Newbie

              sorry for being abstract.. but please ignore the syntax part.