1 18 19 package alt.jiapi.instrumentor; 20 21 import java.lang.reflect.Modifier ; 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.NoSuchElementException ; 26 27 import org.apache.log4j.Category; 28 29 import alt.jiapi.reflect.InstructionList; 30 import alt.jiapi.reflect.JiapiClass; 31 import alt.jiapi.reflect.JiapiMethod; 32 33 import alt.jiapi.Instrumentor; 34 41 public class InstrumentorChain implements Instrumentor { 42 private Instrumentation instrumentation = new Instrumentation(); 43 private static Category log = alt.jiapi.Runtime.getLogCategory(InstrumentorChain.class); 44 45 private JiapiClass currentClass; 46 47 50 private List instrumentors = new ArrayList (); 51 52 public InstrumentorChain() { 53 } 54 55 59 public void add(ChainInstrumentor instrumentor) { 60 if (instrumentors.size() > 0) { 61 AbstractInstrumentor parent = 62 (AbstractInstrumentor)instrumentors.get(instrumentors.size() - 1); 63 parent.setChild((AbstractInstrumentor)instrumentor); 64 } 65 66 instrumentors.add(instrumentor); 67 } 68 69 public JiapiClass getCurrentClass() { 70 return currentClass; 71 } 72 73 76 public void instrument(JiapiClass clazz) { 77 currentClass = clazz; 78 JiapiMethod[] methods = clazz.getDeclaredMethods(); 81 82 AbstractInstrumentor instr = 83 (AbstractInstrumentor)instrumentors.get(0); 84 85 log.info("Bootstrapping instrumentation of " + 86 clazz.getName() + " to " + this); 87 88 instr.setCurrentClass(clazz); 89 instr.instrument(null, instrumentation); 90 91 104 105 log.info("Instrumentation of " + 106 clazz.getName() + " is finished with " + this); 107 } 108 109 110 public String toString() { 111 StringBuffer sb = new StringBuffer ("["); 112 113 Iterator iter = instrumentors.iterator(); 114 while(iter.hasNext()) { 115 ChainInstrumentor i = (ChainInstrumentor)iter.next(); 116 sb.append(i.toString()); 117 118 if (iter.hasNext()) { 119 sb.append(", "); 120 } 121 } 122 123 sb.append(']'); 124 125 return sb.toString(); 126 } 127 128 129 Instrumentation getInstrumentation() { 131 return instrumentation; 132 } 133 } 134 | Popular Tags |