1 11 package org.eclipse.jdt.internal.debug.core.model; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.debug.core.DebugException; 19 import org.eclipse.jdt.debug.core.IJavaFieldVariable; 20 import org.eclipse.jdt.debug.core.IJavaObject; 21 import org.eclipse.jdt.debug.core.IJavaThread; 22 import org.eclipse.jdt.debug.core.IJavaValue; 23 24 import com.ibm.icu.text.MessageFormat; 25 import com.sun.jdi.ArrayType; 26 import com.sun.jdi.ClassType; 27 import com.sun.jdi.Field; 28 import com.sun.jdi.IncompatibleThreadStateException; 29 import com.sun.jdi.Method; 30 import com.sun.jdi.ObjectReference; 31 import com.sun.jdi.ReferenceType; 32 import com.sun.jdi.ThreadReference; 33 import com.sun.jdi.VMDisconnectedException; 34 import com.sun.jdi.Value; 35 36 40 public class JDIObjectValue extends JDIValue implements IJavaObject { 41 42 private IJavaObject[] fCachedReferences; 43 private int fSuspendCount; 44 private long fPreviousMax; 45 46 50 public JDIObjectValue(JDIDebugTarget target, ObjectReference object) { 51 super(target, object); 52 fSuspendCount = -1; 53 fCachedReferences = null; 54 } 55 56 59 public IJavaValue sendMessage(String selector, String signature, IJavaValue[] args, IJavaThread thread, boolean superSend) throws DebugException { 60 JDIThread javaThread = (JDIThread)thread; 61 List arguments = null; 62 if (args == null) { 63 arguments = Collections.EMPTY_LIST; 64 } else { 65 arguments= new ArrayList (args.length); 66 for (int i = 0; i < args.length; i++) { 67 arguments.add(((JDIValue)args[i]).getUnderlyingValue()); 68 } 69 } 70 ObjectReference object = getUnderlyingObject(); 71 Method method = null; 72 ReferenceType refType = getUnderlyingReferenceType(); 73 try { 74 if (superSend) { 75 refType = ((ClassType)refType).superclass(); 77 } 78 method = concreteMethodByName(refType, selector, signature); 79 if (method == null) { 80 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIObjectValue_11, new String [] {selector, signature}), null); 81 } 82 } catch (RuntimeException e) { 83 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIObjectValue_exception_while_performing_method_lookup_for_selector, new String [] {e.toString(), selector, signature}), e); 84 } 85 Value result = javaThread.invokeMethod(null, object, method, arguments, superSend); 86 return JDIValue.createValue((JDIDebugTarget)getDebugTarget(), result); 87 } 88 89 92 public IJavaValue sendMessage(String selector, String signature, IJavaValue[] args, IJavaThread thread, String typeSignature) throws DebugException { 93 JDIThread javaThread = (JDIThread)thread; 94 List arguments = null; 95 if (args == null) { 96 arguments = Collections.EMPTY_LIST; 97 } else { 98 arguments= new ArrayList (args.length); 99 for (int i = 0; i < args.length; i++) { 100 arguments.add(((JDIValue)args[i]).getUnderlyingValue()); 101 } 102 } 103 ObjectReference object = getUnderlyingObject(); 104 Method method = null; 105 ReferenceType refType = getUnderlyingReferenceType(); 106 try { 107 while (typeSignature != null && !refType.signature().equals(typeSignature)) { 108 refType = ((ClassType)refType).superclass(); 110 if (refType == null) { 111 targetRequestFailed(JDIDebugModelMessages.JDIObjectValueMethod_declaring_type_not_found_1, null); 112 } 113 } 114 method= concreteMethodByName(refType, selector, signature); 115 if (method == null) { 116 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIObjectValue_11, new String [] {selector, signature}), null); 117 } 118 } catch (RuntimeException e) { 119 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIObjectValue_exception_while_performing_method_lookup_for_selector, new String [] {e.toString(), selector, signature}), e); 120 } 121 Value result = javaThread.invokeMethod(null, object, method, arguments, true); 122 return JDIValue.createValue((JDIDebugTarget)getDebugTarget(), result); 123 } 124 125 private Method concreteMethodByName(ReferenceType refType, String selector, String signature) throws DebugException { 126 if (refType instanceof ClassType) { 127 return ((ClassType)refType).concreteMethodByName(selector, signature); 128 } 129 if (refType instanceof ArrayType) { 130 return ((ClassType)refType.classObject().referenceType()).superclass().concreteMethodByName(selector, signature); 133 } 134 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIObjectValue_method_lookup_failed_for_selector____0____with_signature____1___1, new String [] {selector, signature}), null); 135 return null; 137 } 138 139 144 public ObjectReference getUnderlyingObject() { 145 return (ObjectReference)getUnderlyingValue(); 146 } 147 148 151 public IJavaFieldVariable getField(String name, boolean superField) throws DebugException { 152 ReferenceType ref = getUnderlyingReferenceType(); 153 try { 154 if (superField) { 155 ref = ((ClassType)ref).superclass(); 157 } 158 Field field = ref.fieldByName(name); 159 if (field != null) { 160 return new JDIFieldVariable((JDIDebugTarget)getDebugTarget(), field, getUnderlyingObject()); 161 } 162 Field enclosingThis= null; 163 Iterator fields= ref.fields().iterator(); 164 while (fields.hasNext()) { 165 Field fieldTmp = (Field)fields.next(); 166 if (fieldTmp.name().startsWith("this$")) { enclosingThis= fieldTmp; 168 break; 169 } 170 } 171 172 if (enclosingThis != null) 173 return ((JDIObjectValue)(new JDIFieldVariable((JDIDebugTarget)getDebugTarget(), enclosingThis, getUnderlyingObject())).getValue()).getField(name, false); 174 } catch (RuntimeException e) { 175 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIObjectValue_exception_retrieving_field, new String []{e.toString()}), e); 176 } 177 return null; 179 } 180 181 184 public IJavaFieldVariable getField(String name, String declaringTypeSignature) throws DebugException { 185 ReferenceType ref= getUnderlyingReferenceType(); 186 try { 187 Field field= null; 188 Field fieldTmp= null; 189 Iterator fields= ref.allFields().iterator(); 190 while (fields.hasNext()) { 191 fieldTmp = (Field)fields.next(); 192 if (name.equals(fieldTmp.name()) && declaringTypeSignature.equals(fieldTmp.declaringType().signature())) { 193 field= fieldTmp; 194 break; 195 } 196 } 197 if (field != null) { 198 return new JDIFieldVariable((JDIDebugTarget)getDebugTarget(), field, getUnderlyingObject()); 199 } 200 } catch (RuntimeException e) { 201 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIObjectValue_exception_retrieving_field, new String []{e.toString()}), e); 202 } 203 return null; 205 } 206 207 222 public IJavaFieldVariable getField(String name, int superClassLevel) throws DebugException { 223 ReferenceType ref= getUnderlyingReferenceType(); 224 try { 225 for (int i= 0 ; i < superClassLevel; i++) { 226 ref= ((ClassType)ref).superclass(); 227 } 228 Field field = ref.fieldByName(name); 229 if (field != null) { 230 return new JDIFieldVariable((JDIDebugTarget)getDebugTarget(), field, getUnderlyingObject()); 231 } 232 } catch (RuntimeException e) { 233 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIObjectValue_exception_retrieving_field, new String []{e.toString()}), e); 234 } 235 return null; 237 } 238 239 247 protected ReferenceType getUnderlyingReferenceType() throws DebugException { 248 try { 249 return getUnderlyingObject().referenceType(); 250 } catch (RuntimeException e) { 251 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIObjectValue_exception_retrieving_reference_type, new String []{e.toString()}), e); 252 } 253 return null; 256 257 } 258 259 263 public IJavaObject getEnclosingObject(int enclosingLevel) throws DebugException { 264 JDIObjectValue res= this; 265 for (int i= 0; i < enclosingLevel; i ++) { 266 ReferenceType ref= res.getUnderlyingReferenceType(); 267 try { 268 Field enclosingThis= null, fieldTmp= null; 269 Iterator fields= ref.fields().iterator(); 270 while (fields.hasNext()) { 271 fieldTmp = (Field)fields.next(); 272 if (fieldTmp.name().startsWith("this$")) { enclosingThis= fieldTmp; 274 } 275 } 276 if (enclosingThis != null) { 277 JDIDebugTarget debugTarget = (JDIDebugTarget)getDebugTarget(); 278 JDIFieldVariable fieldVariable = new JDIFieldVariable(debugTarget, enclosingThis, res.getUnderlyingObject()); 279 res = (JDIObjectValue)fieldVariable.getValue(); 280 } else { 281 return null; 283 } 284 } catch (RuntimeException e) { 285 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIObjectValue_exception_retrieving_field, new String []{e.toString()}), e); 286 } 287 } 288 return res; 289 } 290 291 294 public IJavaThread[] getWaitingThreads() throws DebugException { 295 List waiting = new ArrayList (); 296 try { 297 List threads= getUnderlyingObject().waitingThreads(); 298 JDIDebugTarget debugTarget= (JDIDebugTarget)getDebugTarget(); 299 for (Iterator iter= threads.iterator(); iter.hasNext();) { 300 JDIThread jdiThread = debugTarget.findThread((ThreadReference) iter.next()); 301 if (jdiThread != null) { 302 waiting.add(jdiThread); 303 } 304 } 305 } catch (IncompatibleThreadStateException e) { 306 targetRequestFailed(JDIDebugModelMessages.JDIObjectValue_0, e); 307 } catch (VMDisconnectedException e) { 308 } catch (RuntimeException e) { 310 targetRequestFailed(JDIDebugModelMessages.JDIObjectValue_0, e); 311 } 312 return (IJavaThread[]) waiting.toArray(new IJavaThread[waiting.size()]); 313 } 314 315 318 public IJavaThread getOwningThread() throws DebugException { 319 IJavaThread owningThread= null; 320 try { 321 ThreadReference thread= getUnderlyingObject().owningThread(); 322 JDIDebugTarget debugTarget= (JDIDebugTarget)getDebugTarget(); 323 if (thread != null) { 324 owningThread= debugTarget.findThread(thread); 325 } 326 } catch (IncompatibleThreadStateException e) { 327 targetRequestFailed(JDIDebugModelMessages.JDIObjectValue_1, e); 328 } catch (VMDisconnectedException e) { 329 return null; 330 } catch (RuntimeException e) { 331 targetRequestFailed(JDIDebugModelMessages.JDIObjectValue_1, e); 332 } 333 return owningThread; 334 } 335 336 339 public String getReferenceTypeName() throws DebugException { 340 try { 341 return JDIReferenceType.getGenericName(getUnderlyingReferenceType()); 342 } catch (RuntimeException e) { 343 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIValue_exception_retrieving_reference_type_name, new String [] {e.toString()}), e); 344 return null; 347 } 348 } 349 350 358 public long getUniqueId() throws DebugException { 359 try { 360 ObjectReference underlyingObject = getUnderlyingObject(); 361 if (underlyingObject != null) { 362 return underlyingObject.uniqueID(); 363 } else { 364 return -1L; 365 } 366 } catch (RuntimeException e) { 367 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIValue_exception_retrieving_unique_id, new String [] {e.toString()}), e); 368 return 0; 371 } 372 } 373 374 377 public IJavaObject[] getReferringObjects(long max) throws DebugException { 378 if (fCachedReferences == null || fSuspendCount < ((JDIDebugTarget)getDebugTarget()).getSuspendCount() || fPreviousMax != max){ 380 reloadReferringObjects(max); 381 fPreviousMax = max; 382 fSuspendCount = ((JDIDebugTarget)getDebugTarget()).getSuspendCount(); 383 } 384 return fCachedReferences; 385 } 386 387 392 public boolean isReferencesLoaded(){ 393 return fCachedReferences != null; 394 } 395 396 403 protected void reloadReferringObjects(long max) throws DebugException{ 404 try{ 405 List list = getUnderlyingObject().referringObjects(max); 406 IJavaObject[] references = new IJavaObject[list.size()]; 407 for (int i = 0; i < references.length; i++) { 408 references[i] = (IJavaObject) JDIValue.createValue(getJavaDebugTarget(), (Value) list.get(i)); 409 } 410 fCachedReferences = references; 411 } catch (RuntimeException e) { 412 targetRequestFailed(JDIDebugModelMessages.JDIObjectValue_12, e); 413 fCachedReferences = null; 414 } 415 } 416 } | Popular Tags |