Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2fb46eea69694bea9b40d842c3d71f326089a38d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*******************************************************************************
 * Copyright (c) 2007, 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Stepan Vavra - Bug 419316 - All References or All instances may throw NPE in Eclipse
 *******************************************************************************/
package org.eclipse.jdt.internal.debug.core.logicalstructures;

import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.IVariable;
import org.eclipse.jdt.debug.core.IJavaArrayType;
import org.eclipse.jdt.debug.core.IJavaObject;
import org.eclipse.jdt.debug.core.IJavaType;
import org.eclipse.jdt.debug.core.IJavaValue;
import org.eclipse.jdt.internal.debug.core.HeapWalkingManager;
import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin;
import org.eclipse.jdt.internal.debug.core.model.JDIArrayValue;
import org.eclipse.jdt.internal.debug.core.model.JDIDebugTarget;
import org.eclipse.jdt.internal.debug.core.model.JDIPlaceholderValue;
import org.eclipse.jdt.internal.debug.core.model.JDIReferenceType;

import com.ibm.icu.text.MessageFormat;

/**
 * Java value containing an array of java objects. This value is used to hold a
 * list of all instances of a specific java type.
 * 
 * @since 3.3
 * @see org.eclipse.jdt.internal.debug.ui.heapwalking.AllInstancesActionDelegate
 */
public class JDIAllInstancesValue extends JDIArrayValue {

	private IJavaObject[] fInstances;
	private JDIReferenceType fRoot;
	private IJavaArrayType fType;
	private boolean fIsMoreThanPreference;

	/**
	 * Constructor, specifies whether there are more instances available than
	 * should be displayed according to the user's preference settings.
	 * 
	 * @param target
	 *            the target VM
	 * @param root
	 *            the root object to get instances for
	 */
	public JDIAllInstancesValue(JDIDebugTarget target, JDIReferenceType root) {
		super(target, null);
		fRoot = root;
		try {
			IJavaType[] javaTypes = target.getJavaTypes("java.lang.Object[]"); //$NON-NLS-1$
			if (javaTypes != null && javaTypes.length > 0) {
				fType = (IJavaArrayType) javaTypes[0];
			}
		} catch (DebugException e) {
		}
	}

	/**
	 * @return an array of java objects that are instances of the root type
	 */
	protected IJavaObject[] getInstances() {
		if (fInstances != null) {
			return fInstances;
		} 
		IJavaObject[] instances = new IJavaObject[0];
		fIsMoreThanPreference = false;
		if (fRoot != null) {
			int max = HeapWalkingManager.getDefault()
					.getAllInstancesMaxCount();
			try {
				if (max == 0) {
					instances = fRoot.getInstances(max);
				} else {
					instances = fRoot.getInstances(max + 1);
					if (instances.length > max) {
						instances[max] = new JDIPlaceholderValue(
								(JDIDebugTarget) fRoot.getDebugTarget(),
								MessageFormat.format(LogicalStructuresMessages.JDIAllInstancesValue_2, Integer.toString(max)));
						fIsMoreThanPreference = true;
					}
				}
			} catch (DebugException e) {
				JDIDebugPlugin.log(e);
			}
		}
		fInstances = instances;
		return instances;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jdt.internal.debug.core.model.JDIArrayValue#getLength()
	 */
	@Override
	public synchronized int getLength() throws DebugException {
		return getInstances().length;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jdt.internal.debug.core.model.JDIArrayValue#getSize()
	 */
	@Override
	public int getSize() throws DebugException {
		return getInstances().length;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jdt.internal.debug.core.model.JDIArrayValue#getValue(int)
	 */
	@Override
	public IJavaValue getValue(int index) throws DebugException {
		if (index > getInstances().length - 1 || index < 0) {
			internalError(LogicalStructuresMessages.JDIAllInstancesValue_0);
		}
		return getInstances()[index];
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jdt.internal.debug.core.model.JDIArrayValue#getValues()
	 */
	@Override
	public IJavaValue[] getValues() throws DebugException {
		return getInstances();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jdt.internal.debug.core.model.JDIArrayValue#getVariable(int)
	 */
	@Override
	public IVariable getVariable(int offset) throws DebugException {
		if (offset > getInstances().length - 1 || offset < 0) {
			internalError(LogicalStructuresMessages.JDIAllInstancesValue_1);
		}
		if (isMoreThanPreference() && offset == getInstances().length - 1) {
			return new JDIPlaceholderVariable(
					LogicalStructuresMessages.JDIAllInstancesValue_4,
					getInstances()[offset]);
		}
		return new JDIPlaceholderVariable(MessageFormat.format(LogicalStructuresMessages.JDIAllInstancesValue_5,
				Integer.toString(offset)),
				getInstances()[offset]);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jdt.internal.debug.core.model.JDIArrayValue#getVariables(int,
	 * int)
	 */
	@Override
	public IVariable[] getVariables(int offset, int length)
			throws DebugException {
		if (length == 0) {
			return new IVariable[0];
		}
		if (offset > getInstances().length - 1 || offset < 0) {
			internalError(LogicalStructuresMessages.JDIAllInstancesValue_1);
		}
		IVariable[] vars = new JDIPlaceholderVariable[length];
		for (int i = 0; i < length; i++) {
			vars[i] = getVariable(i + offset);
		}
		return vars;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jdt.internal.debug.core.model.JDIValue#getVariables()
	 */
	@Override
	public IVariable[] getVariables() throws DebugException {
		return getVariables(0, getInstances().length);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jdt.internal.debug.core.model.JDIObjectValue#getReferringObjects
	 * (long)
	 */
	@Override
	public IJavaObject[] getReferringObjects(long max) throws DebugException {
		return new IJavaObject[0];
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jdt.internal.debug.core.model.JDIValue#isAllocated()
	 */
	@Override
	public boolean isAllocated() throws DebugException {
		return getJavaDebugTarget().isAvailable();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jdt.internal.debug.core.model.JDIArrayValue#getInitialOffset
	 * ()
	 */
	@Override
	public int getInitialOffset() {
		return 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jdt.internal.debug.core.model.JDIArrayValue#hasVariables()
	 */
	@Override
	public boolean hasVariables() throws DebugException {
		return getInstances().length > 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jdt.internal.debug.core.model.JDIValue#getJavaType()
	 */
	@Override
	public IJavaType getJavaType() throws DebugException {
		return fType;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jdt.internal.debug.core.model.JDIValue#getSignature()
	 */
	@Override
	public String getSignature() throws DebugException {
		return fType.getSignature();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jdt.internal.debug.core.model.JDIObjectValue#getReferenceTypeName
	 * ()
	 */
	@Override
	public String getReferenceTypeName() throws DebugException {
		return fType.getName();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jdt.internal.debug.core.model.JDIValue#getValueString()
	 */
	@Override
	public String getValueString() throws DebugException {
		if (isMoreThanPreference()) {
			return MessageFormat.format(LogicalStructuresMessages.JDIAllInstancesValue_7,
							Integer.toString(getInstances().length - 1));
		} else if (getInstances().length == 1) {
			return MessageFormat.format(LogicalStructuresMessages.JDIAllInstancesValue_8,
					Integer.toString(getInstances().length));
		} else {
			return MessageFormat.format(LogicalStructuresMessages.JDIAllInstancesValue_9,
					Integer.toString(getInstances().length));
		}
	}

	/**
	 * Returns a string representation of this value intended to be displayed in
	 * the detail pane of views. Lists the references on separate lines.
	 * 
	 * @return a string representation of this value to display in the detail
	 *         pane
	 */
	public String getDetailString() {
		StringBuffer buf = new StringBuffer();
		Object[] elements = getInstances();
		if (elements.length == 0) {
			buf.append(LogicalStructuresMessages.JDIAllInstancesValue_10);
		} else {
			String length = null;
			if (isMoreThanPreference()) {
				length = MessageFormat.format(LogicalStructuresMessages.JDIAllInstancesValue_11,
						Integer.toString(elements.length - 1));
			} else {
				length = Integer.toString(elements.length);
			}
			if (elements.length == 1) {
				buf.append(MessageFormat.format(LogicalStructuresMessages.JDIAllInstancesValue_12, length));
			} else {
				buf.append(MessageFormat.format(LogicalStructuresMessages.JDIAllInstancesValue_13, length));
			}
			for (Object element : elements) {
				buf.append(element + "\n"); //$NON-NLS-1$
			}
		}
		return buf.toString();
	}

	/**
	 * @return whether there are more instances available than should be
	 *         displayed
	 */
	protected boolean isMoreThanPreference() {
		getInstances(); // The instances must be requested to know if there are
						// more than the preference
		return fIsMoreThanPreference;
	}

}

Back to the top