1 11 package org.eclipse.jdt.internal.debug.ui.actions; 12 13 14 import com.ibm.icu.text.MessageFormat; 15 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.debug.core.DebugEvent; 18 import org.eclipse.debug.core.DebugException; 19 import org.eclipse.debug.core.DebugPlugin; 20 import org.eclipse.debug.core.IDebugEventFilter; 21 import org.eclipse.jdt.core.IMethod; 22 import org.eclipse.jdt.core.JavaModelException; 23 import org.eclipse.jdt.core.Signature; 24 import org.eclipse.jdt.debug.core.IJavaDebugTarget; 25 import org.eclipse.jdt.debug.core.IJavaStackFrame; 26 import org.eclipse.jdt.debug.core.IJavaThread; 27 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 28 import org.eclipse.jface.dialogs.MessageDialog; 29 30 33 public class StepIntoSelectionHandler implements IDebugEventFilter { 34 35 38 private IMethod fMethod; 39 40 43 private String fResolvedSignature; 44 45 48 private IJavaThread fThread; 49 50 53 private String fOriginalName; 54 private String fOriginalSignature; 55 private String fOriginalTypeName; 56 private int fOriginalStackDepth; 57 58 61 private boolean fFirstStep = true; 62 63 66 private boolean fStepFilterEnabledState; 67 68 71 private int fExpectedKind = -1; 72 73 76 private int fExpectedDetail = -1; 77 78 82 public StepIntoSelectionHandler(IJavaThread thread, IJavaStackFrame frame, IMethod method) { 83 fMethod = method; 84 fThread = thread; 85 try { 86 fOriginalName = frame.getName(); 87 fOriginalSignature = frame.getSignature(); 88 fOriginalTypeName = frame.getDeclaringTypeName(); 89 if (method.isBinary()) { 90 fResolvedSignature = method.getSignature(); 91 } else { 92 fResolvedSignature = ToggleBreakpointAdapter.resolveMethodSignature(method.getDeclaringType(), method.getSignature()); 93 } 94 } catch (CoreException e) { 95 JDIDebugUIPlugin.log(e); 96 } 97 } 98 99 104 protected IJavaThread getThread() { 105 return fThread; 106 } 107 108 protected IJavaDebugTarget getDebugTarget() { 109 return (IJavaDebugTarget)getThread().getDebugTarget(); 110 } 111 112 117 protected IMethod getMethod() { 118 return fMethod; 119 } 120 121 126 protected String getSignature() { 127 return fResolvedSignature; 128 } 129 130 133 public DebugEvent[] filterDebugEvents(DebugEvent[] events) { 134 DebugEvent event = null; 136 int index = -1; 137 int threadEvents = 0; 138 for (int i = 0; i < events.length; i++) { 139 DebugEvent e = events[i]; 140 if (isExpectedEvent(e)) { 141 event = e; 142 index = i; 143 threadEvents++; 144 } else if (e.getSource() == getThread()) { 145 threadEvents++; 146 } 147 } 148 149 if (event == null) { 150 return events; 152 } 153 154 DebugEvent[] filtered = new DebugEvent[events.length - 1]; 156 if (filtered.length > 0) { 157 int j = 0; 158 for (int i = 0; i < events.length; i++) { 159 if (i != index) { 160 filtered[j] = events[i]; 161 j++; 162 } 163 } 164 } 165 166 if (threadEvents > 1) { 168 cleanup(); 169 return filtered; 170 } 171 172 switch (event.getKind()) { 174 case DebugEvent.RESUME: 175 setExpectedEvent(DebugEvent.SUSPEND, DebugEvent.STEP_END); 177 if (fFirstStep) { 178 fFirstStep = false; 179 return events; } 181 return filtered; 183 case DebugEvent.SUSPEND: 184 try { 186 final IJavaStackFrame frame = (IJavaStackFrame)getThread().getTopStackFrame(); 187 int stackDepth = frame.getThread().getStackFrames().length; 188 String name = null; 189 if (frame.isConstructor()) { 190 name = frame.getDeclaringTypeName(); 191 index = name.lastIndexOf('.'); 192 if (index >= 0) { 193 name = name.substring(index + 1); 194 } 195 } else { 196 name = frame.getName(); 197 } 198 if (name.equals(getMethod().getElementName()) && frame.getSignature().equals(getSignature())) { 199 cleanup(); 201 return events; 202 } 203 Runnable r = null; 205 if (stackDepth > fOriginalStackDepth) { 206 if (frame.isSynthetic()) { 207 r = new Runnable () { 209 public void run() { 210 try { 211 setExpectedEvent(DebugEvent.RESUME, DebugEvent.STEP_INTO); 212 frame.stepInto(); 213 } catch (DebugException e) { 214 JDIDebugUIPlugin.log(e); 215 cleanup(); 216 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[]{new DebugEvent(getDebugTarget(), DebugEvent.CHANGE)}); 217 } 218 } 219 }; 220 } else { 221 r = new Runnable () { 222 public void run() { 223 try { 224 setExpectedEvent(DebugEvent.RESUME, DebugEvent.STEP_RETURN); 225 frame.stepReturn(); 226 } catch (DebugException e) { 227 JDIDebugUIPlugin.log(e); 228 cleanup(); 229 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[]{new DebugEvent(getDebugTarget(), DebugEvent.CHANGE)}); 230 } 231 } 232 }; 233 } 234 } else if (stackDepth == fOriginalStackDepth){ 235 if (!(frame.getSignature().equals(fOriginalSignature) && frame.getName().equals(fOriginalName) && frame.getDeclaringTypeName().equals(fOriginalTypeName))) { 237 missed(); 238 return events; 239 } 240 r = new Runnable () { 241 public void run() { 242 try { 243 setExpectedEvent(DebugEvent.RESUME, DebugEvent.STEP_INTO); 244 frame.stepInto(); 245 } catch (DebugException e) { 246 JDIDebugUIPlugin.log(e); 247 cleanup(); 248 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[]{new DebugEvent(getDebugTarget(), DebugEvent.CHANGE)}); 249 } 250 } 251 }; 252 } else { 253 missed(); 255 return events; 256 } 257 DebugPlugin.getDefault().asyncExec(r); 258 return filtered; 260 } catch (CoreException e) { 261 JDIDebugUIPlugin.log(e); 263 cleanup(); 264 return events; 265 } 266 } 267 return events; 269 270 } 271 272 275 protected void missed() { 276 cleanup(); 277 Runnable r = new Runnable () { 278 public void run() { 279 String methodName = null; 280 try { 281 methodName = Signature.toString(getMethod().getSignature(), getMethod().getElementName(), getMethod().getParameterNames(), false, false); 282 } catch (JavaModelException e) { 283 methodName = getMethod().getElementName(); 284 } 285 new MessageDialog(JDIDebugUIPlugin.getActiveWorkbenchShell(), ActionMessages.StepIntoSelectionHandler_1, null, MessageFormat.format(ActionMessages.StepIntoSelectionHandler_Execution_did_not_enter____0____before_the_current_method_returned__1, new String []{methodName}), MessageDialog.INFORMATION, new String [] {ActionMessages.StepIntoSelectionHandler_2}, 0).open(); 286 } 287 }; 288 JDIDebugUIPlugin.getStandardDisplay().asyncExec(r); 289 } 290 291 294 public void step() { 295 DebugPlugin.getDefault().addDebugEventFilter(this); 297 fStepFilterEnabledState = getDebugTarget().isStepFiltersEnabled(); 298 getDebugTarget().setStepFiltersEnabled(false); 299 try { 300 fOriginalStackDepth = getThread().getStackFrames().length; 301 setExpectedEvent(DebugEvent.RESUME, DebugEvent.STEP_INTO); 302 getThread().stepInto(); 303 } catch (DebugException e) { 304 JDIDebugUIPlugin.log(e); 305 cleanup(); 306 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[]{new DebugEvent(getDebugTarget(), DebugEvent.CHANGE)}); 307 } 308 } 309 310 313 protected void cleanup() { 314 DebugPlugin.getDefault().removeDebugEventFilter(this); 315 getDebugTarget().setStepFiltersEnabled(fStepFilterEnabledState); 317 } 318 319 325 private void setExpectedEvent(int kind, int detail) { 326 fExpectedKind = kind; 327 fExpectedDetail = detail; 328 } 329 330 336 protected boolean isExpectedEvent(DebugEvent event) { 337 return event.getSource().equals(getThread()) && 338 event.getKind() == fExpectedKind && 339 event.getDetail() == fExpectedDetail; 340 } 341 } 342 | Popular Tags |