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.InstrumentationDescriptor; 27 import alt.jiapi.instrumentor.ChainInstrumentor; 28 import alt.jiapi.instrumentor.InstrumentorChain; 29 30 import alt.jiapi.instrumentor.CatchInstrumentor; 31 import alt.jiapi.instrumentor.GrepInstrumentor; 32 import alt.jiapi.instrumentor.HeadInstrumentor; 33 import alt.jiapi.instrumentor.MethodCallInstrumentor; 34 import alt.jiapi.instrumentor.MethodDispatcherInstrumentor; 35 36 45 public class ExceptionEventProducer extends EventProducer { 46 private List listeners = new ArrayList (); 47 48 55 public ExceptionEventProducer(InstrumentationDescriptor id) { 56 this(id, "*"); 57 } 58 59 74 public ExceptionEventProducer(InstrumentationDescriptor id, String resolution) { 75 super(resolution); 76 77 try { 78 InstrumentorChain chain = new InstrumentorChain(); 79 ChainInstrumentor dispatcher = new MethodDispatcherInstrumentor(); 80 CatchInstrumentor selectCatchBlocks = new CatchInstrumentor(); 81 ChainInstrumentor selectHead = new HeadInstrumentor(); 82 ChainInstrumentor callMethod = 83 new MethodCallInstrumentor(new CatchHook(this)); 84 85 selectCatchBlocks.setResolutions(getResolutions()); 86 87 chain.add(dispatcher); 88 chain.add(selectCatchBlocks); 89 chain.add(selectHead); 90 chain.add(callMethod); 91 92 id.addInstrumentor(chain); 93 } catch (Exception e) { 94 e.printStackTrace(); 96 } 97 } 98 99 100 104 public synchronized void addExceptionListener(ExceptionListener fl) { 105 listeners.add(fl); 106 } 107 108 112 public synchronized void removeExceptionListener(ExceptionListener fl) { 113 listeners.remove(fl); 114 } 115 116 120 public void catchTrapped(Object sourceObject, String exceptionName) { 121 catchTrapped(sourceObject, exceptionName, null); 122 } 123 124 128 public void catchTrapped(Object sourceObject, String exceptionName, 129 Object target) { 130 if (!isProtected(sourceObject)) { 131 fireCatchEvent(sourceObject, exceptionName, target); 132 } 133 } 134 135 140 protected synchronized void fireCatchEvent(Object sourceObject, 141 String exceptionName, 142 Object target) { 143 Iterator i = listeners.iterator(); 144 ExceptionEvent event = 145 new ExceptionEvent(this, sourceObject, exceptionName, 146 target, 147 ExceptionEvent.CATCH_TRAPPED); 148 149 while (i.hasNext()) { 150 ExceptionListener el = (ExceptionListener)i.next(); 151 el.exceptionCaught(event); 152 } 153 } 154 } 155 156 | Popular Tags |