1 22 23 package org.aspectj.debugger.request; 24 25 import org.aspectj.debugger.base.*; 26 27 import com.sun.jdi.*; 28 import com.sun.jdi.request.*; 29 import com.sun.jdi.event.*; 30 import java.util.*; 31 32 40 41 public abstract class ThreadRequestAction extends RequestActionSupport implements ThreadListener { 42 43 protected String threadName; 44 45 public ThreadRequestAction(Debugger debugger, String threadName) { 46 super(debugger); 47 this.threadName = threadName; 48 } 49 50 void resolveAll() { 51 resolveAgainstAllThreads(); 52 } 53 54 protected void addListeners() { 55 super.addListeners(); 56 debugger.addThreadListener(this); 57 } 58 59 protected void removeListeners() { 60 super.removeListeners(); 61 debugger.removeThreadListener(this); 62 } 63 64 abstract boolean isEnter(); 65 66 protected EventRequest resolve(ThreadReference threadRef) { 67 try { 68 if (isSame(threadRef)) { 69 EventRequestManager em = vm().eventRequestManager(); 70 if (isEnter()) { 71 MethodEntryRequest entry = em.createMethodEntryRequest(); 72 entry.addThreadFilter(threadRef); 73 entry.setSuspendPolicy(EventRequest.SUSPEND_NONE); entry.enable(); 75 debugger.addExcludesTo(entry); 76 return entry; 77 } else { 78 MethodExitRequest exit = em.createMethodExitRequest(); 79 exit.addThreadFilter(threadRef); 80 exit.setSuspendPolicy(EventRequest.SUSPEND_NONE); exit.enable(); 82 debugger.addExcludesTo(exit); 83 return exit; 84 } 85 } 86 } catch (NoVMException e) { 87 } 88 return null; 89 } 90 91 protected boolean isSame(ThreadReference threadRef) { 92 if (threadRef.name().equals(threadName)) { 93 return true; 94 } else { 95 try { 96 long ourID = Long.parseLong(threadName); 97 long refID = threadRef.uniqueID(); 98 if (ourID == refID) { 99 return true; 100 } 101 } catch (NumberFormatException e) { 102 } 103 } 104 return false; 105 } 106 107 protected boolean resolveAgainstAllThreads() { 108 try { 109 Iterator iter = debugger.getVM().allThreads().iterator(); 110 while (iter.hasNext()) { 111 if (setRequest(resolve((ThreadReference) iter.next())) != null) { 112 return success(); 113 } 114 } 115 } catch (NoVMException e) { 116 } 117 return failure(true); 118 } 119 120 protected boolean resolveAgainstThread(ThreadReference threadRef) { 121 if (setRequest(resolve(threadRef)) != null) { 122 return success(); 123 } 124 return failure(false); 125 } 126 127 128 public void threadStartEvent(ThreadStartEvent e) { 129 if (!isSet()) { 130 resolve(e.thread()); 131 } 132 } 133 public void threadDeathEvent(ThreadDeathEvent e) { 134 } 135 } 136 | Popular Tags |