1 11 package org.eclipse.jdt.internal.debug.eval.ast.instructions; 12 13 import com.ibm.icu.text.MessageFormat; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IStatus; 17 import org.eclipse.core.runtime.Status; 18 import org.eclipse.jdt.core.Signature; 19 import org.eclipse.jdt.debug.core.IJavaObject; 20 import org.eclipse.jdt.debug.core.IJavaPrimitiveValue; 21 import org.eclipse.jdt.debug.core.IJavaValue; 22 import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin; 23 import org.eclipse.jdt.internal.debug.core.model.JDINullValue; 24 25 public class Cast extends CompoundInstruction { 26 27 public static final String IS_INSTANCE= "isInstance"; public static final String IS_INSTANCE_SIGNATURE= "(Ljava/lang/Object;)Z"; 30 private int fTypeTypeId; 31 32 private String fBaseTypeName; 33 34 private int fDimension; 35 36 45 public Cast(int typeTypeId, String baseTypeName, int dimension, int start) { 46 super(start); 47 fTypeTypeId= typeTypeId; 48 fBaseTypeName= baseTypeName; 49 fDimension= dimension; 50 } 51 52 55 public void execute() throws CoreException { 56 IJavaValue value= popValue(); 57 58 if (value instanceof IJavaPrimitiveValue) { 59 IJavaPrimitiveValue primitiveValue = (IJavaPrimitiveValue) value; 60 switch (fTypeTypeId) { 61 case T_double: 62 push(newValue(primitiveValue.getDoubleValue())); 63 break; 64 case T_float: 65 push(newValue(primitiveValue.getFloatValue())); 66 break; 67 case T_long: 68 push(newValue(primitiveValue.getLongValue())); 69 break; 70 case T_int: 71 push(newValue(primitiveValue.getIntValue())); 72 break; 73 case T_short: 74 push(newValue(primitiveValue.getShortValue())); 75 break; 76 case T_byte: 77 push(newValue(primitiveValue.getByteValue())); 78 break; 79 case T_char: 80 push(newValue(primitiveValue.getCharValue())); 81 break; 82 } 83 84 } else if (value instanceof JDINullValue) { 85 push(value); 87 } else { 88 IJavaObject classObject; 89 if (fDimension == 0) { 90 classObject= getClassObject(getType(fBaseTypeName)); 91 } else { 92 classObject= getClassObject(getArrayType(Signature.createTypeSignature(fBaseTypeName, true), fDimension)); 93 } 94 if (classObject == null) { 95 throw new CoreException(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), IStatus.OK, MessageFormat.format(InstructionsEvaluationMessages.Cast_No_class_object, new String []{typeName()}), null)); 96 } 97 IJavaPrimitiveValue resultValue = (IJavaPrimitiveValue)classObject.sendMessage(IS_INSTANCE, IS_INSTANCE_SIGNATURE, new IJavaValue[] {value}, getContext().getThread(), false); 98 if (!resultValue.getBooleanValue()) { 99 throw new CoreException(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), IStatus.OK, MessageFormat.format(InstructionsEvaluationMessages.Cast_ClassCastException__Cannot_cast__0__as__1___1, new String []{value.toString(), typeName()}), null)); 100 } 101 102 push(value); 103 } 104 } 105 106 private String typeName() { 107 String result= fBaseTypeName; 108 for (int i= 0; i < fDimension; i++) { 109 result+= "[]"; } 111 return result; 112 } 113 114 117 public String toString() { 118 return InstructionsEvaluationMessages.Cast_cast_3; 119 } 120 121 } 122 | Popular Tags |