1 package alt.jiapi.tool; 2 3 import java.lang.reflect.Method ; 4 import java.lang.reflect.InvocationTargetException ; 5 import java.util.StringTokenizer ; 6 7 import alt.jiapi.InstrumentationContext; 8 import alt.jiapi.InstrumentationDescriptor; 9 import alt.jiapi.JiapiException; 10 import alt.jiapi.reflect.Loader; 11 import alt.jiapi.reflect.JiapiClass; 12 13 import alt.jiapi.event.FieldEvent; 14 import alt.jiapi.event.FieldEventProducer; 15 import alt.jiapi.event.FieldListener; 16 17 import alt.jiapi.event.MethodEvent; 18 import alt.jiapi.event.MethodEventProducer; 19 import alt.jiapi.event.MethodListener; 20 21 import alt.jiapi.util.Bootstrapper; 22 import alt.jiapi.util.InstrumentingClassLoader; 23 24 29 public class EventTool implements FieldListener, MethodListener { 30 public static void main(String [] args) throws Exception { 31 try { 32 new EventTool(args); 33 } 34 catch(ClassNotFoundException cnfe) { 35 System.out.println(cnfe); 36 } 37 catch(NoSuchMethodException nsme) { 38 System.out.println("No 'public static void main(String[])' method found in " + args[0]); 39 } 40 } 41 42 public EventTool(String [] args) throws JiapiException, ClassNotFoundException , NoSuchMethodException , IllegalAccessException , InvocationTargetException { 43 String globalInclude = System.getProperty("include"); 44 String globalExclude = System.getProperty("exclude"); 45 46 String feInclude = System.getProperty("field:include"); 47 String feExclude = System.getProperty("field:exclude"); 48 49 String meInclude = System.getProperty("method:include"); 50 String meExclude = System.getProperty("method:exclude"); 51 52 InstrumentationContext ctx = new InstrumentationContext(); 54 InstrumentationDescriptor fid = new InstrumentationDescriptor(); 55 InstrumentationDescriptor mid = new InstrumentationDescriptor(); 56 57 if (globalInclude != null) { 60 StringTokenizer st = new StringTokenizer (globalInclude, ","); 61 while(st.hasMoreTokens()) { 62 String rule = st.nextToken(); 63 fid.addInclusionRule(rule); 64 mid.addInclusionRule(rule); 65 } 66 } 67 if (globalExclude != null) { 68 StringTokenizer st = new StringTokenizer (globalExclude, ","); 69 while(st.hasMoreTokens()) { 70 String rule = st.nextToken(); 71 fid.addExclusionRule(rule); 72 mid.addExclusionRule(rule); 73 } 74 } 75 76 if (feInclude != null) { 78 StringTokenizer st = new StringTokenizer (feInclude, ","); 79 while(st.hasMoreTokens()) { 80 String rule = st.nextToken(); 81 fid.addInclusionRule(rule); 82 } 83 } 84 if (feExclude != null) { 85 StringTokenizer st = new StringTokenizer (feExclude, ","); 86 while(st.hasMoreTokens()) { 87 String rule = st.nextToken(); 88 fid.addExclusionRule(rule); 89 } 90 } 91 92 if (meInclude != null) { 93 StringTokenizer st = new StringTokenizer (meInclude, ","); 94 while(st.hasMoreTokens()) { 95 String rule = st.nextToken(); 96 mid.addInclusionRule(rule); 97 } 98 } 99 if (meExclude != null) { 100 StringTokenizer st = new StringTokenizer (meExclude, ","); 101 while(st.hasMoreTokens()) { 102 String rule = st.nextToken(); 103 mid.addExclusionRule(rule); 104 } 105 } 106 107 108 FieldEventProducer fieldEventProducer = new FieldEventProducer(fid); 111 fieldEventProducer.addFieldListener(this); 112 113 MethodEventProducer methodEventProducer = new MethodEventProducer(mid); 115 methodEventProducer.addMethodListener(this); 116 117 ctx.addInstrumentationDescriptor(fid); 119 ctx.addInstrumentationDescriptor(mid); 120 121 122 Class c = InstrumentingClassLoader.createClassLoader(ctx).loadClass(args[0]); 123 Method method = c.getMethod("main", new Class [] {String [].class}); 124 125 String [] __args = new String [args.length - 1]; 126 for (int i = 0; i < __args.length; i++) { 127 __args[i] = args[i+1]; 128 } 129 130 method.invoke(c, new Object [] { __args }); 131 } 132 133 134 135 public void fieldChanging(FieldEvent fe) { 137 System.out.println("Field " + fe.getFieldName() + " is changing"); 138 } 139 public void fieldSet(FieldEvent fe) { 140 System.out.println("Field " + fe.getFieldName() + " is set"); 141 } 142 143 public void fieldGet(FieldEvent fe) { 144 System.out.println("Field " + fe.getFieldName() + " has been get"); 145 } 146 147 public void methodEntered(MethodEvent event) { 149 System.out.println("Method " + event.getClassName() + "." + 150 event.getMethodName() + " entered."); 151 } 152 153 public void methodExited(MethodEvent event) { 154 System.out.println("Method " + event.getClassName() + "." + 155 event.getMethodName() + " exited."); 156 } 157 } 158 | Popular Tags |