1 18 19 package alt.jiapi.interceptor; 20 21 import java.lang.reflect.Field ; 22 23 import java.util.ArrayList ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import org.apache.log4j.Category; 29 30 import alt.jiapi.InstrumentationContext; 31 import alt.jiapi.InstrumentationDescriptor; 32 import alt.jiapi.reflect.Signature; 33 34 import alt.jiapi.event.EventProducer; 35 36 58 public class FieldInterceptor2 extends EventProducer { 59 private FieldHandler handler; 60 private HashMap rfCache = new HashMap (); 61 62 69 public FieldInterceptor2(InstrumentationDescriptor id, 70 FieldHandler handler){ 71 this(id, "*", handler); 72 } 73 74 75 85 public FieldInterceptor2(InstrumentationDescriptor id, 86 String resolution, FieldHandler handler){ 87 this(id, new String [] { resolution }, handler); 88 } 89 90 91 100 public FieldInterceptor2(InstrumentationDescriptor id, 101 String [] resolutions, 102 FieldHandler handler) { 103 super(resolutions); 104 this.handler = handler; 105 106 id.addInstrumentor(new FieldInstrumentor(this, handler)); 107 } 108 109 110 111 121 public void setField(Object o, String name, Object value) throws Throwable { 122 try { 123 Field f = null;; 124 Class c = null; 125 126 if (o instanceof Class ) { c = (Class )o; 128 } 129 else { c = o.getClass(); 131 } 132 133 f = getReflectionField(c, name); 134 135 handler.setField(o, f, value); 136 } 137 catch(Throwable t) { 138 t.printStackTrace(); 139 throw t; 140 } 141 } 142 143 144 153 public Object getField(Object o, String name) throws Throwable { 154 try { 155 Field f = null; 156 Class c = null; 157 158 if (o instanceof Class ) { c = (Class )o; 160 } 161 else { c = o.getClass(); 163 } 164 165 f = getReflectionField(c, name); 166 167 Object r = handler.getField(o, f); 168 return r; 169 } 170 catch(Throwable t) { 171 t.printStackTrace(); 172 throw t; 173 } 174 } 175 176 177 private Field getReflectionField(Class c, String name) { 178 Field f = null; 179 180 f = (Field )rfCache.get(name); 183 if (f != null) { 184 return f; 185 } 186 187 String fName = name.substring(name.lastIndexOf('.') + 1); 188 try { 189 f = c.getDeclaredField(fName); 190 } 191 catch(Exception e) { 192 e.printStackTrace(); 193 } 194 195 rfCache.put(name, f); 196 197 return f; 198 } 199 } 200 201 | Popular Tags |