KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > interceptor > Sample2


1 package samples.interceptor;
2
3 import java.lang.reflect.Field 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  * Sample2 demonstrates how to intercept field accesses
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 Sample2 implements AccessAdvisor, FieldHandler {
21     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
22         new Sample2();
23     }
24     
25     public Sample2() throws Exception JavaDoc {
26         // Configure:
27
InstrumentationContext ctx = new InstrumentationContext();
28         InstrumentationDescriptor id = new InstrumentationDescriptor();
29         id.addInclusionRule("samples.*");
30         ctx.addInstrumentationDescriptor(id);
31         
32         // Associate interceptor with descriptor
33
// Set resolution to 'samples*', which means that interceptor applies
34
// its work only on fields which start with 'samples'
35

36         // FieldInterceptor uses AccessAdvisor
37
FieldInterceptor fi = new FieldInterceptor(id, "samples*",this);
38         // FieldInterceptor2 uses reflection/FieldHandler. This is obsolete.
39
// Performance is roughly 100 times slower
40
//FieldInterceptor2 fi2 = new FieldInterceptor2(id, "samples*Bar*", this);
41

42         // Launch ITest
43
Bootstrapper.launch("samples.Foo", null, ctx,
44                             InstrumentingClassLoader.createClassLoader(ctx));
45     }
46        
47
48     /**
49      * FieldAdvisor
50      *
51      * @param o target instance. If this is null, target Method is
52      * static
53      * @param m Method, that this handler is supposed to invoke
54      * @param args Arguments, that are suitable to be passed to
55      * reflective method.
56      */

57     public Object JavaDoc get(Object JavaDoc o, String JavaDoc name, Object JavaDoc value) {
58     System.out.println(" Getting field " + name + ": ");
59
60          if (value instanceof String JavaDoc) {
61              value = ((String JavaDoc)value).toUpperCase();
62          }
63          else if (value instanceof Integer JavaDoc) {
64              value = new Integer JavaDoc(100);
65          }
66
67         return value;
68     }
69
70
71     /**
72      * FieldAdvisor interface
73      *
74      * @param o target instance. If this is null, target Method is
75      * static
76      * @param m Method, that this handler is supposed to invoke
77      * @param args Arguments, that are suitable to be passed to
78      * reflective method.
79      */

80     public Object JavaDoc set(Object JavaDoc o, String JavaDoc name, Object JavaDoc value) {
81         System.out.println(" Setting field " + name + ": " + value);
82         return value;
83     }
84
85
86     // FieldHandler interface -- Used with FieldInterceptor2
87
public void setField(Object JavaDoc target, Field JavaDoc field, Object JavaDoc value) throws Exception JavaDoc {
88     //System.out.println("Setting field");
89
field.set(target, value);
90     }
91     
92     public Object JavaDoc getField(Object JavaDoc target, Field JavaDoc field) throws Exception JavaDoc {
93     System.out.println("getting");
94         return field.get(target);
95     }
96 }
97
Popular Tags