2 Replies Latest reply on Jun 20, 2005 2:45 PM by chiba

    Accessing a parameter in a method's body

    nadz

      In a simple class like this :

      public class Test1 {
      static int res;

      public static void main(String[] args) {
      foo(2);
      }

      public static int foo(int num) {
      res = num + 2;
      return res;
      }
      }


      How can i access the value of the parameter in red in the method call 'foo(x)' (here this value is 2) ? I can replace this statement and his parameter "$1" using MethodCall.replace(...) but i don't succeed to get the value of this "$1" parameter. How can I do this ?

      Thank you very much !

        • 1. Re: Accessing a parameter in a method's body
          nadz

          For example i have the class :

          public class Test1 {
           static int res;
          
           public static void main(String[] args) {
           foo (2);
           }
          
           public static int foo(int num) {
           res = num + 2;
           return res;
           }
          }

          I want to get the parameter value of foo (here '2') and the returned value (here '4') and then transform the class by adding it 2 fields with these values like this :
          public class Test1 {
           static int res;
           static int paramValue = 2;
           static int returnValue = 4;
           public static void main(String[] args) {
           foo (2);
           }
          
           public static int foo(int num) {
           res = num + 2;
           return res;
           }
          }

          Is there a way to perform this with Javassist ?

          Many thanks for any help !


          • 2. Re: Accessing a parameter in a method's body
            chiba

            paramValue and returnValue are determined at runtime.
            Since those values are not available during compile/load time,
            you cannot obtain them by Javassist.

            Although those values can be guessed by performaing
            constant propagation since they are constant values,
            Javassist does not support it.