String meInclude = System.getProperty("method:include");
String meExclude = System.getProperty("method:exclude");
// Configure:
InstrumentationContext ctx = new InstrumentationContext();
InstrumentationDescriptor fid = new InstrumentationDescriptor();
InstrumentationDescriptor mid = new InstrumentationDescriptor();
// Add global inclusion and exclusion rules for both
// field and method descriptors
if (globalInclude != null) {
StringTokenizer st = new StringTokenizer(globalInclude, ",");
while(st.hasMoreTokens()) {
String rule = st.nextToken();
fid.addInclusionRule(rule);
mid.addInclusionRule(rule);
}
}
if (globalExclude != null) {
StringTokenizer st = new StringTokenizer(globalExclude, ",");
while(st.hasMoreTokens()) {
String rule = st.nextToken();
fid.addExclusionRule(rule);
mid.addExclusionRule(rule);
}
}
// Add event producer specific inclusion and exclusion rules
if (feInclude != null) {
StringTokenizer st = new StringTokenizer(feInclude, ",");
while(st.hasMoreTokens()) {
String rule = st.nextToken();
fid.addInclusionRule(rule);
}
}
if (feExclude != null) {
StringTokenizer st = new StringTokenizer(feExclude, ",");
while(st.hasMoreTokens()) {
String rule = st.nextToken();
fid.addExclusionRule(rule);
}
}
if (meInclude != null) {
StringTokenizer st = new StringTokenizer(meInclude, ",");
while(st.hasMoreTokens()) {
String rule = st.nextToken();
mid.addInclusionRule(rule);
}
}
if (meExclude != null) {
StringTokenizer st = new StringTokenizer(meExclude, ",");
while(st.hasMoreTokens()) {
String rule = st.nextToken();
mid.addExclusionRule(rule);
}
}
// Use event API:
// Events for field access
FieldEventProducer fieldEventProducer = new FieldEventProducer(fid);
fieldEventProducer.addFieldListener(this);
// Events for method access
MethodEventProducer methodEventProducer = new MethodEventProducer(mid);
methodEventProducer.addMethodListener(this);
// Add field and method descriptors to context
ctx.addInstrumentationDescriptor(fid);
ctx.addInstrumentationDescriptor(mid);
Class c = InstrumentingClassLoader.createClassLoader(ctx).loadClass(args[0]);
Method method = c.getMethod("main", new Class[] {String[].class});