1 11 package org.eclipse.jdi.internal; 12 13 14 import java.io.ByteArrayOutputStream ; 15 import java.io.DataInputStream ; 16 import java.io.DataOutputStream ; 17 import java.io.IOException ; 18 import java.util.ArrayList ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 22 import org.eclipse.jdi.internal.jdwp.JdwpCommandPacket; 23 import org.eclipse.jdi.internal.jdwp.JdwpID; 24 import org.eclipse.jdi.internal.jdwp.JdwpObjectID; 25 import org.eclipse.jdi.internal.jdwp.JdwpReplyPacket; 26 27 import com.sun.jdi.ArrayReference; 28 import com.sun.jdi.ClassNotLoadedException; 29 import com.sun.jdi.InternalException; 30 import com.sun.jdi.InvalidTypeException; 31 import com.sun.jdi.ObjectCollectedException; 32 import com.sun.jdi.Type; 33 import com.sun.jdi.Value; 34 35 41 public class ArrayReferenceImpl extends ObjectReferenceImpl implements ArrayReference { 42 43 public static final byte tag = JdwpID.ARRAY_TAG; 44 45 private int fLength = -1; 46 47 50 public ArrayReferenceImpl(VirtualMachineImpl vmImpl, JdwpObjectID objectID) { 51 super("ArrayReference", vmImpl, objectID); } 53 54 57 public byte getTag() { 58 return tag; 59 } 60 61 64 public Value getValue(int index) throws IndexOutOfBoundsException { 65 return (Value)getValues(index, 1).get(0); 66 } 67 68 71 public List getValues() { 72 return getValues(0, -1); 73 } 74 75 78 public List getValues(int firstIndex, int length) throws IndexOutOfBoundsException { 79 80 int arrayLength= length(); 81 82 if (firstIndex < 0 || firstIndex >= arrayLength) { 83 throw new IndexOutOfBoundsException (JDIMessages.ArrayReferenceImpl_Invalid_index_1); 84 } 85 86 if (length == -1) { 87 length = arrayLength - firstIndex; 89 } else if (length < -1) { 90 throw new IndexOutOfBoundsException (JDIMessages.ArrayReferenceImpl_Invalid_number_of_value_to_get_from_array_1); 91 } else if (firstIndex + length > arrayLength) { 92 throw new IndexOutOfBoundsException (JDIMessages.ArrayReferenceImpl_Attempted_to_get_more_values_from_array_than_length_of_array_2); 93 } 94 95 initJdwpRequest(); 97 try { 98 ByteArrayOutputStream outBytes = new ByteArrayOutputStream (); 99 DataOutputStream outData = new DataOutputStream (outBytes); 100 write(this, outData); writeInt(firstIndex, "firstIndex", outData); writeInt(length, "length", outData); 104 JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.AR_GET_VALUES, outBytes); 105 switch (replyPacket.errorCode()) { 106 case JdwpReplyPacket.INVALID_INDEX: 107 throw new IndexOutOfBoundsException (JDIMessages.ArrayReferenceImpl_Invalid_index_of_array_reference_given_1); 108 } 109 defaultReplyErrorHandler(replyPacket.errorCode()); 110 111 DataInputStream replyData = replyPacket.dataInStream(); 112 113 118 119 int type = readByte("type", JdwpID.tagMap(), replyData); int readLength = readInt("length", replyData); switch(type) { 123 case ArrayReferenceImpl.tag: 125 case ClassLoaderReferenceImpl.tag: 127 case ClassObjectReferenceImpl.tag: 128 case StringReferenceImpl.tag: 129 case ObjectReferenceImpl.tag: 130 case ThreadGroupReferenceImpl.tag: 131 case ThreadReferenceImpl.tag: 132 return readObjectSequence(readLength, replyData); 133 134 case BooleanValueImpl.tag: 136 case ByteValueImpl.tag: 137 case CharValueImpl.tag: 138 case DoubleValueImpl.tag: 139 case FloatValueImpl.tag: 140 case IntegerValueImpl.tag: 141 case LongValueImpl.tag: 142 case ShortValueImpl.tag: 143 return readPrimitiveSequence(readLength, type, replyData); 144 145 case VoidValueImpl.tag: 146 case 0: 147 default: 148 throw new InternalException(JDIMessages.ArrayReferenceImpl_Invalid_ArrayReference_Value_tag_encountered___2 + type); 149 } 150 } catch (IOException e) { 151 defaultIOExceptionHandler(e); 152 return null; 153 } finally { 154 handledJdwpRequest(); 155 } 156 } 157 158 161 private List readObjectSequence(int length, DataInputStream in) throws IOException { 162 List elements = new ArrayList (length); 163 for (int i = 0; i < length; i++) { 164 ValueImpl value = ObjectReferenceImpl.readObjectRefWithTag(this, in); 165 elements.add(value); 166 } 167 return elements; 168 } 169 170 173 private List readPrimitiveSequence(int length, int type, DataInputStream in) throws IOException { 174 List elements = new ArrayList (length); 175 for (int i = 0; i < length; i++) { 176 ValueImpl value = ValueImpl.readWithoutTag(this, type, in); 177 elements.add(value); 178 } 179 return elements; 180 } 181 182 185 public int length() { 186 if (fLength == -1) { 187 initJdwpRequest(); 188 try { 189 JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.AR_LENGTH, this); 190 defaultReplyErrorHandler(replyPacket.errorCode()); 191 DataInputStream replyData = replyPacket.dataInStream(); 192 fLength = readInt("length", replyData); } catch (IOException e) { 194 defaultIOExceptionHandler(e); 195 return 0; 196 } finally { 197 handledJdwpRequest(); 198 } 199 } 200 return fLength; 201 } 202 203 206 public void setValue(int index, Value value) throws InvalidTypeException, ClassNotLoadedException { 207 ArrayList list = new ArrayList (1); 208 list.add(value); 209 setValues(index, list, 0, 1); 210 } 211 212 215 public void setValues(List values) throws InvalidTypeException, ClassNotLoadedException { 216 setValues(0, values, 0, -1); 217 } 218 219 222 public void setValues(int index, List values, int srcIndex, int length) throws InvalidTypeException, ClassNotLoadedException { 223 224 int valuesSize= values.size(); 225 int arrayLength= length(); 226 227 if (index < 0 || index >= arrayLength) { 228 throw new IndexOutOfBoundsException (JDIMessages.ArrayReferenceImpl_Invalid_index_1); 229 } 230 if (srcIndex < 0 || srcIndex >= valuesSize) { 231 throw new IndexOutOfBoundsException (JDIMessages.ArrayReferenceImpl_Invalid_srcIndex_2); 232 } 233 234 if (length < -1) { 235 throw new IndexOutOfBoundsException (JDIMessages.ArrayReferenceImpl_Invalid_number_of_value_to_set_in_array_3); 236 } else if (length == -1) { 237 length = arrayLength - index; 239 int lengthTmp= valuesSize - srcIndex; 240 if (lengthTmp < length) { 241 length= lengthTmp; 242 } 243 } else if (index + length > arrayLength) { 244 throw new IndexOutOfBoundsException (JDIMessages.ArrayReferenceImpl_Attempted_to_set_more_values_in_array_than_length_of_array_3); 245 } else if (srcIndex + length > valuesSize) { 246 throw new IndexOutOfBoundsException (JDIMessages.ArrayReferenceImpl_Attempted_to_set_more_values_in_array_than_given_4); 248 } 249 250 List checkedValues= checkValues(values.subList(srcIndex, srcIndex + length), ((ArrayTypeImpl) referenceType()).componentType()); 252 253 initJdwpRequest(); 255 try { 256 ByteArrayOutputStream outBytes = new ByteArrayOutputStream (); 257 DataOutputStream outData = new DataOutputStream (outBytes); 258 write(this, outData); 259 writeInt(index, "index", outData); writeInt(length, "length", outData); Iterator iterValues= checkedValues.iterator(); 262 while (iterValues.hasNext()) { 263 ValueImpl value= (ValueImpl)iterValues.next(); 264 if (value != null) { 265 value.write(this, outData); 266 } else { 267 ValueImpl.writeNull(this, outData); 268 } 269 } 270 271 JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.AR_SET_VALUES, outBytes); 272 switch (replyPacket.errorCode()) { 273 case JdwpReplyPacket.TYPE_MISMATCH: 274 throw new InvalidTypeException(); 275 case JdwpReplyPacket.INVALID_CLASS: 276 throw new ClassNotLoadedException(type().name()); 277 } 278 defaultReplyErrorHandler(replyPacket.errorCode()); 279 } catch (IOException e) { 280 defaultIOExceptionHandler(e); 281 } finally { 282 handledJdwpRequest(); 283 } 284 } 285 286 291 private List checkValues(List values, Type type) throws InvalidTypeException { 292 List checkedValues= new ArrayList (values.size()); 293 Iterator iterValues= values.iterator(); 294 while (iterValues.hasNext()) { 295 checkedValues.add(ValueImpl.checkValue((Value) iterValues.next(), type, virtualMachineImpl())); 296 } 297 return checkedValues; 298 } 299 300 303 public String toString() { 304 try { 305 StringBuffer buf = new StringBuffer (type().name()); 306 buf.insert(buf.length() - 1, length()); 308 buf.append(' '); 310 buf.append(idString()); 311 return buf.toString(); 312 } catch (ObjectCollectedException e) { 313 return JDIMessages.ArrayReferenceImpl__Garbage_Collected__ArrayReference_5 + "[" + length() + "] " + idString(); } catch (Exception e) { 315 return fDescription; 316 } 317 } 318 319 322 public static ArrayReferenceImpl read(MirrorImpl target, DataInputStream in) throws IOException { 323 VirtualMachineImpl vmImpl = target.virtualMachineImpl(); 324 JdwpObjectID ID = new JdwpObjectID(vmImpl); 325 ID.read(in); 326 if (target.fVerboseWriter != null) { 327 target.fVerboseWriter.println("arrayReference", ID.value()); } 329 330 if (ID.isNull()) { 331 return null; 332 } 333 334 ArrayReferenceImpl mirror = new ArrayReferenceImpl(vmImpl, ID); 335 return mirror; 336 } 337 } 338 | Popular Tags |