comparison agent/src/share/classes/sun/jvm/hotspot/jdi/ArrayReferenceImpl.java @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children c18cbe5936b8
comparison
equal deleted inserted replaced
-1:000000000000 0:a61af66fc99e
1 /*
2 * Copyright 2002-2004 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25 package sun.jvm.hotspot.jdi;
26
27 import com.sun.jdi.*;
28
29 import java.util.List;
30 import java.util.ArrayList;
31 import java.util.Iterator;
32
33 import sun.jvm.hotspot.oops.Instance;
34 import sun.jvm.hotspot.oops.Array;
35 import sun.jvm.hotspot.runtime.BasicType;
36 import sun.jvm.hotspot.utilities.Assert;
37
38 public class ArrayReferenceImpl extends ObjectReferenceImpl
39 implements ArrayReference
40 {
41 private int length;
42 ArrayReferenceImpl(VirtualMachine aVm, sun.jvm.hotspot.oops.Array aRef) {
43 super(aVm, aRef);
44 length = (int) aRef.getLength();
45 }
46
47 ArrayTypeImpl arrayType() {
48 return (ArrayTypeImpl)type();
49 }
50
51 /**
52 * Return array length.
53 */
54 public int length() {
55 return length;
56 }
57
58 public Value getValue(int index) {
59 List list = getValues(index, 1);
60 return (Value)list.get(0);
61 }
62
63 public List getValues() {
64 return getValues(0, -1);
65 }
66
67 /**
68 * Validate that the range to set/get is valid.
69 * length of -1 (meaning rest of array) has been converted
70 * before entry.
71 */
72 private void validateArrayAccess(int index, int len) {
73 // because length can be computed from index,
74 // index must be tested first for correct error message
75 if ((index < 0) || (index > length())) {
76 throw new IndexOutOfBoundsException(
77 "Invalid array index: " + index);
78 }
79 if (len < 0) {
80 throw new IndexOutOfBoundsException(
81 "Invalid array range length: " + len);
82 }
83 if (index + len > length()) {
84 throw new IndexOutOfBoundsException(
85 "Invalid array range: " +
86 index + " to " + (index + len - 1));
87 }
88 }
89
90 public List getValues(int index, int len) {
91 if (len == -1) { // -1 means the rest of the array
92 len = length() - index;
93 }
94 validateArrayAccess(index, len);
95 List vals = new ArrayList();
96 if (len == 0) {
97 return vals;
98 }
99
100 sun.jvm.hotspot.oops.TypeArray typeArray = null;
101 sun.jvm.hotspot.oops.ObjArray objArray = null;
102 if (ref() instanceof sun.jvm.hotspot.oops.TypeArray) {
103 typeArray = (sun.jvm.hotspot.oops.TypeArray)ref();
104 } else if (ref() instanceof sun.jvm.hotspot.oops.ObjArray) {
105 objArray = (sun.jvm.hotspot.oops.ObjArray)ref();
106 } else {
107 throw new RuntimeException("should not reach here");
108 }
109
110 char c = arrayType().componentSignature().charAt(0);
111 BasicType variableType = BasicType.charToBasicType(c);
112
113 final int limit = index + len;
114 for (int ii = index; ii < limit; ii++) {
115 ValueImpl valueImpl;
116 if (variableType == BasicType.T_BOOLEAN) {
117 valueImpl = (BooleanValueImpl) vm.mirrorOf(typeArray.getBooleanAt(ii));
118 } else if (variableType == BasicType.T_CHAR) {
119 valueImpl = (CharValueImpl) vm.mirrorOf(typeArray.getCharAt(ii));
120 } else if (variableType == BasicType.T_FLOAT) {
121 valueImpl = (FloatValueImpl) vm.mirrorOf(typeArray.getFloatAt(ii));
122 } else if (variableType == BasicType.T_DOUBLE) {
123 valueImpl = (DoubleValueImpl) vm.mirrorOf(typeArray.getDoubleAt(ii));
124 } else if (variableType == BasicType.T_BYTE) {
125 valueImpl = (ByteValueImpl) vm.mirrorOf(typeArray.getByteAt(ii));
126 } else if (variableType == BasicType.T_SHORT) {
127 valueImpl = (ShortValueImpl) vm.mirrorOf(typeArray.getShortAt(ii));
128 } else if (variableType == BasicType.T_INT) {
129 valueImpl = (IntegerValueImpl) vm.mirrorOf(typeArray.getIntAt(ii));
130 } else if (variableType == BasicType.T_LONG) {
131 valueImpl = (LongValueImpl) vm.mirrorOf(typeArray.getLongAt(ii));
132 } else if (variableType == BasicType.T_OBJECT) {
133 // we may have an [Ljava/lang/Object; - i.e., Object[] with the
134 // elements themselves may be arrays because every array is an Object.
135 valueImpl = (ObjectReferenceImpl) vm.objectMirror(objArray.getObjAt(ii));
136 } else if (variableType == BasicType.T_ARRAY) {
137 valueImpl = (ArrayReferenceImpl) vm.arrayMirror((Array) objArray.getObjAt(ii));
138 } else {
139 throw new RuntimeException("should not reach here");
140 }
141 vals.add (valueImpl);
142 }
143 return vals;
144 }
145
146 public void setValue(int index, Value value)
147 throws InvalidTypeException,
148 ClassNotLoadedException {
149 vm.throwNotReadOnlyException("ArrayReference.setValue(...)");
150 }
151
152 public void setValues(List values)
153 throws InvalidTypeException,
154 ClassNotLoadedException {
155 setValues(0, values, 0, -1);
156 }
157
158 public void setValues(int index, List values,
159 int srcIndex, int length)
160 throws InvalidTypeException,
161 ClassNotLoadedException {
162
163 vm.throwNotReadOnlyException("ArrayReference.setValue(...)");
164
165 }
166
167 public String toString() {
168 return "instance of " + arrayType().componentTypeName() +
169 "[" + length() + "] (id=" + uniqueID() + ")";
170 }
171 }