1 18 19 package alt.jiapi.event; 20 21 import java.lang.reflect.Method ; 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import alt.jiapi.InstrumentationContext; 27 import alt.jiapi.InstrumentationDescriptor; 28 29 import alt.jiapi.instrumentor.ChainInstrumentor; 30 import alt.jiapi.instrumentor.InstrumentorChain; 31 32 import alt.jiapi.instrumentor.FieldAccessStrategy; 33 import alt.jiapi.instrumentor.HeadInstrumentor; 34 import alt.jiapi.instrumentor.Hook; 35 import alt.jiapi.instrumentor.MethodDispatcherInstrumentor; 36 import alt.jiapi.instrumentor.MethodCallInstrumentor; 37 import alt.jiapi.instrumentor.GrepInstrumentor; 38 import alt.jiapi.instrumentor.TailInstrumentor; 39 40 49 public class FieldEventProducer extends EventProducer { 50 private List listeners = new ArrayList (); 51 52 53 60 public FieldEventProducer(InstrumentationDescriptor id) { 61 this(id, "*"); 62 } 63 64 73 public FieldEventProducer(InstrumentationDescriptor id, String resolution) { 74 super(resolution); 75 id.addInstrumentor(new FieldEventInstrumentor(this)); 76 77 if (true) { 78 return; 79 } 80 81 try { 82 ChainInstrumentor setDispatcher = new MethodDispatcherInstrumentor(); 84 85 GrepInstrumentor grepWriteAccess = 86 new GrepInstrumentor(new FieldAccessStrategy("*__jiapi_field*", true, FieldAccessStrategy.WRITE_ACCESS)); 89 90 grepWriteAccess.setResolutions(getResolutions()); 91 92 ChainInstrumentor afterSet = new TailInstrumentor(); 93 ChainInstrumentor callFieldSet = 94 new MethodCallInstrumentor(new FieldSetHook(this)); 95 96 InstrumentorChain setChain = new InstrumentorChain(); 97 setChain.add(setDispatcher); 98 setChain.add(grepWriteAccess); 99 setChain.add(afterSet); 100 setChain.add(callFieldSet); 101 102 103 ChainInstrumentor getDispatcher = new MethodDispatcherInstrumentor(); 104 GrepInstrumentor grepReadAccess = 105 new GrepInstrumentor(new FieldAccessStrategy("*__jiapi_field*", true, FieldAccessStrategy.READ_ACCESS)); 108 109 grepReadAccess.setResolutions(getResolutions()); 110 111 ChainInstrumentor beforeRead = new HeadInstrumentor(); 112 ChainInstrumentor callFieldGet = 113 new MethodCallInstrumentor(new FieldGetHook(this)); 114 115 InstrumentorChain getChain = new InstrumentorChain(); 116 getChain.add(getDispatcher); 117 getChain.add(grepReadAccess); 118 getChain.add(beforeRead); 119 getChain.add(callFieldGet); 120 121 122 id.addInstrumentor(getChain); 123 id.addInstrumentor(setChain); 124 } catch (Exception e) { 125 e.printStackTrace(); 127 } 128 } 129 130 131 135 public synchronized void addFieldListener(FieldListener fl) { 136 listeners.add(fl); 137 } 138 139 143 public synchronized void removeFieldListener(FieldListener fl) { 144 listeners.remove(fl); 145 } 146 147 151 public void fieldGet(Object sourceObject, String fieldName) { 152 if (!isProtected(sourceObject)) { 153 fireFieldGetEvent(sourceObject, fieldName); 154 } 155 } 156 157 161 public void fieldSet(Object sourceObject, String fieldName) { 162 if (!isProtected(sourceObject)) { 163 fireFieldSetEvent(sourceObject, fieldName); 164 } 165 } 166 167 173 protected synchronized void fireFieldSetEvent(Object sourceObject, 174 String fieldName) { 175 Iterator i = listeners.iterator(); 176 FieldEvent event = new FieldEvent(this, sourceObject, fieldName, 177 FieldEvent.FIELD_SET); 178 179 while (i.hasNext()) { 180 FieldListener fl = (FieldListener)i.next(); 181 fl.fieldSet(event); 182 } 183 } 184 185 191 protected synchronized void fireFieldGetEvent(Object sourceObject, 192 String fieldName) { 193 Iterator i = listeners.iterator(); 194 FieldEvent event = new FieldEvent(this, sourceObject, fieldName, 195 FieldEvent.FIELD_GET); 196 197 while (i.hasNext()) { 198 FieldListener fl = (FieldListener)i.next(); 199 fl.fieldGet(event); 200 } 201 } 202 } 203 204 | Popular Tags |