1 11 package org.eclipse.jdt.internal.debug.core.logicalstructures; 12 13 import com.ibm.icu.text.MessageFormat; 14 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IAdaptable; 18 import org.eclipse.core.runtime.IConfigurationElement; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.Status; 21 import org.eclipse.debug.core.DebugEvent; 22 import org.eclipse.debug.core.DebugException; 23 import org.eclipse.debug.core.DebugPlugin; 24 import org.eclipse.debug.core.ILogicalStructureType; 25 import org.eclipse.debug.core.IStatusHandler; 26 import org.eclipse.debug.core.model.IDebugTarget; 27 import org.eclipse.debug.core.model.ISourceLocator; 28 import org.eclipse.debug.core.model.IThread; 29 import org.eclipse.debug.core.model.IValue; 30 import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector; 31 import org.eclipse.jdt.core.IJavaElement; 32 import org.eclipse.jdt.core.IJavaProject; 33 import org.eclipse.jdt.core.JavaCore; 34 import org.eclipse.jdt.debug.core.IJavaClassType; 35 import org.eclipse.jdt.debug.core.IJavaDebugTarget; 36 import org.eclipse.jdt.debug.core.IJavaInterfaceType; 37 import org.eclipse.jdt.debug.core.IJavaObject; 38 import org.eclipse.jdt.debug.core.IJavaReferenceType; 39 import org.eclipse.jdt.debug.core.IJavaStackFrame; 40 import org.eclipse.jdt.debug.core.IJavaThread; 41 import org.eclipse.jdt.debug.core.IJavaType; 42 import org.eclipse.jdt.debug.core.IJavaValue; 43 import org.eclipse.jdt.debug.core.IJavaVariable; 44 import org.eclipse.jdt.debug.eval.IAstEvaluationEngine; 45 import org.eclipse.jdt.debug.eval.ICompiledExpression; 46 import org.eclipse.jdt.debug.eval.IEvaluationListener; 47 import org.eclipse.jdt.debug.eval.IEvaluationResult; 48 import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin; 49 50 public class JavaLogicalStructure implements ILogicalStructureType { 51 52 private static IStatusHandler fgStackFrameProvider; 53 54 57 private String fType; 58 62 private boolean fSubtypes; 63 66 private String fValue; 67 70 private String fDescription; 71 74 private String [][] fVariables; 75 79 private String fContributingPluginId= null; 80 81 84 private class EvaluationBlock implements IEvaluationListener { 85 86 private IJavaObject fEvaluationValue; 87 private IJavaReferenceType fEvaluationType; 88 private IJavaThread fThread; 89 private IAstEvaluationEngine fEvaluationEngine; 90 private IEvaluationResult fResult; 91 92 99 public EvaluationBlock(IJavaObject value, IJavaReferenceType type, IJavaThread thread, IAstEvaluationEngine evaluationEngine) { 100 fEvaluationValue= value; 101 fEvaluationType= type; 102 fThread= thread; 103 fEvaluationEngine= evaluationEngine; 104 } 105 106 109 public void evaluationComplete(IEvaluationResult result) { 110 synchronized(this) { 111 fResult= result; 112 this.notify(); 113 } 114 } 115 116 122 public IJavaValue evaluate(String snippet) throws DebugException { 123 ICompiledExpression compiledExpression= fEvaluationEngine.getCompiledExpression(snippet, fEvaluationType); 124 if (compiledExpression.hasErrors()) { 125 String [] errorMessages = compiledExpression.getErrorMessages(); 126 log(errorMessages); 127 return new JavaStructureErrorValue(errorMessages, fEvaluationValue); 128 } 129 fResult= null; 130 fEvaluationEngine.evaluateExpression(compiledExpression, fEvaluationValue, fThread, this, DebugEvent.EVALUATION_IMPLICIT, false); 131 synchronized(this) { 132 if (fResult == null) { 133 try { 134 this.wait(); 135 } catch (InterruptedException e) { 136 } 137 } 138 } 139 if (fResult == null) { 140 return new JavaStructureErrorValue(LogicalStructuresMessages.JavaLogicalStructure_1, fEvaluationValue); 141 } 142 if (fResult.hasErrors()) { 143 DebugException exception = fResult.getException(); 144 String message; 145 if (exception != null) { 146 message= MessageFormat.format(LogicalStructuresMessages.JavaLogicalStructure_2, new String [] { exception.getMessage() }); 147 } else { 148 message= LogicalStructuresMessages.JavaLogicalStructure_3; 149 } 150 return new JavaStructureErrorValue(message, fEvaluationValue); 151 } 152 return fResult.getValue(); 153 } 154 155 159 private void log(String [] messages) { 160 if (isContributed()) { 161 StringBuffer log= new StringBuffer (); 162 for (int i = 0; i < messages.length; i++) { 163 log.append(messages[i]).append('\n'); 164 } 165 JDIDebugPlugin.log(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), IStatus.ERROR, log.toString(),null)); 166 } 167 } 168 } 169 170 173 public JavaLogicalStructure(String type, boolean subtypes, String value, String description, String [][] variables) { 174 fType= type; 175 fSubtypes= subtypes; 176 fValue= value; 177 fDescription= description; 178 fVariables= variables; 179 } 180 181 184 public JavaLogicalStructure(IConfigurationElement configurationElement) throws CoreException { 185 fType= configurationElement.getAttribute("type"); if (fType == null) { 187 throw new CoreException(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), JDIDebugPlugin.INTERNAL_ERROR, LogicalStructuresMessages.JavaLogicalStructures_0, null)); 188 } 189 fSubtypes= Boolean.valueOf(configurationElement.getAttribute("subtypes")).booleanValue(); fValue= configurationElement.getAttribute("value"); fDescription= configurationElement.getAttribute("description"); if (fDescription == null) { 193 throw new CoreException(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), JDIDebugPlugin.INTERNAL_ERROR, LogicalStructuresMessages.JavaLogicalStructures_4, null)); 194 } 195 IConfigurationElement[] variableElements= configurationElement.getChildren("variable"); if (fValue== null && variableElements.length == 0) { 197 throw new CoreException(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), JDIDebugPlugin.INTERNAL_ERROR, LogicalStructuresMessages.JavaLogicalStructures_1, null)); 198 } 199 fVariables= new String [variableElements.length][2]; 200 for (int j= 0; j < fVariables.length; j++) { 201 String variableName= variableElements[j].getAttribute("name"); if (variableName == null) { 203 throw new CoreException(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), JDIDebugPlugin.INTERNAL_ERROR, LogicalStructuresMessages.JavaLogicalStructures_2, null)); 204 } 205 fVariables[j][0]= variableName; 206 String variableValue= variableElements[j].getAttribute("value"); if (variableValue == null) { 208 throw new CoreException(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), JDIDebugPlugin.INTERNAL_ERROR, LogicalStructuresMessages.JavaLogicalStructures_3, null)); 209 } 210 fVariables[j][1]= variableValue; 211 } 212 fContributingPluginId= configurationElement.getContributor().getName(); 213 } 214 215 218 public boolean providesLogicalStructure(IValue value) { 219 if (!(value instanceof IJavaObject)) { 220 return false; 221 } 222 return getType((IJavaObject) value) != null; 223 } 224 225 228 public IValue getLogicalStructure(IValue value) { 229 if (!(value instanceof IJavaObject)) { 230 return value; 231 } 232 IJavaObject javaValue= (IJavaObject) value; 233 try { 234 IJavaReferenceType type = getType(javaValue); 235 if (type == null) { 236 return value; 237 } 238 IJavaStackFrame stackFrame= getStackFrame(javaValue); 239 if (stackFrame == null) { 240 return value; 241 } 242 243 ISourceLocator locator= javaValue.getLaunch().getSourceLocator(); 245 Object sourceElement= null; 246 if (locator instanceof ISourceLookupDirector) { 247 String [] sourcePaths= type.getSourcePaths(null); 248 if (sourcePaths != null && sourcePaths.length > 0) { 249 sourceElement= ((ISourceLookupDirector) locator).getSourceElement(sourcePaths[0]); 250 } 251 if (!(sourceElement instanceof IJavaElement) && sourceElement instanceof IAdaptable) { 252 sourceElement = ((IAdaptable)sourceElement).getAdapter(IJavaElement.class); 253 } 254 } 255 if (sourceElement == null) { 256 sourceElement = locator.getSourceElement(stackFrame); 257 if (!(sourceElement instanceof IJavaElement) && sourceElement instanceof IAdaptable) { 258 sourceElement = ((IAdaptable)sourceElement).getAdapter(IJavaElement.class); 259 } 260 } 261 IJavaProject project= null; 262 if (sourceElement instanceof IJavaElement) { 263 project= ((IJavaElement) sourceElement).getJavaProject(); 264 } else if (sourceElement instanceof IResource) { 265 IJavaProject resourceProject = JavaCore.create(((IResource)sourceElement).getProject()); 266 if (resourceProject.exists()) { 267 project= resourceProject; 268 } 269 } 270 if (project == null) { 271 return value; 272 } 273 274 IAstEvaluationEngine evaluationEngine= JDIDebugPlugin.getDefault().getEvaluationEngine(project, (IJavaDebugTarget)stackFrame.getDebugTarget()); 275 276 EvaluationBlock evaluationBlock= new EvaluationBlock(javaValue, type, (IJavaThread)stackFrame.getThread(), evaluationEngine); 277 if (fValue == null) { 278 IJavaVariable[] variables= new IJavaVariable[fVariables.length]; 280 for (int i= 0; i < fVariables.length; i++) { 281 variables[i]= new JDIPlaceholderVariable(fVariables[i][0], evaluationBlock.evaluate(fVariables[i][1])); 282 } 283 return new LogicalObjectStructureValue(javaValue, variables); 284 } 285 return evaluationBlock.evaluate(fValue); 287 288 } catch (CoreException e) { 289 JDIDebugPlugin.log(e); 290 } 291 return value; 292 } 293 294 299 private IJavaReferenceType getType(IJavaObject value) { 300 try { 301 IJavaType type= value.getJavaType(); 302 if (!(type instanceof IJavaClassType)) { 303 return null; 304 } 305 IJavaClassType classType= (IJavaClassType) type; 306 if (classType.getName().equals(fType)) { 307 return classType; 309 } 310 if (!fSubtypes) { 311 return null; 313 } 314 IJavaClassType superClass= classType.getSuperclass(); 315 while (superClass != null) { 316 if (superClass.getName().equals(fType)) { 317 return superClass; 319 } 320 superClass= superClass.getSuperclass(); 321 } 322 IJavaInterfaceType[] superInterfaces= classType.getAllInterfaces(); 323 for (int i= 0; i < superInterfaces.length; i++) { 324 if (superInterfaces[i].getName().equals(fType)) { 325 return superInterfaces[i]; 327 } 328 } 329 } catch (DebugException e) { 330 JDIDebugPlugin.log(e); 331 return null; 332 } 333 return null; 334 } 335 336 342 private IJavaStackFrame getStackFrame(IValue value) throws CoreException { 343 IStatusHandler handler = getStackFrameProvider(); 344 if (handler != null) { 345 IJavaStackFrame stackFrame = (IJavaStackFrame)handler.handleStatus(JDIDebugPlugin.STATUS_GET_EVALUATION_FRAME, value); 346 if (stackFrame != null) { 347 return stackFrame; 348 } 349 } 350 IDebugTarget target = value.getDebugTarget(); 351 IJavaDebugTarget javaTarget = (IJavaDebugTarget) target.getAdapter(IJavaDebugTarget.class); 352 if (javaTarget != null) { 353 IThread[] threads = javaTarget.getThreads(); 354 for (int i = 0; i < threads.length; i++) { 355 IThread thread = threads[i]; 356 if (thread.isSuspended()) { 357 return (IJavaStackFrame)thread.getTopStackFrame(); 358 } 359 } 360 } 361 return null; 362 } 363 364 368 private static IStatusHandler getStackFrameProvider() { 369 if (fgStackFrameProvider == null) { 370 fgStackFrameProvider = DebugPlugin.getDefault().getStatusHandler(JDIDebugPlugin.STATUS_GET_EVALUATION_FRAME); 371 } 372 return fgStackFrameProvider; 373 } 374 375 379 public boolean isSubtypes() { 380 return fSubtypes; 381 } 382 383 387 public void setSubtypes(boolean subtypes) { 388 fSubtypes = subtypes; 389 } 390 391 395 public String getQualifiedTypeName() { 396 return fType; 397 } 398 402 public void setType(String type) { 403 fType = type; 404 } 405 409 public String getValue() { 410 return fValue; 411 } 412 416 public void setValue(String value) { 417 fValue = value; 418 } 419 423 public String [][] getVariables() { 424 return fVariables; 425 } 426 430 public void setVariables(String [][] variables) { 431 fVariables = variables; 432 } 433 437 public void setDescription(String description) { 438 fDescription = description; 439 } 440 441 444 public String getDescription(IValue value) { 445 return getDescription(); 446 } 447 448 451 public String getDescription() { 452 return fDescription; 453 } 454 455 460 public boolean isContributed() { 461 return fContributingPluginId != null; 462 } 463 464 470 public String getContributingPluginId() { 471 return fContributingPluginId; 472 } 473 474 477 public String getId() { 478 return JDIDebugPlugin.getUniqueIdentifier() + fType + fDescription; 479 } 480 } 481 | Popular Tags |