1 Reply Latest reply on Feb 5, 2005 11:37 AM by chiba

    Detecting and replacing operators expressions with Javasssit

    ivangarcia44

      Hi,

      I am working on a project in which I have to instrument code in Java. Javassist can be used to instrument field access and method invocations, but it can not detect and replace operator expressions as shown below.

      This is a code example:

      public class Node {
      int elem;

      void swapNode(Node t) {
      if (elem - t.elem > 5) {
      // Do something..
      }
      }
      }

      After the instrumentation this is the resulting code:

      public class Node {
      int elem;

      void swapNode(Node t) {
      if (
      Expression._pc._update_GT( _get_elem()._minus(t._get_elem()) , 5)
      )
      {
      // ...
      }
      }

      Expression _get_elem() {
      // ...
      }
      }

      public class Expression {
      static PC _pc;

      public Expression _minus(Expression LeftOperand) {
      // return new Expression(this - LeftOperand);
      }
      }

      public class PC {
      static public boolean _update_GT(Expression e1, Expression e2) {
      // ...
      }
      }


      I was thinking of modifying the ExprEditor so it can detect opcodes operators like minus and greater than (- >). Once I detect them I have to create a class that inherits from the Expr class to replace the code and call the methods shown below. I have to implement the replace method and the proceed handler classes.

      Do you think that is possible doing this with Javassist or should I proceed using other library like BCEL or my own from the scratch?

      Thanks,
      Ivan Garcia