KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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 org.eclipse.debug.core.DebugException;
14 import org.eclipse.debug.core.model.IIndexedValue;
15 import org.eclipse.debug.core.model.IVariable;
16 import org.eclipse.jdt.debug.core.IJavaObject;
17 import org.eclipse.jdt.debug.core.IJavaType;
18 import org.eclipse.jdt.debug.core.IJavaVariable;
19 import org.eclipse.jdt.internal.debug.core.HeapWalkingManager;
20 import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin;
21 import org.eclipse.jdt.internal.debug.core.logicalstructures.JDIPlaceholderVariable;
22
23 import com.ibm.icu.text.MessageFormat;
24 import com.sun.jdi.ObjectReference;
25
26 /**
27  * A JDI Value representing a set of references to the root object specified
28  * in the constructor. Used to add a list of all references to an object to
29  * various views including the variables view. The value should belong to
30  * a <code>JDIReferenceListVariable</code>. The children of this value will
31  * be <code>JDIReferenceListEntryVariable</code>, each representing one reference
32  * to the root object.
33  *
34  * @see JDIReferenceListVariable
35  * @see JDIReferenceListEntryVariable
36  * @since 3.3
37  */

38 public class JDIReferenceListValue extends JDIObjectValue implements IIndexedValue {
39
40     private IJavaObject fRoot;
41     private boolean fIsMoreThanPreference;
42     
43     /**
44      * Constructor, initializes this value with its debug target and root object
45      * @param target The debug target associated with this value
46      * @param root The root object that the elements in the array refer to.
47      */

48     public JDIReferenceListValue(IJavaObject root) {
49         super((JDIDebugTarget)root.getDebugTarget(), ((JDIObjectValue)root).getUnderlyingObject());
50         fRoot = root;
51     }
52     
53     /**
54      * @return all references to the root object as an array of IJavaObjects
55      */

56     protected synchronized IJavaObject[] getReferences(){
57         try{
58             int max = HeapWalkingManager.getDefault().getAllReferencesMaxCount();
59             IJavaObject[] referringObjects = null;
60             fIsMoreThanPreference = false;
61             if (max == 0){
62                 referringObjects = fRoot.getReferringObjects(max);
63             } else {
64                 referringObjects = fRoot.getReferringObjects(max+1);
65                 if (referringObjects.length > max){
66                     fIsMoreThanPreference = true;
67                     referringObjects[max] = new JDIPlaceholderValue((JDIDebugTarget)fRoot.getDebugTarget(),MessageFormat.format(JDIDebugModelMessages.JDIReferenceListValue_9,new String JavaDoc[]{Integer.toString(max)}));
68                 }
69             }
70             return referringObjects;
71         } catch (DebugException e) {
72             JDIDebugPlugin.log(e);
73             return new IJavaObject[0];
74         }
75     }
76     
77     /**
78      * @return whether the references to the root object have been loaded from the vm yet.
79      */

80     protected synchronized boolean referencesLoaded(){
81         if (fRoot instanceof JDIObjectValue){
82             return ((JDIObjectValue)fRoot).isReferencesLoaded();
83         }
84         return false;
85     }
86
87     /* (non-Javadoc)
88      * @see org.eclipse.jdt.internal.debug.core.model.JDIValue#getVariables()
89      */

90     public IVariable[] getVariables() throws DebugException {
91         IJavaObject[] elements = getReferences();
92         IVariable[] vars = new JDIPlaceholderVariable[elements.length];
93         int length = elements.length;
94         if(fIsMoreThanPreference){
95             length--;
96             vars[length] = new JDIPlaceholderVariable(JDIDebugModelMessages.JDIReferenceListValue_11, elements[length]);
97         }
98         
99         for (int i = 0; i < length; i++) {
100             vars[i] = new JDIReferenceListEntryVariable(MessageFormat.format(JDIDebugModelMessages.JDIReferenceListValue_0, new String JavaDoc[]{Integer.toString(i)}),elements[i]);
101         }
102         return vars;
103     }
104     
105     /* (non-Javadoc)
106      * @see org.eclipse.jdt.internal.debug.core.model.JDIObjectValue#getUnderlyingObject()
107      */

108     public ObjectReference getUnderlyingObject() {
109         return null;
110     }
111     
112     /* (non-Javadoc)
113      * @see org.eclipse.jdt.internal.debug.core.model.JDIValue#hasVariables()
114      */

115     public boolean hasVariables() throws DebugException {
116         if (referencesLoaded()){
117             return getReferences().length > 0;
118         }
119         return true;
120     }
121
122     /* (non-Javadoc)
123      * @see org.eclipse.jdt.internal.debug.core.model.JDIValue#isAllocated()
124      */

125     public boolean isAllocated() throws DebugException {
126         return fRoot.isAllocated();
127     }
128     
129     /* (non-Javadoc)
130      * @see org.eclipse.jdt.internal.debug.core.model.JDIValue#getJavaType()
131      */

132     public IJavaType getJavaType() throws DebugException {
133         IJavaType[] javaTypes = getJavaDebugTarget().getJavaTypes(getReferenceTypeName());
134         if (javaTypes.length > 0) {
135             return javaTypes[0];
136         }
137         return null;
138     }
139
140     /* (non-Javadoc)
141      * @see org.eclipse.jdt.internal.debug.core.model.JDIValue#getSignature()
142      */

143     public String JavaDoc getSignature() throws DebugException {
144         return "[Ljava/lang/Object;"; //$NON-NLS-1$
145
}
146
147     /* (non-Javadoc)
148      * @see org.eclipse.jdt.internal.debug.core.model.JDIObjectValue#getReferenceTypeName()
149      */

150     public String JavaDoc getReferenceTypeName() throws DebugException {
151         return "java.lang.Object[]"; //$NON-NLS-1$
152
}
153
154     /* (non-Javadoc)
155      * @see org.eclipse.jdt.internal.debug.core.model.JDIValue#getValueString()
156      */

157     public String JavaDoc getValueString() throws DebugException {
158         return ""; //$NON-NLS-1$
159
}
160     
161     /**
162      * Returns a string representation of this value intended to be displayed
163      * in the detail pane of views. Lists the references on separate lines.
164      *
165      * @return a string representation of this value to display in the detail pane
166      */

167     public String JavaDoc getDetailString(){
168         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
169         Object JavaDoc[] elements = getReferences();
170         if (elements.length == 0){
171             buf.append(JDIDebugModelMessages.JDIReferenceListValue_2);
172         }
173         else{
174             String JavaDoc length = null;
175             if (fIsMoreThanPreference){
176                 length = MessageFormat.format(JDIDebugModelMessages.JDIReferenceListValue_15,new String JavaDoc[]{Integer.toString(elements.length-1)});
177             } else {
178                 length = Integer.toString(elements.length);
179             }
180             if (elements.length == 1){
181                 buf.append(MessageFormat.format(JDIDebugModelMessages.JDIReferenceListValue_3,new String JavaDoc[]{length}));
182             } else {
183                 buf.append(MessageFormat.format(JDIDebugModelMessages.JDIReferenceListValue_4,new String JavaDoc[]{length}));
184             }
185             for (int i = 0; i < elements.length; i++) {
186                 buf.append(elements[i] + "\n"); //$NON-NLS-1$
187
}
188         }
189         return buf.toString();
190     }
191     
192     /* (non-Javadoc)
193      * @see org.eclipse.jdt.internal.debug.core.model.JDIValue#toString()
194      */

195     public String JavaDoc toString() {
196         return MessageFormat.format(JDIDebugModelMessages.JDIReferenceListValue_6,new String JavaDoc[]{getUnderlyingValue().toString()});
197     }
198
199     /* (non-Javadoc)
200      * @see org.eclipse.jdt.internal.debug.core.model.JDIValue#equals(java.lang.Object)
201      */

202     public boolean equals(Object JavaDoc o) {
203         // Two JDIReferenceListValues are equal if they both have the same root object.
204
if (o instanceof JDIReferenceListValue) {
205             JDIReferenceListValue ref = (JDIReferenceListValue) o;
206             return ref.fRoot.equals(fRoot);
207         }
208         return false;
209     }
210
211     /* (non-Javadoc)
212      * @see org.eclipse.jdt.internal.debug.core.model.JDIValue#hashCode()
213      */

214     public int hashCode() {
215         return getClass().hashCode() + fRoot.hashCode();
216     }
217
218     /* (non-Javadoc)
219      * @see org.eclipse.debug.core.model.IIndexedValue#getInitialOffset()
220      */

221     public int getInitialOffset() {
222         return 0;
223     }
224
225     /* (non-Javadoc)
226      * @see org.eclipse.debug.core.model.IIndexedValue#getSize()
227      */

228     public int getSize() throws DebugException {
229         return getVariables().length;
230     }
231
232     /* (non-Javadoc)
233      * @see org.eclipse.debug.core.model.IIndexedValue#getVariable(int)
234      */

235     public IVariable getVariable(int offset) throws DebugException {
236         IVariable[] variables = getVariables();
237         if (offset < variables.length) {
238             return variables[offset];
239         } else {
240             requestFailed(JDIDebugModelMessages.JDIReferenceListValue_7, new IndexOutOfBoundsException JavaDoc());
241             return null;
242         }
243     }
244
245     /* (non-Javadoc)
246      * @see org.eclipse.debug.core.model.IIndexedValue#getVariables(int, int)
247      */

248     public IVariable[] getVariables(int offset, int length) throws DebugException {
249         IVariable[] variables = getVariables();
250         if (offset < variables.length && (offset + length) <= variables.length) {
251             IJavaVariable[] vars = new IJavaVariable[length];
252             System.arraycopy(variables, offset, vars, 0, length);
253             return vars;
254         } else {
255             requestFailed(JDIDebugModelMessages.JDIReferenceListValue_8, new IndexOutOfBoundsException JavaDoc());
256             return null;
257         }
258     }
259
260 }
261
Popular Tags