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.event.MethodEvent;
import alt.jiapi.event.MethodEventProducer;
import alt.jiapi.event.MethodListener;
import alt.jiapi.util.Bootstrapper;
import alt.jiapi.util.InstrumentingClassLoader;
public class Sample3 implements FieldListener, MethodListener {
public static void main(String args[]) throws Exception {
new Sample3();
}
public Sample3() throws Exception {
// Configure:
InstrumentationContext ctx = new InstrumentationContext();
InstrumentationDescriptor id = new InstrumentationDescriptor();
id.addInclusionRule("samples.event.*");
ctx.addInstrumentationDescriptor(id);
// Use event API:
// Events for field access
FieldEventProducer fieldEventProducer = new FieldEventProducer(id);
fieldEventProducer.addFieldListener(this);
// Events for method access
MethodEventProducer methodEventProducer = new MethodEventProducer(id);
methodEventProducer.addMethodListener(this);
// JiapiClass jc = new Loader().loadClass("samples.event.Foo");
Bootstrapper.launch("samples.event.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");
}
// MethodListener
public void methodEntered(MethodEvent event) {
System.out.println("Method " + event.getClassName() + "." +
event.getMethodName() + " entered.");
}
public void methodExited(MethodEvent event) {
System.out.println("Method " + event.getClassName() + "." +
event.getMethodName() + " exited.");
}
}