1 11 package org.eclipse.jdt.internal.debug.core.breakpoints; 12 13 14 import java.util.HashMap ; 15 import java.util.Map ; 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.resources.IWorkspaceRunnable; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.core.runtime.Status; 22 import org.eclipse.debug.core.DebugException; 23 import org.eclipse.debug.core.DebugPlugin; 24 import org.eclipse.debug.core.model.IDebugTarget; 25 import org.eclipse.jdt.debug.core.IJavaWatchpoint; 26 import org.eclipse.jdt.internal.debug.core.model.JDIDebugTarget; 27 import com.sun.jdi.Field; 28 import com.sun.jdi.ObjectReference; 29 import com.sun.jdi.ReferenceType; 30 import com.sun.jdi.ThreadReference; 31 import com.sun.jdi.VMDisconnectedException; 32 import com.sun.jdi.event.AccessWatchpointEvent; 33 import com.sun.jdi.event.Event; 34 import com.sun.jdi.event.ModificationWatchpointEvent; 35 import com.sun.jdi.request.AccessWatchpointRequest; 36 import com.sun.jdi.request.EventRequest; 37 import com.sun.jdi.request.EventRequestManager; 38 import com.sun.jdi.request.ModificationWatchpointRequest; 39 import com.sun.jdi.request.WatchpointRequest; 40 41 public class JavaWatchpoint extends JavaLineBreakpoint implements IJavaWatchpoint { 42 43 private static final String JAVA_WATCHPOINT= "org.eclipse.jdt.debug.javaWatchpointMarker"; 49 protected static final String ACCESS= "org.eclipse.jdt.debug.core.access"; 55 protected static final String MODIFICATION= "org.eclipse.jdt.debug.core.modification"; 61 protected static final String AUTO_DISABLED="org.eclipse.jdt.debug.core.auto_disabled"; 63 68 protected static final String FIELD_NAME= "org.eclipse.jdt.debug.core.fieldName"; 73 protected static final Integer ACCESS_EVENT= new Integer (0); 74 78 protected static final Integer MODIFICATION_EVENT= new Integer (1); 79 87 private HashMap fLastEventTypes= new HashMap (10); 88 89 public JavaWatchpoint() { 90 } 91 92 95 public JavaWatchpoint(final IResource resource, final String typeName, final String fieldName, final int lineNumber, final int charStart, final int charEnd, final int hitCount, final boolean add, final Map attributes) throws DebugException { 96 IWorkspaceRunnable wr= new IWorkspaceRunnable() { 97 public void run(IProgressMonitor monitor) throws CoreException { 98 setMarker(resource.createMarker(JAVA_WATCHPOINT)); 99 100 addLineBreakpointAttributes(attributes, getModelIdentifier(), true, lineNumber, charStart, charEnd); 102 addTypeNameAndHitCount(attributes, typeName, hitCount); 103 attributes.put(SUSPEND_POLICY, new Integer (getDefaultSuspendPolicy())); 104 addFieldName(attributes, fieldName); 106 addDefaultAccessAndModification(attributes); 108 109 ensureMarker().setAttributes(attributes); 111 112 register(add); 113 } 114 }; 115 run(getMarkerRule(resource), wr); 116 } 117 118 127 protected boolean createRequest(JDIDebugTarget target, ReferenceType type) throws CoreException { 128 if (shouldSkipBreakpoint()) { 129 return false; 130 } 131 Field field= null; 132 133 field= type.fieldByName(getFieldName()); 134 if (field == null) { 135 return false; 137 } 138 AccessWatchpointRequest accessRequest= null; 139 ModificationWatchpointRequest modificationRequest= null; 140 if (target.supportsAccessWatchpoints()) { 141 accessRequest= createAccessWatchpoint(target, field); 142 registerRequest(accessRequest, target); 143 } else { 144 notSupported(JDIDebugBreakpointMessages.JavaWatchpoint_no_access_watchpoints); 145 } 146 if (target.supportsModificationWatchpoints()) { 147 modificationRequest= createModificationWatchpoint(target, field); 148 if (modificationRequest == null) { 149 return false; 150 } 151 registerRequest(modificationRequest, target); 152 return true; 153 } 154 notSupported(JDIDebugBreakpointMessages.JavaWatchpoint_no_modification_watchpoints); 155 return false; 156 } 157 158 161 protected void setRequestThreadFilter(EventRequest request, ThreadReference thread) { 162 ((WatchpointRequest)request).addThreadFilter(thread); 163 } 164 165 171 protected void notSupported(String message) throws DebugException { 172 throw new DebugException(new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), 173 DebugException.NOT_SUPPORTED, message, null)); } 175 176 179 protected AccessWatchpointRequest createAccessWatchpoint(JDIDebugTarget target, Field field) throws CoreException { 180 return (AccessWatchpointRequest) createWatchpoint(target, field, true); 181 } 182 183 186 protected ModificationWatchpointRequest createModificationWatchpoint(JDIDebugTarget target, Field field) throws CoreException { 187 return (ModificationWatchpointRequest) createWatchpoint(target, field, false); 188 } 189 190 202 protected WatchpointRequest createWatchpoint(JDIDebugTarget target, Field field, boolean access) throws CoreException { 203 WatchpointRequest request= null; 204 EventRequestManager manager = target.getEventRequestManager(); 205 if (manager == null) { 206 target.requestFailed(JDIDebugBreakpointMessages.JavaWatchpoint_Unable_to_create_breakpoint_request___VM_disconnected__1, null); 207 } 208 try { 209 if (access) { 210 request= manager.createAccessWatchpointRequest(field); 211 } else { 212 request= manager.createModificationWatchpointRequest(field); 213 } 214 configureRequest(request, target); 215 } catch (VMDisconnectedException e) { 216 if (!target.isAvailable()) { 217 return null; 218 } 219 target.internalError(e); 220 return null; 221 } catch (RuntimeException e) { 222 target.internalError(e); 223 return null; 224 } 225 return request; 226 } 227 228 231 protected EventRequest recreateRequest(EventRequest request, JDIDebugTarget target) throws CoreException { 232 try { 233 Field field= ((WatchpointRequest) request).field(); 234 if (request instanceof AccessWatchpointRequest) { 235 request= createAccessWatchpoint(target, field); 236 } else if (request instanceof ModificationWatchpointRequest) { 237 request= createModificationWatchpoint(target, field); 238 } 239 } catch (VMDisconnectedException e) { 240 if (!target.isAvailable()) { 241 return request; 242 } 243 target.internalError(e); 244 return request; 245 } catch (RuntimeException e) { 246 target.internalError(e); 247 } 248 return request; 249 } 250 251 259 public void setEnabled(boolean enabled) throws CoreException { 260 if (enabled) { 261 if (!(isAccess() || isModification())) { 262 setDefaultAccessAndModification(); 263 } 264 } 265 super.setEnabled(enabled); 266 } 267 268 271 public boolean isAccess() throws CoreException { 272 return ensureMarker().getAttribute(ACCESS, false); 273 } 274 275 286 public void setAccess(boolean access) throws CoreException { 287 if (access == isAccess()) { 288 return; 289 } 290 setAttribute(ACCESS, access); 291 if (access && !isEnabled()) { 292 setEnabled(true); 293 } else if (!(access || isModification())) { 294 setEnabled(false); 295 } 296 recreate(); 297 } 298 299 302 public boolean isModification() throws CoreException { 303 return ensureMarker().getAttribute(MODIFICATION, false); 304 } 305 306 317 public void setModification(boolean modification) throws CoreException { 318 if (modification == isModification()) { 319 return; 320 } 321 setAttribute(MODIFICATION, modification); 322 if (modification && !isEnabled()) { 323 setEnabled(true); 324 } else if (!(modification || isAccess())) { 325 setEnabled(false); 326 } 327 recreate(); 328 } 329 330 338 protected void setDefaultAccessAndModification() throws CoreException { 339 Object [] values= new Object []{Boolean.TRUE, Boolean.TRUE}; 340 String [] attributes= new String []{ACCESS, MODIFICATION}; 341 setAttributes(attributes, values); 342 } 343 344 345 354 protected void addDefaultAccessAndModification(Map attributes) { 355 attributes.put(ACCESS, Boolean.TRUE); 356 attributes.put(MODIFICATION, Boolean.TRUE); 357 attributes.put(AUTO_DISABLED, Boolean.FALSE); 358 } 359 360 363 protected void addFieldName(Map attributes, String fieldName) { 364 attributes.put(FIELD_NAME, fieldName); 365 } 366 367 370 public String getFieldName() throws CoreException { 371 return ensureMarker().getAttribute(FIELD_NAME, null); 372 } 373 374 382 public boolean handleEvent(Event event, JDIDebugTarget target) { 383 if (event instanceof AccessWatchpointEvent) { 384 fLastEventTypes.put(target, ACCESS_EVENT); 385 } else if (event instanceof ModificationWatchpointEvent) { 386 fLastEventTypes.put(target, MODIFICATION_EVENT); 387 } 388 return super.handleEvent(event, target); 389 } 390 391 392 395 protected void updateEnabledState(EventRequest request, JDIDebugTarget target) throws CoreException { 396 boolean enabled = isEnabled(); 397 if (request instanceof AccessWatchpointRequest) { 398 if (isAccess()) { 399 if (enabled != request.isEnabled()) { 400 internalUpdateEnabledState(request, enabled, target); 401 } 402 } else { 403 if (request.isEnabled()) { 404 internalUpdateEnabledState(request, false, target); 405 } 406 } 407 } 408 if (request instanceof ModificationWatchpointRequest) { 409 if (isModification()) { 410 if (enabled != request.isEnabled()) { 411 internalUpdateEnabledState(request, enabled, target); 412 } 413 } else { 414 if (request.isEnabled()) { 415 internalUpdateEnabledState(request, false, target); 416 } 417 } 418 } 419 } 420 421 424 public boolean isAccessSuspend(IDebugTarget target) { 425 Integer lastEventType= (Integer ) fLastEventTypes.get(target); 426 if (lastEventType == null) { 427 return false; 428 } 429 return lastEventType.equals(ACCESS_EVENT); 430 } 431 434 public boolean supportsCondition() { 435 return false; 436 } 437 438 441 public void removeFromTarget(JDIDebugTarget target) throws CoreException { 442 fLastEventTypes.remove(target); 443 super.removeFromTarget(target); 444 } 445 446 449 protected void addInstanceFilter(EventRequest request, ObjectReference object) { 450 if (request instanceof WatchpointRequest) { 451 ((WatchpointRequest)request).addInstanceFilter(object); 452 } 453 } 454 455 458 public boolean supportsAccess() { 459 return true; 460 } 461 462 465 public boolean supportsModification() { 466 return true; 467 } 468 469 472 protected boolean installableReferenceType(ReferenceType type, JDIDebugTarget target) throws CoreException { 473 String installableType= getTypeName(); 474 String queriedType= type.name(); 475 if (installableType == null || queriedType == null) { 476 return false; 477 } 478 if (installableType.equals(queriedType)) { 479 return queryInstallListeners(target, type); 480 } 481 482 return false; 483 } 484 } 485 | Popular Tags |