Bytecode Instrumentation

| 2004 JavaOne
SM
Conference | Session 3042 Copyright Hewlett Packard Co., 2004.
60
MyTransformer.mytransform()
public public static byte[] mytransform(String classToInst,
String methodToInst) {
try {
JavaClass java_class = Repository.lookupClass(classToInst);
// Add method to CP we will call from the instrumented method
ConstantPool constants = java_class.getConstantPool();
cp = new ConstantPoolGen(constants);
monitoringMethodToCall = cp.addMethodref("MyMon",
"countMethodEntry",
"(II)V");
// Select the method to instrument and find the index
Method[] methods = java_class.getMethods();
methods[j] = traceMethod(methods[j], methodIndex);
// Set the constant pool
java_class.setConstantPool(cp.getFinalConstantPool());
} catch( Exception e) { e.printStackTrace();}
return java_class.getBytes();
}