KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > event > Sample2


1 package samples.event;
2
3 import alt.jiapi.InstrumentationContext;
4 import alt.jiapi.InstrumentationDescriptor;
5 import alt.jiapi.reflect.Loader;
6 import alt.jiapi.reflect.JiapiClass;
7
8 import alt.jiapi.event.FieldEvent;
9 import alt.jiapi.event.FieldEventProducer;
10 import alt.jiapi.event.FieldListener;
11
12 import alt.jiapi.util.Bootstrapper;
13 import alt.jiapi.util.InstrumentingClassLoader;
14
15 public class Sample2 implements FieldListener {
16     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
17         new Sample2();
18     }
19     
20     public Sample2() throws Exception JavaDoc {
21         // Configure:
22
InstrumentationContext ctx = new InstrumentationContext();
23         InstrumentationDescriptor id = new InstrumentationDescriptor();
24         id.addInclusionRule("samples.*");
25         ctx.addInstrumentationDescriptor(id);
26         
27         // Use event API:
28
// Events for field access
29
FieldEventProducer fieldEventProducer = new FieldEventProducer(id);
30         fieldEventProducer.addFieldListener(this);
31
32         // JiapiClass jc = new Loader().loadClass("samples.event.Foo");
33

34         Bootstrapper.launch("samples.Foo", null, ctx,
35                             InstrumentingClassLoader.createClassLoader(ctx));
36     }
37        
38
39     // FieldListener:
40
public void fieldChanging(FieldEvent fe) {
41         System.out.println("Field " + fe.getFieldName() + " is changing");
42     }
43     public void fieldSet(FieldEvent fe) {
44         System.out.println("Field " + fe.getFieldName() + " is set");
45     }
46
47     public void fieldGet(FieldEvent fe) {
48         System.out.println("Field " + fe.getFieldName() + " has been get");
49     }
50 }
51
Popular Tags