1 11 package org.eclipse.jdi.internal; 12 13 14 import java.io.DataInputStream ; 15 import java.io.DataOutputStream ; 16 import java.io.IOException ; 17 import java.util.ArrayList ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 21 import org.eclipse.jdi.internal.jdwp.JdwpID; 22 import org.eclipse.jdi.internal.jdwp.JdwpObjectID; 23 24 import com.sun.jdi.ArrayType; 25 import com.sun.jdi.ClassNotLoadedException; 26 import com.sun.jdi.ClassType; 27 import com.sun.jdi.InterfaceType; 28 import com.sun.jdi.InternalException; 29 import com.sun.jdi.InvalidTypeException; 30 import com.sun.jdi.PrimitiveType; 31 import com.sun.jdi.ReferenceType; 32 import com.sun.jdi.Type; 33 import com.sun.jdi.Value; 34 import com.sun.jdi.VoidType; 35 36 42 public abstract class ValueImpl extends MirrorImpl implements Value { 43 46 protected ValueImpl(String description, VirtualMachineImpl vmImpl) { 47 super(description, vmImpl); 48 } 49 50 53 public abstract Type type(); 54 55 58 public abstract byte getTag(); 59 60 63 public static ValueImpl readWithTag(MirrorImpl target, DataInputStream in) throws IOException { 64 byte tag = target.readByte("object tag", JdwpID.tagMap(), in); return readWithoutTag(target, tag, in); 66 } 67 68 71 public static ValueImpl readWithoutTag(MirrorImpl target, int type, DataInputStream in) throws IOException { 72 VirtualMachineImpl vmImpl = target.virtualMachineImpl(); 73 switch(type) { 75 case ArrayReferenceImpl.tag: 76 return ArrayReferenceImpl.read(target, in); 77 case ClassLoaderReferenceImpl.tag: 78 return ClassLoaderReferenceImpl.read(target, in); 79 case ClassObjectReferenceImpl.tag: 80 return ClassObjectReferenceImpl.read(target, in); 81 case StringReferenceImpl.tag: 82 return StringReferenceImpl.read(target, in); 83 case ObjectReferenceImpl.tag: 84 return ObjectReferenceImpl.readObjectRefWithoutTag(target, in); 85 case ThreadGroupReferenceImpl.tag: 86 return ThreadGroupReferenceImpl.read(target, in); 87 case ThreadReferenceImpl.tag: 88 return ThreadReferenceImpl.read(target, in); 89 case BooleanValueImpl.tag: 90 return BooleanValueImpl.read(target, in); 91 case ByteValueImpl.tag: 92 return ByteValueImpl.read(target, in); 93 case CharValueImpl.tag: 94 return CharValueImpl.read(target, in); 95 case DoubleValueImpl.tag: 96 return DoubleValueImpl.read(target, in); 97 case FloatValueImpl.tag: 98 return FloatValueImpl.read(target, in); 99 case IntegerValueImpl.tag: 100 return IntegerValueImpl.read(target, in); 101 case LongValueImpl.tag: 102 return LongValueImpl.read(target, in); 103 case ShortValueImpl.tag: 104 return ShortValueImpl.read(target, in); 105 case VoidValueImpl.tag: 106 return new VoidValueImpl(vmImpl); 107 case 0: 108 return null; 109 default: 110 throw new InternalException(JDIMessages.ValueImpl_Invalid_Value_tag_encountered___1 + type); 111 } 112 } 113 114 117 public void writeWithTag(MirrorImpl target, DataOutputStream out) throws IOException { 118 target.writeByte(getTag(), "tag", JdwpID.tagMap(), out); write(target, out); 120 } 121 122 125 public abstract void write(MirrorImpl target, DataOutputStream out) throws IOException ; 126 127 130 public static void writeNull(MirrorImpl target, DataOutputStream out) throws IOException { 131 JdwpObjectID nullID = new JdwpObjectID(target.virtualMachineImpl()); 132 nullID.write(out); 133 if (target.fVerboseWriter != null) 134 target.fVerboseWriter.println("objectReference", nullID.value()); } 136 137 140 public static void writeNullWithTag(MirrorImpl target, DataOutputStream out) throws IOException { 141 target.writeByte(ObjectReferenceImpl.tag, "tag", JdwpID.tagMap(), out); writeNull(target, out); 143 } 144 145 152 protected static List checkValues(List values, List types, VirtualMachineImpl vm) throws InvalidTypeException { 153 List result= new ArrayList (values.size()); 154 Iterator iterValues= values.iterator(); 155 Iterator iterTypes= types.iterator(); 156 while (iterValues.hasNext()) { 157 Value value= (Value)iterValues.next(); 158 Type type= (Type)iterTypes.next(); 159 result.add(checkValue(value, type, vm)); 160 } 161 return result; 162 } 163 164 172 public static ValueImpl checkValue(Value value, Type type, VirtualMachineImpl vm) throws InvalidTypeException { 173 if (value == null) { 174 if (!(type instanceof PrimitiveType)) { 175 return null; 176 } 177 } else { 178 vm.checkVM(value); 179 TypeImpl valueType= (TypeImpl)value.type(); 180 if (valueType instanceof PrimitiveType && type instanceof PrimitiveType) { 181 return checkPrimitiveValue((PrimitiveValueImpl) value, (PrimitiveTypeImpl) valueType, (PrimitiveTypeImpl) type); 182 } 183 if (valueType instanceof ReferenceType && type instanceof ReferenceType) { 184 checkReferenceType((ReferenceType) valueType, (ReferenceType) type); 185 return (ValueImpl)value; 186 } 187 if(valueType instanceof VoidType && type instanceof VoidType) { 188 return (VoidValueImpl) value; 189 } 190 } 191 throw new InvalidTypeException(JDIMessages.ValueImpl_Type_of_the_value_not_compatible_with_the_expected_type__1); 192 } 193 194 196 private static void checkReferenceType(ReferenceType valueType, ReferenceType type) throws InvalidTypeException { 197 if (valueType instanceof ArrayType) { 198 if (type instanceof ArrayType) { 199 try { 200 Type valueComponentType= ((ArrayType) valueType).componentType(); 201 Type componentType= ((ArrayType) type).componentType(); 202 if (valueComponentType instanceof PrimitiveType) { 203 if (valueComponentType.equals(componentType)) { 204 return; 205 } 206 } else if (valueComponentType instanceof ReferenceType && componentType instanceof ReferenceType) { 207 checkReferenceType((ReferenceType) valueComponentType, (ReferenceType) componentType); 208 return; 209 } 210 } catch (ClassNotLoadedException e) { 211 } 213 } else { 214 if (type.signature().equals("Ljava/lang/Object;")) { return; 217 } 218 } 219 } else { 220 if (type instanceof ClassType) { 221 if (valueType instanceof ClassType) { 222 ClassType superClass= (ClassType) valueType; 223 while (superClass != null) { 224 if (superClass.equals(type)) { 225 return; 226 } 227 superClass= superClass.superclass(); 228 } 229 } else if (valueType instanceof InterfaceType) { 230 if (type.signature().equals("Ljava/lang/Object;")) { return; 233 } 234 } 235 } else if (type instanceof InterfaceType) { 236 if (valueType instanceof InterfaceType) { 237 if (checkInterfaceType((InterfaceType) valueType, (InterfaceType) type)) { 238 return; 239 } 240 } else { 241 List interfaces= ((ClassType)valueType).allInterfaces(); 242 for (Iterator iter= interfaces.iterator(); iter.hasNext();) { 243 if (checkInterfaceType((InterfaceType) iter.next(), (InterfaceType) type)) { 244 return; 245 } 246 } 247 } 248 } 249 } 250 251 throw new InvalidTypeException(JDIMessages.ValueImpl_Type_of_the_value_not_compatible_with_the_expected_type__1); 252 } 253 254 private static boolean checkInterfaceType(InterfaceType valueType, InterfaceType type) { 255 if (valueType.equals(type)) { 256 return true; 257 } 258 List superInterfaces= valueType.superinterfaces(); 259 for (Iterator iter= superInterfaces.iterator(); iter.hasNext();) { 260 if (checkInterfaceType((InterfaceType) iter.next(), type)) { 261 return true; 262 } 263 } 264 return false; 265 } 266 267 274 protected static ValueImpl checkPrimitiveValue(PrimitiveValueImpl value, PrimitiveTypeImpl valueType, PrimitiveTypeImpl type) throws InvalidTypeException { 275 char valueTypeSignature= valueType.signature().charAt(0); 276 char typeSignature= type.signature().charAt(0); 277 if (valueTypeSignature == typeSignature) { 278 return value; 279 } 280 VirtualMachineImpl vm= value.virtualMachineImpl(); 281 switch (typeSignature) { 282 case 'D': 283 if (valueTypeSignature != 'Z') { 284 return new DoubleValueImpl(vm, new Double (value.doubleValue())); 285 } 286 break; 287 case 'F': 288 if (valueTypeSignature != 'Z' && valueTypeSignature != 'D') { 289 return new FloatValueImpl(vm, new Float (value.floatValue())); 290 } 291 break; 292 case 'J': 293 if (valueTypeSignature != 'Z' && valueTypeSignature != 'D' && valueTypeSignature != 'F') { 294 return new LongValueImpl(vm, new Long (value.longValue())); 295 } 296 break; 297 case 'I': 298 if (valueTypeSignature == 'B' || valueTypeSignature == 'C' || valueTypeSignature == 'S') { 299 return new IntegerValueImpl(vm, new Integer (value.intValue())); 300 } 301 break; 302 case 'S': 303 if (valueTypeSignature == 'B') { 304 return new ShortValueImpl(vm, new Short (value.shortValue())); 305 } 306 break; 307 } 308 throw new InvalidTypeException(JDIMessages.ValueImpl_Type_of_the_value_not_compatible_with_the_expected_type__1); 309 } 310 } 311 | Popular Tags |