1 package test; 2 3 import java.lang.reflect.InvocationTargetException ; 4 import java.lang.reflect.Method ; 5 import java.util.StringTokenizer ; 6 7 import alt.jiapi.*; 8 import alt.jiapi.instrumentor.*; 9 import alt.jiapi.util.Bootstrapper; 10 import alt.jiapi.reflect.Loader; 11 12 30 public class InstrumentorTest implements Hook { 31 protected InstrumentationContext ctx; 32 protected String className; 33 34 { 35 className = System.getProperty("class"); 36 if (className == null) { 37 className = defaultClassName(); 38 } 39 } 40 41 protected InstrumentorTest() { 42 try { 43 ctx = new InstrumentationContext(); 44 } 46 catch (Exception e) { 47 e.printStackTrace(); 48 } 49 } 50 51 protected void run(String [] args) { 52 Bootstrapper.launch(className, args, ctx, getClass().getClassLoader()); 53 } 54 55 58 protected void addInclusionRules(InstrumentationDescriptor id, 59 String [] rules) throws JiapiException { 60 for (int i = 0; i < rules.length; i++) { 61 id.addInclusionRule(rules[i]); 62 } 63 } 64 67 protected void addExclusionRules(InstrumentationDescriptor id, 68 String [] rules) throws JiapiException { 69 for (int i = 0; i < rules.length; i++) { 70 id.addExclusionRule(rules[i]); 71 } 72 } 73 74 protected String defaultClassName() { 75 return "test.Foo"; 76 } 77 78 81 public String [] getInclusionRules() { 82 String inclusions = System.getProperty("include"); 83 return tokenizeRules(inclusions); 84 } 85 88 public String [] getExclusionRules() { 89 String exclusions = System.getProperty("exclude"); 90 exclusions += ":" + this.getClass().getName() + ":test.InstrumentorTest"; 91 return tokenizeRules(exclusions); 92 } 93 94 100 protected String [] tokenizeRules(String rules) { 101 String [] __rules = null; 102 if (rules != null) { 103 StringTokenizer st = new StringTokenizer (rules, ":"); 104 __rules = new String [st.countTokens()]; 105 106 for (int i = 0; i < __rules.length; i++) { 107 __rules[i] = st.nextToken(); 108 } 109 } 110 111 if (__rules == null) { 112 __rules = new String [0]; 113 } 114 115 return __rules; 116 } 117 118 122 public void hookMethod(Object _this, String s2) { 123 System.out.println ("Hook called : " + _this + ", " + s2); 124 } 125 126 127 132 public Method getHookMethod() { 133 Method hookMethod = null; 134 Class [] params = new Class [] {Object .class, String .class}; 135 136 try { 137 hookMethod = this.getClass().getMethod("hookMethod", params); 138 } 139 catch(Exception e) { 140 throw new RuntimeException (e.toString ()); 142 } 143 144 return hookMethod; 145 } 146 147 148 public Object getInstance() { 149 return this; 150 } 151 } 152 | Popular Tags |