KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > interceptor > Sample1


1 package samples.interceptor;
2
3 import java.lang.reflect.InvocationHandler JavaDoc;
4
5 import alt.jiapi.InstrumentationContext;
6 import alt.jiapi.InstrumentationDescriptor;
7 import alt.jiapi.reflect.Loader;
8 import alt.jiapi.reflect.JiapiClass;
9 import alt.jiapi.util.Bootstrapper;
10 import alt.jiapi.util.InstrumentingClassLoader;
11
12 import alt.jiapi.interceptor.*;
13
14 /**
15  * Sample1 demonstrates how to intercept method invocations
16  * made.
17  * One is free to modify method call parameters / return value at will, as
18  * long as their type matches methods signature.
19  */

20 public class Sample1 implements InvocationHandler JavaDoc {
21     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
22         String JavaDoc className = "samples.Foo";
23         if (args.length > 0) {
24             className = args[0];
25         }
26
27         new Sample1(className);
28     }
29     
30     public Sample1(String JavaDoc className) throws Exception JavaDoc {
31         // Configure:
32
InstrumentationContext ctx = new InstrumentationContext();
33         InstrumentationDescriptor id = new InstrumentationDescriptor();
34         id.addInclusionRule("samples.*");
35         ctx.addInstrumentationDescriptor(id);
36         
37         // Associate interceptor with descriptor
38
// Set resolution to 'foo*', which means that interceptor applies
39
// its work only on invocations to methods which start with 'foo'
40
InvocationInterceptor ii = new InvocationInterceptor(id, "samples*",this);
41         // Launch samples.Foo
42
Bootstrapper.launch(className, null, ctx,
43                             InstrumentingClassLoader.createClassLoader(ctx));
44     }
45        
46
47     /**
48      * InvocationHandler interface
49      *
50      * @param o target instance. If this is null, target Method is
51      * static
52      * @param m Method, that this handler is supposed to invoke
53      * @param args Arguments, that are suitable to be passed to
54      * reflective method.
55      */

56     public Object JavaDoc invoke(Object JavaDoc o, java.lang.reflect.Method JavaDoc m, Object JavaDoc[] args) throws Exception JavaDoc {
57         System.out.println(" Calling " + m.getName());
58
59         for (int i = 0; i < args.length; i++) {
60             // Modify call parameters. If call param is string, convert it
61
// to upper case
62
if (args[i] instanceof String JavaDoc) {
63                 args[i] = ((String JavaDoc)args[i]).toUpperCase();
64             }
65         }
66
67         long l1 = System.currentTimeMillis();
68
69         // Make a call to target method through java.lang.reflect.Method
70
Object JavaDoc rv = m.invoke(o, args);
71
72         long l2 = System.currentTimeMillis();
73         System.out.println(" " + m.getName() + " executed in " +
74                            (l2 - l1) + " ms");
75
76         // Remember to return value. We could change the return
77
// value if desired. One must make sure, that the type
78
// of the return value is valid.
79
if (rv instanceof Integer JavaDoc) {
80             int i = ((Integer JavaDoc)rv).intValue();
81             rv = new Integer JavaDoc(i + 1);
82         }
83
84         return rv;
85     }
86 }
87
Popular Tags