KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > core > logicalstructures > JDIAllInstancesValue


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.logicalstructures;
12
13 import org.eclipse.debug.core.DebugException;
14 import org.eclipse.debug.core.model.IVariable;
15 import org.eclipse.jdt.debug.core.IJavaArrayType;
16 import org.eclipse.jdt.debug.core.IJavaObject;
17 import org.eclipse.jdt.debug.core.IJavaType;
18 import org.eclipse.jdt.debug.core.IJavaValue;
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.model.JDIArrayValue;
22 import org.eclipse.jdt.internal.debug.core.model.JDIDebugTarget;
23 import org.eclipse.jdt.internal.debug.core.model.JDIPlaceholderValue;
24 import org.eclipse.jdt.internal.debug.core.model.JDIReferenceType;
25
26 import com.ibm.icu.text.MessageFormat;
27
28 /**
29  * Java value containing an array of java objects. This value is used to hold a list of
30  * all instances of a specific java type.
31  *
32  * @since 3.3
33  * @see org.eclipse.jdt.internal.debug.ui.heapwalking.AllInstancesActionDelegate
34  */

35 public class JDIAllInstancesValue extends JDIArrayValue {
36
37     private IJavaObject[] fInstances;
38     private JDIReferenceType fRoot;
39     private IJavaArrayType fType;
40     private boolean fIsMoreThanPreference;
41     
42     /**
43      * Constructor, specifies whether there are more instances available than should be
44      * displayed according to the user's preference settings.
45      *
46      * @param target the target VM
47      * @param root the root object to get instances for
48      */

49     public JDIAllInstancesValue(JDIDebugTarget target, JDIReferenceType root) {
50         super(target, null);
51         fRoot = root;
52         try {
53             IJavaType[] javaTypes = target.getJavaTypes("java.lang.Object[]"); //$NON-NLS-1$
54
if (javaTypes.length > 0) {
55                 fType = (IJavaArrayType) javaTypes[0];
56             }
57         } catch (DebugException e) {}
58     }
59     
60     /**
61      * @return an array of java objects that are instances of the root type
62      */

63     protected IJavaObject[] getInstances(){
64         if (fInstances != null){
65             return fInstances;
66         } else {
67             IJavaObject[] instances = new IJavaObject[0];
68             fIsMoreThanPreference = false;
69             if (fRoot != null){
70                 int max = HeapWalkingManager.getDefault().getAllInstancesMaxCount();
71                 try{
72                     if (max == 0){
73                         instances = fRoot.getInstances(max);
74                     } else {
75                         instances = fRoot.getInstances(max+1);
76                         if (instances.length > max){
77                             instances[max] = new JDIPlaceholderValue((JDIDebugTarget)fRoot.getDebugTarget(),MessageFormat.format(LogicalStructuresMessages.JDIAllInstancesValue_2,new String JavaDoc[]{Integer.toString(max)}));
78                             fIsMoreThanPreference = true;
79                         }
80                     }
81                 } catch (DebugException e) {
82                     JDIDebugPlugin.log(e);
83                 }
84             }
85             fInstances = instances;
86             return instances;
87         }
88     }
89
90     /* (non-Javadoc)
91      * @see org.eclipse.jdt.internal.debug.core.model.JDIArrayValue#getLength()
92      */

93     public synchronized int getLength() throws DebugException {
94         return getInstances().length;
95     }
96
97     /* (non-Javadoc)
98      * @see org.eclipse.jdt.internal.debug.core.model.JDIArrayValue#getSize()
99      */

100     public int getSize() throws DebugException {
101         return getInstances().length;
102     }
103
104     /* (non-Javadoc)
105      * @see org.eclipse.jdt.internal.debug.core.model.JDIArrayValue#getValue(int)
106      */

107     public IJavaValue getValue(int index) throws DebugException {
108         if(index > getInstances().length-1 || index < 0) {
109             internalError(LogicalStructuresMessages.JDIAllInstancesValue_0);
110         }
111         return getInstances()[index];
112     }
113
114     /* (non-Javadoc)
115      * @see org.eclipse.jdt.internal.debug.core.model.JDIArrayValue#getValues()
116      */

117     public IJavaValue[] getValues() throws DebugException {
118         return getInstances();
119     }
120
121     /* (non-Javadoc)
122      * @see org.eclipse.jdt.internal.debug.core.model.JDIArrayValue#getVariable(int)
123      */

124     public IVariable getVariable(int offset) throws DebugException {
125         if(offset > getInstances().length-1 || offset < 0) {
126             internalError(LogicalStructuresMessages.JDIAllInstancesValue_1);
127         }
128         if(isMoreThanPreference() && offset == getInstances().length-1){
129             return new JDIPlaceholderVariable(LogicalStructuresMessages.JDIAllInstancesValue_4, getInstances()[offset]);
130         } else {
131             return new JDIPlaceholderVariable(MessageFormat.format(LogicalStructuresMessages.JDIAllInstancesValue_5,new String JavaDoc[]{Integer.toString(offset)}), getInstances()[offset]);
132         }
133     }
134
135     /* (non-Javadoc)
136      * @see org.eclipse.jdt.internal.debug.core.model.JDIArrayValue#getVariables(int, int)
137      */

138     public IVariable[] getVariables(int offset, int length) throws DebugException {
139         if (length == 0){
140             return new IVariable[0];
141         }
142         if(offset > getInstances().length-1 || offset < 0) {
143             internalError(LogicalStructuresMessages.JDIAllInstancesValue_1);
144         }
145         IVariable[] vars = new JDIPlaceholderVariable[length];
146         for (int i = 0; i < length; i++) {
147             vars[i] = getVariable(i + offset);
148         }
149         return vars;
150     }
151     
152     /* (non-Javadoc)
153      * @see org.eclipse.jdt.internal.debug.core.model.JDIValue#getVariables()
154      */

155     public IVariable[] getVariables() throws DebugException {
156         return getVariables(0, getInstances().length);
157     }
158
159     /* (non-Javadoc)
160      * @see org.eclipse.jdt.internal.debug.core.model.JDIObjectValue#getReferringObjects(long)
161      */

162     public IJavaObject[] getReferringObjects(long max) throws DebugException {
163         return new IJavaObject[0];
164     }
165     
166     /* (non-Javadoc)
167      * @see org.eclipse.jdt.internal.debug.core.model.JDIValue#isAllocated()
168      */

169     public boolean isAllocated() throws DebugException {
170         return getJavaDebugTarget().isAvailable();
171     }
172     
173     /* (non-Javadoc)
174      * @see org.eclipse.jdt.internal.debug.core.model.JDIArrayValue#getInitialOffset()
175      */

176     public int getInitialOffset() {
177         return 0;
178     }
179     
180     /* (non-Javadoc)
181      * @see org.eclipse.jdt.internal.debug.core.model.JDIArrayValue#hasVariables()
182      */

183     public boolean hasVariables() throws DebugException {
184         return getInstances().length > 0;
185     }
186     
187     /* (non-Javadoc)
188      * @see org.eclipse.jdt.internal.debug.core.model.JDIValue#getJavaType()
189      */

190     public IJavaType getJavaType() throws DebugException {
191         return fType;
192     }
193
194     /* (non-Javadoc)
195      * @see org.eclipse.jdt.internal.debug.core.model.JDIValue#getSignature()
196      */

197     public String JavaDoc getSignature() throws DebugException {
198         return fType.getSignature();
199     }
200
201     /* (non-Javadoc)
202      * @see org.eclipse.jdt.internal.debug.core.model.JDIObjectValue#getReferenceTypeName()
203      */

204     public String JavaDoc getReferenceTypeName() throws DebugException {
205         return fType.getName();
206     }
207
208     /* (non-Javadoc)
209      * @see org.eclipse.jdt.internal.debug.core.model.JDIValue#getValueString()
210      */

211     public String JavaDoc getValueString() throws DebugException {
212         if (isMoreThanPreference()){
213             return MessageFormat.format(LogicalStructuresMessages.JDIAllInstancesValue_7,new String JavaDoc[]{Integer.toString(getInstances().length-1)});
214         } else if (getInstances().length == 1) {
215             return MessageFormat.format(LogicalStructuresMessages.JDIAllInstancesValue_8,new String JavaDoc[]{Integer.toString(getInstances().length)});
216         } else {
217             return MessageFormat.format(LogicalStructuresMessages.JDIAllInstancesValue_9,new String JavaDoc[]{Integer.toString(getInstances().length)});
218         }
219     }
220     
221     /**
222      * Returns a string representation of this value intended to be displayed
223      * in the detail pane of views. Lists the references on separate lines.
224      *
225      * @return a string representation of this value to display in the detail pane
226      */

227     public String JavaDoc getDetailString(){
228         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
229         Object JavaDoc[] elements = getInstances();
230         if (elements.length == 0){
231             buf.append(LogicalStructuresMessages.JDIAllInstancesValue_10);
232         }
233         else{
234             String JavaDoc length = null;
235             if (isMoreThanPreference()){
236                 length = MessageFormat.format(LogicalStructuresMessages.JDIAllInstancesValue_11,new String JavaDoc[]{Integer.toString(elements.length-1)});
237             } else {
238                 length = Integer.toString(elements.length);
239             }
240             if (elements.length == 1){
241                 buf.append(MessageFormat.format(LogicalStructuresMessages.JDIAllInstancesValue_12,new String JavaDoc[]{length}));
242             } else {
243                 buf.append(MessageFormat.format(LogicalStructuresMessages.JDIAllInstancesValue_13,new String JavaDoc[]{length}));
244             }
245             for (int i = 0; i < elements.length; i++) {
246                 buf.append(elements[i] + "\n"); //$NON-NLS-1$
247
}
248         }
249         return buf.toString();
250     }
251     
252     /**
253      * @return whether there are more instances available than should be displayed
254      */

255     protected boolean isMoreThanPreference() {
256         getInstances(); //The instances must be requested to know if there are more than the preference
257
return fIsMoreThanPreference;
258     }
259
260 }
261
Popular Tags