1 11 package org.eclipse.jdt.internal.debug.core.breakpoints; 12 13 14 import java.util.ArrayList ; 15 import java.util.HashSet ; 16 import java.util.List ; 17 import java.util.Map ; 18 import java.util.Set ; 19 import java.util.StringTokenizer ; 20 import java.util.regex.Pattern ; 21 22 import org.eclipse.core.resources.IResource; 23 import org.eclipse.core.resources.IWorkspaceRunnable; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IProgressMonitor; 26 import org.eclipse.debug.core.DebugException; 27 import org.eclipse.debug.core.model.IBreakpoint; 28 import org.eclipse.jdt.debug.core.IJavaDebugTarget; 29 import org.eclipse.jdt.debug.core.IJavaExceptionBreakpoint; 30 import org.eclipse.jdt.debug.core.IJavaObject; 31 import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin; 32 import org.eclipse.jdt.internal.debug.core.model.JDIDebugTarget; 33 import org.eclipse.jdt.internal.debug.core.model.JDIThread; 34 import org.eclipse.jdt.internal.debug.core.model.JDIValue; 35 36 import com.sun.jdi.ClassType; 37 import com.sun.jdi.Location; 38 import com.sun.jdi.ObjectReference; 39 import com.sun.jdi.ReferenceType; 40 import com.sun.jdi.ThreadReference; 41 import com.sun.jdi.VMDisconnectedException; 42 import com.sun.jdi.event.Event; 43 import com.sun.jdi.event.ExceptionEvent; 44 import com.sun.jdi.request.EventRequest; 45 import com.sun.jdi.request.EventRequestManager; 46 import com.sun.jdi.request.ExceptionRequest; 47 48 public class JavaExceptionBreakpoint extends JavaBreakpoint implements IJavaExceptionBreakpoint { 49 50 private static final String JAVA_EXCEPTION_BREAKPOINT= "org.eclipse.jdt.debug.javaExceptionBreakpointMarker"; 52 58 protected static final String CAUGHT = "org.eclipse.jdt.debug.core.caught"; 65 protected static final String UNCAUGHT = "org.eclipse.jdt.debug.core.uncaught"; 71 protected static final String CHECKED = "org.eclipse.jdt.debug.core.checked"; 73 79 protected static final String INCLUSION_FILTERS = "org.eclipse.jdt.debug.core.inclusion_filters"; 81 87 protected static final String EXCLUSION_FILTERS = "org.eclipse.jdt.debug.core.exclusion_filters"; 92 protected static final String SUSPEND_ON_SUBCLASSES = "org.eclipse.jdt.debug.core.suspend_on_subclasses"; 94 98 protected String fExceptionName = null; 99 100 103 protected String [] fInclusionClassFilters= null; 104 105 108 protected String [] fExclusionClassFilters= null; 109 110 private ObjectReference fLastException; 111 private JDIDebugTarget fLastTarget; 112 113 public JavaExceptionBreakpoint() { 114 } 115 116 133 public JavaExceptionBreakpoint(final IResource resource, final String exceptionName, final boolean caught, final boolean uncaught, final boolean checked, final boolean add, final Map attributes) throws DebugException { 134 IWorkspaceRunnable wr= new IWorkspaceRunnable() { 135 136 public void run(IProgressMonitor monitor) throws CoreException { 137 setMarker(resource.createMarker(JAVA_EXCEPTION_BREAKPOINT)); 139 140 attributes.put(IBreakpoint.ID, getModelIdentifier()); 142 attributes.put(TYPE_NAME, exceptionName); 143 attributes.put(ENABLED, Boolean.TRUE); 144 attributes.put(CAUGHT, Boolean.valueOf(caught)); 145 attributes.put(UNCAUGHT, Boolean.valueOf(uncaught)); 146 attributes.put(CHECKED, Boolean.valueOf(checked)); 147 attributes.put(SUSPEND_POLICY, new Integer (getDefaultSuspendPolicy())); 148 149 ensureMarker().setAttributes(attributes); 150 151 register(add); 152 } 153 154 }; 155 run(getMarkerRule(resource), wr); 156 } 157 158 163 protected EventRequest[] newRequests(JDIDebugTarget target, ReferenceType type) throws CoreException { 164 if (!isCaught() && !isUncaught()) { 165 return null; 166 } 167 ExceptionRequest request= null; 168 EventRequestManager manager = target.getEventRequestManager(); 169 if (manager == null) { 170 target.requestFailed(JDIDebugBreakpointMessages.JavaExceptionBreakpoint_Unable_to_create_breakpoint_request___VM_disconnected__1, null); 171 return null; 172 } 173 174 try { 175 request= manager.createExceptionRequest(type, isCaught(), isUncaught()); 176 configureRequest(request, target); 177 } catch (VMDisconnectedException e) { 178 if (target.isAvailable()) { 179 JDIDebugPlugin.log(e); 180 } 181 return null; 182 } catch (RuntimeException e) { 183 target.internalError(e); 184 return null; 185 } 186 return new EventRequest[]{request}; 187 } 188 189 197 public void setEnabled(boolean enabled) throws CoreException { 198 if (enabled) { 199 if (!(isCaught() || isUncaught())) { 200 setAttributes(new String [] {CAUGHT, UNCAUGHT}, new Object [] {Boolean.TRUE, Boolean.TRUE}); 201 } 202 } 203 super.setEnabled(enabled); 204 } 205 206 211 protected void setCaughtAndUncaught(boolean caught, boolean uncaught) throws CoreException { 212 Object [] values= new Object []{Boolean.valueOf(caught), Boolean.valueOf(uncaught)}; 213 String [] attributes= new String []{CAUGHT, UNCAUGHT}; 214 setAttributes(attributes, values); 215 } 216 217 220 public boolean isCaught() throws CoreException { 221 return ensureMarker().getAttribute(CAUGHT, false); 222 } 223 224 227 public void setCaught(boolean caught) throws CoreException { 228 if (caught == isCaught()) { 229 return; 230 } 231 setAttribute(CAUGHT, caught); 232 if (caught && !isEnabled()) { 233 setEnabled(true); 234 } else if (!(caught || isUncaught())) { 235 setEnabled(false); 236 } 237 recreate(); 238 } 239 240 243 public void setSuspendOnSubclasses(boolean suspend) throws CoreException { 244 if(suspend != isSuspendOnSubclasses()) { 245 setAttribute(SUSPEND_ON_SUBCLASSES, suspend); 246 recreate(); 247 } 248 } 249 250 253 public boolean isSuspendOnSubclasses() throws CoreException { 254 return ensureMarker().getAttribute(SUSPEND_ON_SUBCLASSES, false); 255 } 256 257 260 public boolean isUncaught() throws CoreException { 261 return ensureMarker().getAttribute(UNCAUGHT, false); 262 } 263 264 267 public void setUncaught(boolean uncaught) throws CoreException { 268 if (uncaught == isUncaught()) { 269 return; 270 } 271 setAttribute(UNCAUGHT, uncaught); 272 if (uncaught && !isEnabled()) { 273 setEnabled(true); 274 } else if (!(uncaught || isCaught())) { 275 setEnabled(false); 276 } 277 recreate(); 278 } 279 280 283 public boolean isChecked() throws CoreException { 284 return ensureMarker().getAttribute(CHECKED, false); 285 } 286 287 290 protected void setRequestThreadFilter(EventRequest request, ThreadReference thread) { 291 ((ExceptionRequest)request).addThreadFilter(thread); 292 } 293 294 300 public boolean handleBreakpointEvent(Event event, JDIDebugTarget target, JDIThread thread) { 301 if (event instanceof ExceptionEvent) { 302 ObjectReference ex = ((ExceptionEvent)event).exception(); 303 fLastTarget = target; 304 fLastException = ex; 305 String name = null; 306 try { 307 name = ex.type().name(); 308 if(!name.equals(getTypeName())) { 309 if(!isSuspendOnSubclasses() & isSubclass((ClassType) ex.type(), getTypeName())) { 310 return true; 311 } 312 } 313 } catch (VMDisconnectedException e) { 314 return true; 315 } catch (CoreException e) { 316 JDIDebugPlugin.log(e); 317 } catch (RuntimeException e) { 318 try { 319 target.targetRequestFailed(e.getMessage(), e); 320 } catch (DebugException de) { 321 JDIDebugPlugin.log(e); 322 return false; 323 } 324 } 325 setExceptionName(name); 326 if (getExclusionClassFilters().length >= 1 327 || getInclusionClassFilters().length >= 1 328 || filtersIncludeDefaultPackage(fInclusionClassFilters) 329 || filtersIncludeDefaultPackage(fExclusionClassFilters)) { 330 Location location = ((ExceptionEvent)event).location(); 331 String typeName = location.declaringType().name(); 332 boolean defaultPackage = typeName.indexOf('.') == -1; 333 boolean included = true; 334 String [] filters = getInclusionClassFilters(); 335 if (filters.length > 0) { 336 included = matchesFilters(filters, typeName, defaultPackage); 337 } 338 boolean excluded = false; 339 filters = getExclusionClassFilters(); 340 if (filters.length > 0) { 341 excluded = matchesFilters(filters, typeName, defaultPackage); 342 } 343 if (included && !excluded) { 344 return !suspend(thread); 345 } 346 return true; 347 } 348 return !suspend(thread); 349 } 350 return true; 351 } 352 353 362 private boolean isSubclass(ClassType type, String typeName) { 363 type = type.superclass(); 364 while (type != null) { 365 if (type.name().equals(typeName)) { 366 return true; 367 } 368 type = type.superclass(); 369 } 370 return false; 371 } 372 373 376 protected void setInstalledIn(IJavaDebugTarget target, boolean installed) { 377 fLastException = null; 378 fLastTarget = null; 379 super.setInstalledIn(target, installed); 380 } 381 382 387 protected boolean filtersIncludeDefaultPackage(String [] filters) { 388 for (int i = 0; i < filters.length; i++) { 389 if (filters[i].length() == 0 || (filters[i].indexOf('.') == -1)) { 390 return true; 391 } 392 } 393 return false; 394 } 395 396 404 protected boolean matchesFilters(String [] filters, String typeName, boolean defaultPackage) { 405 for (int i = 0; i < filters.length; i++) { 406 String filter = filters[i]; 407 if (defaultPackage && filter.length() == 0) { 408 return true; 409 } 410 411 filter = filter.replaceAll("\\.", "\\\\."); filter = filter.replaceAll("\\*", "\\.\\*"); Pattern pattern = Pattern.compile(filter); 414 if (pattern.matcher(typeName).find()) { 415 return true; 416 } 417 } 418 return false; 419 } 420 421 426 protected void setExceptionName(String name) { 427 fExceptionName = name; 428 } 429 430 433 public String getExceptionTypeName() { 434 return fExceptionName; 435 } 436 437 441 public String [] getFilters() { 442 String [] iFilters= getInclusionFilters(); 443 String [] eFilters= getExclusionFilters(); 444 String [] filters= new String [iFilters.length + eFilters.length]; 445 System.arraycopy(iFilters, 0, filters, 0, iFilters.length); 446 System.arraycopy(eFilters, 0, filters, iFilters.length, eFilters.length); 447 return filters; 448 } 449 450 454 public void setFilters(String [] filters, boolean inclusive) throws CoreException { 455 if (inclusive) { 456 setInclusionFilters(filters); 457 } else { 458 setExclusionFilters(filters); 459 } 460 recreate(); 461 } 462 463 466 protected void configureRequest(EventRequest eRequest, JDIDebugTarget target) throws CoreException { 467 String [] iFilters= getInclusionClassFilters(); 468 String [] eFilters= getExclusionClassFilters(); 469 470 ExceptionRequest request= (ExceptionRequest)eRequest; 471 472 if (iFilters.length == 1) { 473 if (eFilters.length ==0) { 474 request.addClassFilter(iFilters[0]); 475 } 476 } else if (eFilters.length == 1) { 477 if (iFilters.length == 0) { 478 request.addClassExclusionFilter(eFilters[0]); 479 } 480 } 481 482 super.configureRequest(eRequest, target); 483 } 484 485 490 protected String serializeList(String [] list) { 491 if (list == null) { 492 return ""; } 494 Set set= new HashSet (list.length); 495 496 StringBuffer buffer = new StringBuffer (); 497 for (int i = 0; i < list.length; i++) { 498 if (i > 0) { 499 buffer.append(','); 500 } 501 String pattern= list[i]; 502 if (!set.contains(pattern)) { 503 if (pattern.length() == 0) { 504 pattern= "."; } 507 buffer.append(pattern); 508 } 509 } 510 return buffer.toString(); 511 } 512 513 516 protected String [] parseList(String listString) { 517 List list = new ArrayList (10); 518 StringTokenizer tokenizer = new StringTokenizer (listString, ","); while (tokenizer.hasMoreTokens()) { 520 String token = tokenizer.nextToken(); 521 if (token.equals(".")) { token= ""; } 526 list.add(token); 527 } 528 return (String [])list.toArray(new String [list.size()]); 529 } 530 531 535 public boolean isInclusiveFiltered() throws CoreException { 536 return ensureMarker().getAttribute(INCLUSION_FILTERS, "").length() > 0; } 538 539 protected String [] getInclusionClassFilters() { 540 if (fInclusionClassFilters == null) { 541 try { 542 fInclusionClassFilters= parseList(ensureMarker().getAttribute(INCLUSION_FILTERS, "")); } catch (CoreException ce) { 544 fInclusionClassFilters= new String []{}; 545 } 546 } 547 return fInclusionClassFilters; 548 } 549 550 protected void setInclusionClassFilters(String [] filters) { 551 fInclusionClassFilters = filters; 552 } 553 554 protected String [] getExclusionClassFilters() { 555 if (fExclusionClassFilters == null) { 556 try { 557 fExclusionClassFilters= parseList(ensureMarker().getAttribute(EXCLUSION_FILTERS, "")); } catch (CoreException ce) { 559 fExclusionClassFilters= new String []{}; 560 } 561 } 562 return fExclusionClassFilters; 563 } 564 565 protected void setExclusionClassFilters(String [] filters) { 566 fExclusionClassFilters = filters; 567 } 568 569 572 protected boolean installableReferenceType(ReferenceType type, JDIDebugTarget target) throws CoreException { 573 String installableType= getTypeName(); 574 String queriedType= type.name(); 575 if (installableType == null || queriedType == null) { 576 return false; 577 } 578 if (installableType.equals(queriedType)) { 579 return queryInstallListeners(target, type); 580 } 581 582 return false; 583 } 584 587 public String [] getExclusionFilters() { 588 return getExclusionClassFilters(); 589 } 590 591 594 public String [] getInclusionFilters() { 595 return getInclusionClassFilters(); 596 } 597 598 601 public void setExclusionFilters(String [] filters) throws CoreException { 602 String serializedFilters= serializeList(filters); 603 604 if (serializedFilters.equals(ensureMarker().getAttribute(EXCLUSION_FILTERS, ""))) { return; 607 } 608 609 setExclusionClassFilters(filters); 610 611 setAttribute(EXCLUSION_FILTERS, serializedFilters); 612 recreate(); 613 } 614 615 618 public void setInclusionFilters(String [] filters) throws CoreException { 619 String serializedFilters= serializeList(filters); 620 621 if (serializedFilters.equals(ensureMarker().getAttribute(INCLUSION_FILTERS, ""))) { return; 624 } 625 626 setInclusionClassFilters(filters); 627 628 setAttribute(INCLUSION_FILTERS, serializedFilters); 629 recreate(); 630 } 631 632 635 protected void addInstanceFilter(EventRequest request, ObjectReference object) { 636 if (request instanceof ExceptionRequest) { 637 ((ExceptionRequest)request).addInstanceFilter(object); 638 } 639 } 640 641 648 public IJavaObject getLastException() { 649 if (fLastException != null) { 650 return (IJavaObject) JDIValue.createValue(fLastTarget, fLastException); 651 } 652 return null; 653 } 654 } 655 656 | Popular Tags |