KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > jazzpect > Sample1


1 package samples.jazzpect;
2
3 import org.aopalliance.intercept.MethodInterceptor;
4 import org.aopalliance.intercept.MethodInvocation;
5
6 import alt.jiapi.jazzpect.Initializer;
7
8 /**
9  * Class Sample1.
10  *
11  * @author Mika Riekkinen
12  */

13 public class Sample1 implements MethodInterceptor {
14     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
15         String JavaDoc className = "samples.Foo";
16         if (args.length > 0) {
17             className = args[0];
18         }
19
20         new Sample1(className);
21     }
22
23
24     public Sample1(String JavaDoc className) throws Exception JavaDoc {
25         // For some reason, if resolution is "*", instrumentation fails
26
// Initializer i = new Initializer("samples.*", this);
27
Initializer i =
28             new Initializer(new String JavaDoc[] {"samples.*"}, null,
29                             "samples.*", this);
30         // Run the main method of class
31
i.runMainMethod(className, null);
32
33         // alternatively, we could get the classloader from initializer
34
// and load the class manually. If done this way, care must
35
// be taken, that *all* the relevant(application) classes are loaded
36
// by i.getClassLoader().
37
}
38
39
40     // Interface MethodInterceptor: ---------------------------
41
public Object JavaDoc invoke(MethodInvocation mi) throws Throwable JavaDoc {
42         // return mi.proceed();
43
long l1 = System.currentTimeMillis();
44         // Object[] args = mi.getArguments();
45
Object JavaDoc o = mi.proceed();
46         long l2 = System.currentTimeMillis();
47
48         System.out.println("It took " + (l2-l1) + " ms to invoke " +
49                            mi.getMethod().getName());
50
51         return o;
52     }
53 }
54
Popular Tags