5 Replies Latest reply on Jul 9, 2003 11:17 PM by Ravi Pal

    Passing an Extra Parameter to the New generated Method...

    Ravi Pal Newbie

      Dear users,
      I am working on a task which is to make new method with one extra parameter , using the existing method source like :

      Original Method :
      Method A (int a) {
      return a+2;
      }

      Generated Method :
      MethodAx(int a, new MyClass b) {
      return a+2;
      }

      Than i need to change the original Method body to:

      Method A(int a) {
      MethodAx(a,new MyClass());
      }


      So basicaly it is redirection.

      My problem is i am not able to put the Extra parameter call inthe Original Method.

      I am not able to generate
      MethodAx(a,new MyClass()); this line of code.

      Any help will be greatly Appriciated.

      Thaking in advance.

      Ravi Pal

        • 1. Re: Passing an Extra Parameter to the New generated Method..
          Shigeru Chiba Expert

          What do you want to do with b passed
          to methodAx?

          • 2. Re: Passing an Extra Parameter to the New generated Method..
            Ravi Pal Newbie

            b is my own class.. which measures the memory used by the Methods.. in the class.

            It is somethign like this :

            class MyClass {
            int usage = 0;

            statis getMyClassObj() {
            .........
            return new MyClass();
            ..........
            }
            }

            So i measure for every class and method the memory used.

            Hope this helps.

            -- Ravi Pal

            • 3. Chiba-comment pl: Passing an Extra Parameter to the New gene
              Ravi Pal Newbie

              Hello Chibba.. i was waiting for your comments on this. Whether it is possible or not?

              i hope you will answer?

              --Ravi Pal

              • 4. Re: Chiba-comment pl: Passing an Extra Parameter to the New
                Shigeru Chiba Expert

                I'll add this feature to Javassist. So, please
                help me design the API for that.

                What I'd like to know is what you want to
                do with b in the generated method Ax:

                > Generated Method :
                > MethodAx(int a, new MyClass b) {
                > return a+2;
                > }

                In other words, let me know the body of Ax.
                The example above does not use b at all!

                Thanks,
                Chiba

                • 5. Re: Chiba-comment pl: Passing an Extra Parameter to the New
                  Ravi Pal Newbie

                  chiba,
                  well 'b' is an object of custome class. it could do anything. Over here in my case i am trying to manipulate some hidden variable in that object.

                  MethodAx(int a, new MyClass b) {
                  b.set(returning+"a+2");
                  ....
                  ..
                  b.setUsageMemory(calculateMemory());
                  ...
                  return a+2;
                  }

                  Something like this.

                  The easy way can be to introduce the new MyClass() object in the Existing method only... which will be very easy like :
                  MethodA(int a) {
                  MyClass b = new MyClass();
                  ...//any statements in original method
                  return a+2
                  }

                  But the problem is that if i keep on doing this in each and every method it is going to consume lots of memory in my case.. so i just want a wrapper which will redirect all the Method Invocations to my New METHOD Methodx(int a ,new MyClass b).... so that my application is not affected at all.

                  in any case i achieved 80% of my goal i could get the new Method Generated and i generate a new class also but the error i get is i could not invoke the new method from the original method. that is my problem.

                  In the example below : methoda is original method and i generate methodax with extra parameter.. i am able to do that successfully i guess (When i decompile my class file i can see the new method).

                  Originaly i have this method in the class :

                  methoda(int a) {
                  ..
                  // any statements.
                  ..
                  }

                  What i want is another method like this :
                  methodax(int a, MyClass b) {
                  //some statements plus my own statements
                  }

                  I can get like that without a problem. But i want to change the original method body to invoke the new gerenated method.

                  What i want is :
                  methoda(int a) {
                  methodax(int a, new myClass()) // i am not able to achieve this
                  }

                  What i get is (when i decompile my generated class):
                  methoda(int a) {
                  methodax(new Object[]{
                  new Integer(a), new MyClass(<no parameter>)
                  });
                  }

                  and when i try to run the Class using > java testClass

                  it gives errror that Some variable is not registered and you are trying to access it.

                  So... what i get is that the Method invocation which i want to make is.. a problem.

                  So if you could help me getting this it will be a big help. Thanks a lot.
                  --Ravi