Ashok
Ashok

Reputation: 1952

invoke a method using Javassist

I'm a newbie to Javassist and trying few tutorials. I understand that Javassist helps to manipulate bytecode and helps achieving structural reflection. As mentioned in wiki (http://en.wikipedia.org/wiki/Javassist), I believe it does have support to achieve reflection.

I'm trying to do a basic evaluation to understand howmuch time would java reflection takes to invoke a method and howmuch time would javassist takes. In the process, assume I have a class:

public class Addition {

  public int add(int a, int b){
     return a+b;
  }
}

Now, using java reflection api, I would use the following code to calculate howmuch time will be taken to invoke 100 objects:

import java.lang.reflect.*;

public class ReflectionClass {

  /**
   * @param args
   */
  public static void main(String[] args) {
    int numberOfObjects = 10000;
    double start,time;
    try {

      Class cls = Class.forName("Addition");
      Class partypes[] = new Class[2];
       partypes[0] = Integer.TYPE;
       partypes[1] = Integer.TYPE;
       Method meth = cls.getMethod(
         "add", partypes);
       Addition methobj = new Addition();
       Object arglist[] = new Object[2];
       arglist[0] = new Integer(37);
       arglist[1] = new Integer(47);


       start = System.currentTimeMillis();
       for(int i=0;i<numberOfObjects;i++){
         Object retobj= meth.invoke(methobj, arglist);
         Integer retval = (Integer)retobj;
         System.out.println(retval.intValue());
       }

       time = System.currentTimeMillis() - start;

       System.out.println("Method call for "+numberOfObjects +" objects is::"+time +" milliseconds");
    }
    catch (Throwable e) {
       System.err.println(e);
    }
  }

}

Now, I'm not sure how to proceed with javassist. i.e.,

I will create a ctclass object and store the class I'm going to read. and I could also get all the declaredmethods using getdeclaredmethods and store it in a ctMethod variable.

But, unlike java reflection api, I can't find an invoke method in ctMethod api of javassist. Does this mean, I've to create a duplicate method and have to make a call to the original method as explained in: http://www.ibm.com/developerworks/java/library/j-dyn0916/index.html.

I'm not sure if this is the right way to proceed.

Am I missing something?

Upvotes: 0

Views: 2734

Answers (1)

user1205938
user1205938

Reputation: 269

Javassist is used to change existing classes or generate new ones programmatically. Its reflective capabilities are used during the transformation of existing classes to find out details about the class being transformed.

The reflective capabilities of javassist are similar to those of the reflection api, but javassist has no way to invoke methods (unless you generate code that invokes those methods of course).

What you could do, is use javassist to transform the main method of your ReflectionClass such that the timing code is added dynamically, i.e. you could build a primitive profiler with it. Then you could leave the timing code out of the source file of ReflectionClass and add it at run-time when the class is loaded into the JVM.

Another approach would be to generate a class that 'invokes 100 objects' as you say, but timing it wouldn't be very interesting because it would run just as fast as regular java bytecode.

Upvotes: 1

Related Questions