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.Comparator ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.debug.core.DebugException; 20 import org.eclipse.debug.core.model.IVariable; 21 import org.eclipse.jdt.debug.core.IJavaType; 22 import org.eclipse.jdt.debug.core.IJavaValue; 23 import org.eclipse.jdt.debug.core.IJavaVariable; 24 25 import com.ibm.icu.text.MessageFormat; 26 import com.sun.jdi.ArrayReference; 27 import com.sun.jdi.ClassObjectReference; 28 import com.sun.jdi.Field; 29 import com.sun.jdi.ObjectCollectedException; 30 import com.sun.jdi.ObjectReference; 31 import com.sun.jdi.PrimitiveValue; 32 import com.sun.jdi.ReferenceType; 33 import com.sun.jdi.StringReference; 34 import com.sun.jdi.Type; 35 import com.sun.jdi.VMDisconnectedException; 36 import com.sun.jdi.Value; 37 38 43 public class JDIValue extends JDIDebugElement implements IJavaValue { 44 45 private Value fValue; 46 private List fVariables; 47 48 51 private boolean fAllocated = true; 52 53 58 public JDIValue(JDIDebugTarget target, Value value) { 59 super(target); 60 fValue = value; 61 } 62 63 66 public Object getAdapter(Class adapter) { 67 if (adapter == IJavaValue.class) { 68 return this; 69 } 70 return super.getAdapter(adapter); 71 } 72 73 77 public static JDIValue createValue(JDIDebugTarget target, Value value) { 78 if (value == null) { 79 return new JDINullValue(target); 80 } 81 if (value instanceof ArrayReference) { 82 return new JDIArrayValue(target, (ArrayReference)value); 83 } 84 if (value instanceof ClassObjectReference) { 85 return new JDIClassObjectValue(target,(ClassObjectReference)value); 86 } 87 if (value instanceof ObjectReference) { 88 return new JDIObjectValue(target, (ObjectReference)value); 89 } 90 if (value instanceof PrimitiveValue) { 91 return new JDIPrimitiveValue(target, value); 92 } 93 return new JDIValue(target, value); 94 } 95 96 99 public String getValueString() throws DebugException { 100 if (fValue == null) { 101 return JDIDebugModelMessages.JDIValue_null_4; 102 } 103 if (fValue instanceof StringReference) { 104 try { 105 return ((StringReference) fValue).value(); 106 } catch (ObjectCollectedException e) { 107 return JDIDebugModelMessages.JDIValue_deallocated; 108 } catch (RuntimeException e) { 109 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIValue_exception_retrieving_value, new String [] {e.toString()}), e); 110 return null; 113 } 114 } 115 if (fValue instanceof ObjectReference) { 116 StringBuffer name= new StringBuffer (); 117 if (fValue instanceof ClassObjectReference) { 118 name.append('('); 119 name.append(((ClassObjectReference)fValue).reflectedType()); 120 name.append(')'); 121 } 122 name.append(" ("); name.append(JDIDebugModelMessages.JDIValue_id_8); 124 name.append('='); 125 try { 126 name.append(((ObjectReference)fValue).uniqueID()); 127 } catch (RuntimeException e) { 128 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIValue_exception_retrieving_unique_id, new String [] {e.toString()}), e); 129 return null; 132 } 133 name.append(')'); 134 return name.toString(); 135 } 136 return String.valueOf(fValue); 138 } 139 140 143 public String getReferenceTypeName() throws DebugException { 144 try { 145 if (fValue == null) { 146 return JDIDebugModelMessages.JDIValue_null_4; 147 } 148 return getUnderlyingType().name(); 149 } catch (RuntimeException e) { 150 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIValue_exception_retrieving_reference_type_name, new String [] {e.toString()}), e); 151 return null; 154 } 155 } 156 157 160 public int hashCode() { 161 if (fValue == null) { 162 return getClass().hashCode(); 163 } 164 return fValue.hashCode(); 165 } 166 167 170 public boolean equals(Object o) { 171 if (this == o) { 172 return true; 173 } 174 if (o instanceof JDIValue) { 175 Value other = ((JDIValue)o).getUnderlyingValue(); 176 if (fValue == null) { 177 return false; 178 } 179 if (other == null) { 180 return false; 181 } 182 return fValue.equals(other); 183 } 184 return false; 185 } 186 187 190 public IVariable[] getVariables() throws DebugException { 191 List list = getVariablesList(); 192 return (IVariable[])list.toArray(new IVariable[list.size()]); 193 } 194 195 201 protected synchronized List getVariablesList() throws DebugException { 202 if (fVariables != null) { 203 return fVariables; 204 } else 205 if (fValue instanceof ObjectReference) { 206 ObjectReference object= (ObjectReference) fValue; 207 fVariables= new ArrayList (); 208 if (isArray()) { 209 try { 210 int length= getArrayLength(); 211 for (int i = 0; i < length; i++) { 212 fVariables.add(new JDIArrayEntryVariable(getJavaDebugTarget(), getArrayReference(), i)); 213 } 214 } catch (DebugException e) { 215 if (e.getCause() instanceof ObjectCollectedException) { 216 return Collections.EMPTY_LIST; 217 } 218 throw e; 219 } 220 } else { 221 List fields= null; 222 try { 223 ReferenceType refType= object.referenceType(); 224 fields= refType.allFields(); 225 } catch (ObjectCollectedException e) { 226 return Collections.EMPTY_LIST; 227 } catch (RuntimeException e) { 228 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIValue_exception_retrieving_fields, new String [] {e.toString()}), e); 229 return null; 232 } 233 Iterator list= fields.iterator(); 234 while (list.hasNext()) { 235 Field field= (Field) list.next(); 236 fVariables.add(new JDIFieldVariable((JDIDebugTarget)getDebugTarget(), field, object)); 237 } 238 Collections.sort(fVariables, new Comparator () { 239 public int compare(Object a, Object b) { 240 return sortChildren(a, b); 241 } 242 }); 243 } 244 245 return fVariables; 246 } else { 247 return Collections.EMPTY_LIST; 248 } 249 } 250 251 255 protected int sortChildren(Object a, Object b) { 256 IJavaVariable v1= (IJavaVariable)a; 257 IJavaVariable v2= (IJavaVariable)b; 258 259 try { 260 boolean v1isStatic= v1.isStatic(); 261 boolean v2isStatic= v2.isStatic(); 262 if (v1isStatic && !v2isStatic) { 263 return -1; 264 } 265 if (!v1isStatic && v2isStatic) { 266 return 1; 267 } 268 return v1.getName().compareToIgnoreCase(v2.getName()); 269 } catch (DebugException de) { 270 logError(de); 271 return -1; 272 } 273 } 274 275 278 protected boolean isArray() { 279 return fValue instanceof ArrayReference; 280 } 281 282 285 protected ArrayReference getArrayReference() { 286 if (isArray()) { 287 return (ArrayReference)fValue; 288 } 289 return null; 290 } 291 292 295 public boolean isAllocated() throws DebugException { 296 if (fAllocated) { 297 if (fValue instanceof ObjectReference) { 298 try { 299 fAllocated = !((ObjectReference)fValue).isCollected(); 300 } catch (VMDisconnectedException e) { 301 fAllocated = false; 303 } catch (RuntimeException e) { 304 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIValue_exception_is_collected, new String [] {e.toString()}), e); 305 } 308 } else { 309 JDIDebugTarget dt = (JDIDebugTarget)getDebugTarget(); 310 fAllocated = dt.isAvailable(); 311 } 312 } 313 return fAllocated; 314 } 315 316 319 public String getSignature() throws DebugException { 320 try { 321 if (fValue != null) { 322 return fValue.type().signature(); 323 } 324 return null; 325 } catch (RuntimeException e) { 326 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIValue_exception_retrieving_type_signature, new String [] {e.toString()}), e); 327 return null; 330 } 331 } 332 333 336 public String getGenericSignature() throws DebugException { 337 try { 338 if (fValue != null) { 339 Type type= fValue.type(); 340 if (type instanceof ReferenceType) { 341 return ((ReferenceType)type).genericSignature(); 342 } 343 return null; 344 } 345 return null; 346 } catch (RuntimeException e) { 347 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIValue_exception_retrieving_type_signature, new String [] {e.toString()}), e); 348 return null; 351 } 352 } 353 354 357 public int getArrayLength() throws DebugException { 358 if (isArray()) { 359 try { 360 return getArrayReference().length(); 361 } catch (RuntimeException e) { 362 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIValue_exception_retrieving_length_of_array, new String [] {e.toString()}), e); 363 } 364 } 365 return -1; 366 } 367 368 371 protected Value getUnderlyingValue() { 372 return fValue; 373 } 374 375 378 public IJavaType getJavaType() throws DebugException { 379 return JDIType.createType((JDIDebugTarget)getDebugTarget(), getUnderlyingType()); 380 } 381 382 391 protected Type getUnderlyingType() throws DebugException { 392 try { 393 return getUnderlyingValue().type(); 394 } catch (RuntimeException e) { 395 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIValue_exception_retrieving_type, new String [] {e.toString()}), e); 396 return null; 399 } 400 } 401 402 405 public String toString() { 406 return getUnderlyingValue().toString(); 407 } 408 411 public boolean hasVariables() throws DebugException { 412 return getVariablesList().size() > 0; 413 } 414 415 } 416 | Popular Tags |