KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > core > model > JDIValue


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.core.model;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Comparator JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
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 /**
39  * Represents the value of a java variable
40  *
41  * @see IJavaValue
42  */

43 public class JDIValue extends JDIDebugElement implements IJavaValue {
44     
45     private Value fValue;
46     private List JavaDoc fVariables;
47     
48     /**
49      * A flag indicating if this value is still allocated (valid)
50      */

51     private boolean fAllocated = true;
52     
53     /**
54      * Constructor
55      * @param target debug target that this value belongs to
56      * @param value the underlying value this value represents
57      */

58     public JDIValue(JDIDebugTarget target, Value value) {
59         super(target);
60         fValue = value;
61     }
62     
63     /* (non-Javadoc)
64      * @see org.eclipse.jdt.internal.debug.core.model.JDIDebugElement#getAdapter(java.lang.Class)
65      */

66     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
67         if (adapter == IJavaValue.class) {
68             return this;
69         }
70         return super.getAdapter(adapter);
71     }
72     
73     /**
74      * Creates the appropriate kind of value - i.e. a primitive
75      * value, object, class object, array, or <code>null</code>.
76      */

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     /**
97      * @see IValue#getValueString()
98      */

99     public String JavaDoc 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 JavaDoc e) {
109                 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIValue_exception_retrieving_value, new String JavaDoc[] {e.toString()}), e);
110                 // execution will not reach this line, as
111
// #targetRequestFailed will thrown an exception
112
return null;
113             }
114         }
115         if (fValue instanceof ObjectReference) {
116             StringBuffer JavaDoc name= new StringBuffer JavaDoc();
117             if (fValue instanceof ClassObjectReference) {
118                 name.append('(');
119                 name.append(((ClassObjectReference)fValue).reflectedType());
120                 name.append(')');
121             }
122             name.append(" ("); //$NON-NLS-1$
123
name.append(JDIDebugModelMessages.JDIValue_id_8);
124             name.append('=');
125             try {
126                 name.append(((ObjectReference)fValue).uniqueID());
127             } catch (RuntimeException JavaDoc e) {
128                 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIValue_exception_retrieving_unique_id, new String JavaDoc[] {e.toString()}), e);
129                 // execution will not reach this line, as
130
// #targetRequestFailed will thrown an exception
131
return null;
132             }
133             name.append(')');
134             return name.toString();
135         }
136         // see bug 43285
137
return String.valueOf(fValue);
138     }
139     
140     /**
141      * @see IValue#getReferenceTypeName()
142      */

143     public String JavaDoc getReferenceTypeName() throws DebugException {
144         try {
145             if (fValue == null) {
146                 return JDIDebugModelMessages.JDIValue_null_4;
147             }
148             return getUnderlyingType().name();
149         } catch (RuntimeException JavaDoc e) {
150             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIValue_exception_retrieving_reference_type_name, new String JavaDoc[] {e.toString()}), e);
151             // execution will not reach this line, as
152
// #targetRequestFailed will thrown an exception
153
return null;
154         }
155     }
156
157     /**
158      * @see Object#hashCode()
159      */

160     public int hashCode() {
161         if (fValue == null) {
162             return getClass().hashCode();
163         }
164         return fValue.hashCode();
165     }
166
167     /**
168      * @see Object#equals(Object)
169      */

170     public boolean equals(Object JavaDoc 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     /**
188      * @see IValue#getVariables()
189      */

190     public IVariable[] getVariables() throws DebugException {
191         List JavaDoc list = getVariablesList();
192         return (IVariable[])list.toArray(new IVariable[list.size()]);
193     }
194     
195     /**
196      * Returns a list of variables that are children of this value. The result is cached.
197      *
198      * @return list of variable children
199      * @throws DebugException
200      */

201     protected synchronized List JavaDoc 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 JavaDoc();
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 JavaDoc 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 JavaDoc e) {
228                         targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIValue_exception_retrieving_fields, new String JavaDoc[] {e.toString()}), e);
229                         // execution will not reach this line, as
230
// #targetRequestFailed will thrown an exception
231
return null;
232                     }
233                     Iterator JavaDoc 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 JavaDoc() {
239                         public int compare(Object JavaDoc a, Object JavaDoc b) {
240                             return sortChildren(a, b);
241                         }
242                     });
243                 }
244                 
245                 return fVariables;
246             } else {
247                 return Collections.EMPTY_LIST;
248             }
249     }
250     
251     /**
252      * Group statics and instance variables,
253      * sort alphabetically within each group.
254      */

255     protected int sortChildren(Object JavaDoc a, Object JavaDoc 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     /**
276      * Returns whether this value is an array
277      */

278     protected boolean isArray() {
279         return fValue instanceof ArrayReference;
280     }
281     
282     /**
283      * Returns this value as an array reference, or <code>null</code>
284      */

285     protected ArrayReference getArrayReference() {
286         if (isArray()) {
287             return (ArrayReference)fValue;
288         }
289         return null;
290     }
291
292     /**
293      * @see IValue#isAllocated()
294      */

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                     // if the VM disconnects, this value is not allocated
302
fAllocated = false;
303                 } catch (RuntimeException JavaDoc e) {
304                     targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIValue_exception_is_collected, new String JavaDoc[] {e.toString()}), e);
305                     // execution will fall through, as
306
// #targetRequestFailed will thrown an exception
307
}
308             } else {
309                 JDIDebugTarget dt = (JDIDebugTarget)getDebugTarget();
310                 fAllocated = dt.isAvailable();
311             }
312         }
313         return fAllocated;
314     }
315     
316     /**
317      * @see IJavaValue#getSignature()
318      */

319     public String JavaDoc getSignature() throws DebugException {
320         try {
321             if (fValue != null) {
322                 return fValue.type().signature();
323             }
324             return null;
325         } catch (RuntimeException JavaDoc e) {
326             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIValue_exception_retrieving_type_signature, new String JavaDoc[] {e.toString()}), e);
327             // execution will not reach this line, as
328
// #targetRequestFailed will thrown an exception
329
return null;
330         }
331     }
332
333     /* (non-Javadoc)
334      * @see org.eclipse.jdt.debug.core.IJavaValue#getGenericSignature()
335      */

336     public String JavaDoc 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 JavaDoc e) {
347             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIValue_exception_retrieving_type_signature, new String JavaDoc[] {e.toString()}), e);
348             // execution will not reach this line, as
349
// #targetRequestFailed will thrown an exception
350
return null;
351         }
352     }
353     
354     /**
355      * @see IJavaValue#getArrayLength()
356      */

357     public int getArrayLength() throws DebugException {
358         if (isArray()) {
359             try {
360                 return getArrayReference().length();
361             } catch (RuntimeException JavaDoc e) {
362                 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIValue_exception_retrieving_length_of_array, new String JavaDoc[] {e.toString()}), e);
363             }
364         }
365         return -1;
366     }
367     
368     /**
369      * Returns this value's underlying JDI value
370      */

371     protected Value getUnderlyingValue() {
372         return fValue;
373     }
374             
375     /**
376      * @see IJavaValue#getJavaType()
377      */

378     public IJavaType getJavaType() throws DebugException {
379         return JDIType.createType((JDIDebugTarget)getDebugTarget(), getUnderlyingType());
380     }
381     
382     /**
383      * Returns this value's underlying type.
384      *
385      * @return type
386      * @exception DebugException if this method fails. Reasons include:<ul>
387      * <li>Failure communicating with the VM. The DebugException's
388      * status code contains the underlying exception responsible for
389      * the failure.</li>
390      */

391     protected Type getUnderlyingType() throws DebugException {
392         try {
393             return getUnderlyingValue().type();
394         } catch (RuntimeException JavaDoc e) {
395             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIValue_exception_retrieving_type, new String JavaDoc[] {e.toString()}), e);
396             // execution will not fall through to here,
397
// as #requestFailed will throw an exception
398
return null;
399         }
400     }
401
402     /**
403      * @see java.lang.Object#toString()
404      */

405     public String JavaDoc toString() {
406         return getUnderlyingValue().toString();
407     }
408     /**
409      * @see IValue#hasVariables()
410      */

411     public boolean hasVariables() throws DebugException {
412         return getVariablesList().size() > 0;
413     }
414
415 }
416
Popular Tags