package samples.event;
import alt.jiapi.InstrumentationContext;
import alt.jiapi.InstrumentationDescriptor;
import alt.jiapi.reflect.Loader;
import alt.jiapi.reflect.JiapiClass;
import alt.jiapi.event.FieldEvent;
import alt.jiapi.event.FieldEventProducer;
import alt.jiapi.event.FieldListener;
import alt.jiapi.util.Bootstrapper;
import alt.jiapi.util.InstrumentingClassLoader;
public class Sample2 implements FieldListener {
public static void main(String args[]) throws Exception {
new Sample2();
}
public Sample2() throws Exception {
// Configure:
InstrumentationContext ctx = new InstrumentationContext();
InstrumentationDescriptor id = new InstrumentationDescriptor();
id.addInclusionRule("samples.*");
ctx.addInstrumentationDescriptor(id);
// Use event API:
// Events for field access
FieldEventProducer fieldEventProducer = new FieldEventProducer(id);
fieldEventProducer.addFieldListener(this);
// JiapiClass jc = new Loader().loadClass("samples.event.Foo");
Bootstrapper.launch("samples.Foo", null, ctx,
InstrumentingClassLoader.createClassLoader(ctx));
}
// FieldListener:
public void fieldChanging(FieldEvent fe) {
System.out.println("Field " + fe.getFieldName() + " is changing");
}
public void fieldSet(FieldEvent fe) {
System.out.println("Field " + fe.getFieldName() + " is set");
}
public void fieldGet(FieldEvent fe) {
System.out.println("Field " + fe.getFieldName() + " has been get");
}
}