package Sequenic.T2ext.Instrumenter; import org.apache.bcel.* ; import org.apache.bcel.generic.*; import org.apache.bcel.classfile.* ; /** * A simple example of how to use BCEL to inject 'sensors' for instrumentation. * Look at the code in main. * * @author Wishnu Prasetya * */ public class InjectionSimpleExample { /** * Our target class... */ Class C ; /** * BCEL representation of a target class C; it allows a class file to * be analyzed, but not transformed. */ JavaClass clazz ; /** * BCEL interface to manipulate a class. */ ClassGen classGen ; /** * Our target method ... * Note that this is bcel.classfle.Method; not java.lang.Reflect.Method */ Method m = null ; /** * A method generator; this will hold the new version of m: */ MethodGen mGen = null ; /** * Constructor; will set-up some stuffs before injecting into the target * method. */ public InjectionSimpleExample(Class target, String methodName){ C = target ; // Load-up the class: try { clazz = Repository.lookupClass(C); } catch (Exception e) { throw new Error(e) ; } // Set-up the class-gen to allow manipulation on the class: classGen = new ClassGen(clazz) ; // Ok, now we can start injecting; but first we need to get the 'method' // whose name you specified above. Unfortunately is a bit complicated; // the code below is not nice, but will do: Method[] methods = clazz.getMethods() ; for (int i=0; i