1 package de.loskutov.bco.editors; 2 3 import java.text.MessageFormat ; 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 8 9 import org.eclipse.core.resources.IResource; 10 import org.eclipse.core.resources.ResourcesPlugin; 11 import org.eclipse.core.runtime.CoreException; 12 import org.eclipse.core.runtime.IAdaptable; 13 import org.eclipse.core.runtime.IProgressMonitor; 14 import org.eclipse.core.runtime.IStatus; 15 import org.eclipse.core.runtime.Status; 16 import org.eclipse.core.runtime.jobs.Job; 17 import org.eclipse.debug.core.DebugPlugin; 18 import org.eclipse.debug.core.IBreakpointManager; 19 import org.eclipse.debug.core.model.IBreakpoint; 20 import org.eclipse.jdt.core.Flags; 21 import org.eclipse.jdt.core.IClassFile; 22 import org.eclipse.jdt.core.IField; 23 import org.eclipse.jdt.core.IMember; 24 import org.eclipse.jdt.core.IMethod; 25 import org.eclipse.jdt.core.ISourceRange; 26 import org.eclipse.jdt.core.IType; 27 import org.eclipse.jdt.core.JavaModelException; 28 import org.eclipse.jdt.core.dom.CompilationUnit; 29 import org.eclipse.jdt.debug.core.IJavaBreakpoint; 30 import org.eclipse.jdt.debug.core.IJavaFieldVariable; 31 import org.eclipse.jdt.debug.core.IJavaLineBreakpoint; 32 import org.eclipse.jdt.debug.core.IJavaMethodBreakpoint; 33 import org.eclipse.jdt.debug.core.IJavaWatchpoint; 34 import org.eclipse.jdt.debug.core.JDIDebugModel; 35 import org.eclipse.jdt.internal.debug.core.JavaDebugUtils; 36 import org.eclipse.jdt.internal.debug.ui.BreakpointUtils; 37 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 38 import org.eclipse.jdt.internal.debug.ui.actions.ActionMessages; 39 import org.eclipse.jdt.internal.debug.ui.actions.BreakpointFieldLocator; 40 import org.eclipse.jdt.internal.debug.ui.actions.BreakpointLocationVerifierJob; 41 import org.eclipse.jdt.internal.debug.ui.actions.BreakpointMethodLocator; 42 import org.eclipse.jdt.internal.debug.ui.actions.ToggleBreakpointAdapter; 43 import org.eclipse.jface.action.IStatusLineManager; 44 import org.eclipse.jface.text.BadLocationException; 45 import org.eclipse.jface.text.IDocument; 46 import org.eclipse.jface.text.IRegion; 47 import org.eclipse.jface.text.ITextSelection; 48 import org.eclipse.jface.viewers.ISelection; 49 import org.eclipse.jface.viewers.IStructuredSelection; 50 import org.eclipse.swt.widgets.Display; 51 import org.eclipse.ui.IEditorInput; 52 import org.eclipse.ui.IEditorPart; 53 import org.eclipse.ui.IWorkbenchPart; 54 import org.eclipse.ui.texteditor.IDocumentProvider; 55 import org.eclipse.ui.texteditor.ITextEditor; 56 57 58 64 public class BytecodeBreakpointAdapter extends ToggleBreakpointAdapter { 65 66 public boolean canToggleBreakpoints(IWorkbenchPart part, 67 ISelection selection) { 68 return super.canToggleBreakpoints(part, selection); 70 } 71 72 public void toggleBreakpoints(IWorkbenchPart part, ISelection selection) 73 throws CoreException { 74 super.toggleBreakpoints(part, selection); 76 } 77 78 public boolean canToggleLineBreakpoints(IWorkbenchPart part, 79 ISelection selection) { 80 return super.canToggleBreakpoints(part, selection); 82 } 83 84 public boolean canToggleMethodBreakpoints(IWorkbenchPart part, 85 ISelection selection) { 86 return super.canToggleMethodBreakpoints(part, selection); 88 } 89 90 public boolean canToggleWatchpoints(IWorkbenchPart part, 91 ISelection selection) { 92 return super.canToggleWatchpoints(part, selection); 94 } 95 96 public void toggleLineBreakpoints(IWorkbenchPart part, ISelection selection) 97 throws CoreException { 98 super.toggleLineBreakpoints(part, selection); 100 } 101 102 public void toggleMethodBreakpoints(final IWorkbenchPart part, 103 final ISelection finalSelection) { 104 Job job = new Job("Toggle Method Breakpoints") { protected IStatus run(IProgressMonitor monitor) { 106 if (monitor.isCanceled()) { 107 return Status.CANCEL_STATUS; 108 } 109 if(isInterface(finalSelection)) { 110 report(ActionMessages.ToggleBreakpointAdapter_7, part); 111 return Status.OK_STATUS; 112 } 113 try { 114 report(null, part); 115 ISelection selection = finalSelection; 116 selection = translateToMembers(part, selection); 117 ITextEditor textEditor = getTextEditor(part); 118 if (textEditor != null && selection instanceof ITextSelection) { 119 ITextSelection textSelection = (ITextSelection) selection; 120 CompilationUnit compilationUnit = parseCompilationUnit(textEditor); 121 if (compilationUnit != null) { 122 BreakpointMethodLocator locator = new BreakpointMethodLocator(textSelection.getOffset()); 123 compilationUnit.accept(locator); 124 String methodName = locator.getMethodName(); 125 if (methodName == null) { 126 report(ActionMessages.ManageMethodBreakpointActionDelegate_CantAdd, part); 127 return Status.OK_STATUS; 128 } 129 String typeName = locator.getTypeName(); 130 String methodSignature = locator.getMethodSignature(); 131 if (methodSignature == null) { 132 report(ActionMessages.ManageMethodBreakpointActionDelegate_methodNonAvailable, part); 133 return Status.OK_STATUS; 134 } 135 IJavaMethodBreakpoint existing = getMethodBreakpoint(typeName, methodName, methodSignature); 138 if (existing == null) { 139 createMethodBreakpoint(getResource((IEditorPart) part), typeName, methodName, methodSignature, true, false, false, -1, -1, -1, 0, true, new HashMap (10)); 140 } else { 141 removeBreakpoint(existing, true); 142 } 143 } 144 } else if (selection instanceof IStructuredSelection) { 145 IMethod[] members = getMethods((IStructuredSelection) selection); 146 if (members.length == 0) { 147 report(ActionMessages.ToggleBreakpointAdapter_9, part); 148 return Status.OK_STATUS; 149 } 150 for (int i = 0, length = members.length; i < length; i++) { 151 IMethod method = members[i]; 152 IJavaBreakpoint breakpoint = getMethodBreakpoint(method); 153 if (breakpoint == null) { 154 int start = -1; 156 int end = -1; 157 ISourceRange range = method.getNameRange(); 158 if (range != null) { 159 start = range.getOffset(); 160 end = start + range.getLength(); 161 } 162 Map attributes = new HashMap (10); 163 BreakpointUtils.addJavaBreakpointAttributes(attributes, method); 164 IType type = method.getDeclaringType(); 165 String methodSignature = method.getSignature(); 166 String methodName = method.getElementName(); 167 if (method.isConstructor()) { 168 methodName = "<init>"; if (type.isEnum()) { 170 methodSignature = "(Ljava.lang.String;I" + methodSignature.substring(1); } 172 } 173 if (!type.isBinary()) { 174 methodSignature = resolveMethodSignature(type, methodSignature); 176 if (methodSignature == null) { 177 report(ActionMessages.ManageMethodBreakpointActionDelegate_methodNonAvailable, part); 178 return Status.OK_STATUS; 179 } 180 } 181 createMethodBreakpoint(BreakpointUtils.getBreakpointResource(method), type.getFullyQualifiedName(), methodName, methodSignature, true, false, false, -1, start, end, 0, true, attributes); 182 } else { 183 removeBreakpoint(breakpoint, true); 185 } 186 } 187 } 188 } catch (CoreException e) { 189 return e.getStatus(); 190 } 191 return Status.OK_STATUS; 192 } 193 }; 194 job.setSystem(true); 195 job.schedule(); 196 197 } 198 199 public void toggleWatchpoints(final IWorkbenchPart part, final ISelection finalSelection) { 200 Job job = new Job("Toggle Watchpoints") { protected IStatus run(IProgressMonitor monitor) { 202 if (monitor.isCanceled()) { 203 return Status.CANCEL_STATUS; 204 } 205 if(isInterface(finalSelection)) { 206 report(ActionMessages.ToggleBreakpointAdapter_5, part); 207 return Status.OK_STATUS; 208 } 209 try { 210 report(null, part); 211 ISelection selection = finalSelection; 212 selection = translateToMembers(part, selection); 213 ITextEditor textEditor = getTextEditor(part); 214 boolean allowed = false; 215 if (textEditor != null && selection instanceof ITextSelection) { 216 ITextSelection textSelection = (ITextSelection) selection; 217 CompilationUnit compilationUnit = parseCompilationUnit(textEditor); 218 if (compilationUnit != null) { 219 BreakpointFieldLocator locator = new BreakpointFieldLocator(textSelection.getOffset()); 220 compilationUnit.accept(locator); 221 String fieldName = locator.getFieldName(); 222 if (fieldName == null) { 223 report(ActionMessages.ManageWatchpointActionDelegate_CantAdd, part); 224 return Status.OK_STATUS; 225 } 226 int idx = fieldName.indexOf("final"); if(!(idx > -1) & !(fieldName.indexOf("static") > -1 & idx > -1)) { allowed = true; 229 } 230 String typeName = locator.getTypeName(); 231 IJavaWatchpoint existing = getWatchpoint(typeName, fieldName); 234 if (existing == null) { 235 if(!allowed) { 236 report(ActionMessages.ToggleBreakpointAdapter_8, part); 237 return Status.OK_STATUS; 238 } 239 createWatchpoint(getResource((IEditorPart) part), typeName, fieldName, -1, -1, -1, 0, true, new HashMap (10)); 240 } else { 241 removeBreakpoint(existing, true); 242 } 243 } 244 } else if (selection instanceof IStructuredSelection) { 245 List fields = getFields((IStructuredSelection) selection); 246 if (fields.isEmpty()) { 247 report(ActionMessages.ToggleBreakpointAdapter_10, part); 248 return Status.OK_STATUS; 249 } 250 Iterator theFields = fields.iterator(); 251 while (theFields.hasNext()) { 252 Object element = theFields.next(); 253 IField javaField = null; 254 IJavaFieldVariable var = null; 255 String typeName = null; 256 String fieldName = null; 257 if (element instanceof IField) { 258 javaField = (IField) element; 259 typeName = javaField.getDeclaringType().getFullyQualifiedName(); 260 fieldName = javaField.getElementName(); 261 int f = javaField.getFlags(); 262 boolean fin = Flags.isFinal(f); 263 allowed = !fin && !(Flags.isStatic(f) && fin); 264 } else if (element instanceof IJavaFieldVariable) { 265 var = (IJavaFieldVariable) element; 266 typeName = var.getDeclaringType().getName(); 267 fieldName = var.getName(); 268 allowed = !(var.isFinal() || var.isStatic()); 269 } 270 IJavaBreakpoint breakpoint = getWatchpoint(typeName, fieldName); 271 if (breakpoint == null) { 272 if(!allowed) { 273 report(ActionMessages.ToggleBreakpointAdapter_8, part); 274 return Status.OK_STATUS; 275 } 276 IResource resource = null; 277 int start = -1; 278 int end = -1; 279 Map attributes = new HashMap (10); 280 if (javaField == null) { 281 if(var != null) { 282 Object object = JavaDebugUtils.resolveSourceElement(var.getJavaType(), var.getLaunch()); 283 if (object instanceof IAdaptable) { 284 IAdaptable adaptable = (IAdaptable) object; 285 resource = (IResource) adaptable.getAdapter(IResource.class); 286 } 287 } 288 if (resource == null) { 289 resource = ResourcesPlugin.getWorkspace().getRoot(); 290 } 291 } else { 292 IType type = javaField.getDeclaringType(); 293 ISourceRange range = javaField.getNameRange(); 294 if (range != null) { 295 start = range.getOffset(); 296 end = start + range.getLength(); 297 } 298 BreakpointUtils.addJavaBreakpointAttributes(attributes, javaField); 299 resource = BreakpointUtils.getBreakpointResource(type); 300 } 301 createWatchpoint(resource, typeName, fieldName, -1, start, end, 0, true, attributes); 302 } else { 303 removeBreakpoint(breakpoint, true); 305 } 306 } 307 } 308 } catch (CoreException e) { 309 return e.getStatus(); 310 } 311 return Status.OK_STATUS; 312 } 313 }; 314 job.setSystem(true); 315 job.schedule(); 316 } 317 318 324 public void toggleLineBreakpoints(final IWorkbenchPart part, final ISelection selection, final boolean bestMatch) { 325 Job job = new Job("Toggle Line Breakpoint") { protected IStatus run(IProgressMonitor monitor) { 327 if(isInterface(selection)) { 328 report(ActionMessages.ToggleBreakpointAdapter_6, part); 329 return Status.OK_STATUS; 330 } 331 ITextEditor editor = getTextEditor(part); 332 if (editor != null && selection instanceof ITextSelection) { 333 if (monitor.isCanceled()) { 334 return Status.CANCEL_STATUS; 335 } 336 report(null, part); 337 ITextSelection textSelection = (ITextSelection) selection; 338 IType type = getType(textSelection); 339 int lineNumber = textSelection.getStartLine() + 1; 340 int offset = textSelection.getOffset(); 341 try { 342 IEditorInput editorInput = editor.getEditorInput(); 343 IDocumentProvider documentProvider = editor.getDocumentProvider(); 344 if (documentProvider == null) { 345 return Status.CANCEL_STATUS; 346 } 347 IDocument document = documentProvider.getDocument(editorInput); 348 if (type == null) { 349 IClassFile classFile = (IClassFile) editorInput.getAdapter(IClassFile.class); 350 if (classFile != null) { 351 type = classFile.getType(); 352 if (type.getDeclaringType() != null) { 356 ISourceRange sourceRange = type.getSourceRange(); 357 int start = sourceRange.getOffset(); 358 int end = start + sourceRange.getLength(); 359 if (offset < start || offset > end) { 360 IStatusLineManager statusLine = editor.getEditorSite().getActionBars().getStatusLineManager(); 362 statusLine.setErrorMessage(MessageFormat.format(ActionMessages.ManageBreakpointRulerAction_Breakpoints_can_only_be_created_within_the_type_associated_with_the_editor___0___1, new String [] { type.getTypeQualifiedName() })); 363 Display.getCurrent().beep(); 364 return Status.OK_STATUS; 365 } 366 } 367 } 368 } 369 String typeName = null; 370 IResource resource = null; 371 Map attributes = new HashMap (10); 372 if (type == null) { 373 resource = getResource(editor); 374 CompilationUnit unit = parseCompilationUnit(editor); 375 Iterator types = unit.types().iterator(); 376 } else { 387 typeName = type.getFullyQualifiedName(); 388 int index = typeName.indexOf('$'); 389 if (index >= 0) { 390 typeName = typeName.substring(0, index); 391 } 392 resource = BreakpointUtils.getBreakpointResource(type); 393 try { 394 IRegion line = document.getLineInformation(lineNumber - 1); 395 int start = line.getOffset(); 396 int end = start + line.getLength() - 1; 397 BreakpointUtils.addJavaBreakpointAttributesWithMemberDetails(attributes, type, start, end); 398 } catch (BadLocationException ble) { 399 JDIDebugUIPlugin.log(ble); 400 } 401 } 402 if (typeName != null && resource != null) { 403 IJavaLineBreakpoint existingBreakpoint = JDIDebugModel.lineBreakpointExists(resource, typeName, lineNumber); 404 if (existingBreakpoint != null) { 405 removeBreakpoint(existingBreakpoint, true); 406 return Status.OK_STATUS; 407 } 408 createLineBreakpoint(resource, typeName, lineNumber, -1, -1, 0, true, attributes, document, bestMatch, type, editor); 409 } 410 } catch (CoreException ce) { 411 return ce.getStatus(); 412 } 413 } 414 return Status.OK_STATUS; 415 } 416 }; 417 job.setSystem(true); 418 job.schedule(); 419 } 420 421 430 protected IJavaMethodBreakpoint getMethodBreakpoint(String typeName, String methodName, String methodSignature) throws CoreException { 431 final IBreakpointManager breakpointManager = DebugPlugin.getDefault().getBreakpointManager(); 432 IBreakpoint[] breakpoints = breakpointManager.getBreakpoints(JDIDebugModel.getPluginIdentifier()); 433 for (int i = 0; i < breakpoints.length; i++) { 434 IBreakpoint breakpoint = breakpoints[i]; 435 if (breakpoint instanceof IJavaMethodBreakpoint) { 436 final IJavaMethodBreakpoint methodBreakpoint = (IJavaMethodBreakpoint) breakpoint; 437 if (typeName.equals(methodBreakpoint.getTypeName()) && methodName.equals(methodBreakpoint.getMethodName()) && methodSignature.equals(methodBreakpoint.getMethodSignature())) { 438 return methodBreakpoint; 439 } 440 } 441 } 442 return null; 443 } 444 445 453 protected IJavaWatchpoint getWatchpoint(String typeName, String fieldName) throws CoreException { 454 IBreakpointManager breakpointManager = DebugPlugin.getDefault().getBreakpointManager(); 455 IBreakpoint[] breakpoints = breakpointManager.getBreakpoints(JDIDebugModel.getPluginIdentifier()); 456 for (int i = 0; i < breakpoints.length; i++) { 457 IBreakpoint breakpoint = breakpoints[i]; 458 if (breakpoint instanceof IJavaWatchpoint) { 459 IJavaWatchpoint watchpoint = (IJavaWatchpoint) breakpoint; 460 if (typeName.equals(watchpoint.getTypeName()) && fieldName.equals(watchpoint.getFieldName())) { 461 return watchpoint; 462 } 463 } 464 } 465 return null; 466 } 467 468 protected void createMethodBreakpoint(IResource resource, String typeName, String methodName, String methodSignature, boolean entry, boolean exit, boolean nativeOnly, int lineNumber, int charStart, int charEnd, int hitCount, boolean register, Map attributes) throws CoreException { 469 JDIDebugModel.createMethodBreakpoint(resource, typeName, methodName, methodSignature, entry, exit, nativeOnly, lineNumber, charStart, charEnd, hitCount, register, attributes); 470 } 471 472 protected void createWatchpoint(IResource resource, String typeName, String fieldName, int lineNumber, int charStart, int charEnd, int hitCount, boolean register, Map attributes) throws CoreException { 473 JDIDebugModel.createWatchpoint(resource, typeName, fieldName, lineNumber, charStart, charEnd, hitCount, register, attributes); 474 } 475 476 protected void createLineBreakpoint(IResource resource, String typeName, int lineNumber, int charStart, int charEnd, int hitCount, boolean register, Map attributes, IDocument document, boolean bestMatch, IType type, IEditorPart editorPart) throws CoreException { 477 IJavaLineBreakpoint breakpoint = JDIDebugModel.createLineBreakpoint(resource, typeName, lineNumber, charStart, charEnd, hitCount, register, attributes); 478 new BreakpointLocationVerifierJob(document, breakpoint, lineNumber, bestMatch, typeName, type, resource, editorPart).schedule(); 479 } 480 481 487 protected void removeBreakpoint(IBreakpoint breakpoint, boolean delete) throws CoreException { 488 DebugPlugin.getDefault().getBreakpointManager().removeBreakpoint(breakpoint, delete); 489 } 490 491 497 protected boolean isInterface(ISelection selection) { 498 if (!selection.isEmpty()) { 499 try { 500 if(selection instanceof IStructuredSelection) { 501 IStructuredSelection ss = (IStructuredSelection) selection; 502 Iterator iterator = ss.iterator(); 503 IType type = null; 504 Object obj = null; 505 while (iterator.hasNext()) { 506 obj = iterator.next(); 507 if(obj instanceof IMember) { 508 type = ((IMember)obj).getDeclaringType(); 509 } 510 if(type != null && type.isInterface()) { 511 return true; 512 } 513 } 514 } 515 else if(selection instanceof ITextSelection) { 516 ITextSelection tsel = (ITextSelection) selection; 517 IType type = getType(tsel); 518 if(type != null && type.isInterface()) { 519 return true; 520 } 521 } 522 } 523 catch (JavaModelException e) {JDIDebugUIPlugin.log(e);} 524 } 525 return false; 526 } 527 } 528 | Popular Tags |