KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > eval > ast > instructions > Cast


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 JavaDoc IS_INSTANCE= "isInstance"; //$NON-NLS-1$
28
public static final String JavaDoc IS_INSTANCE_SIGNATURE= "(Ljava/lang/Object;)Z"; //$NON-NLS-1$
29

30     private int fTypeTypeId;
31     
32     private String JavaDoc fBaseTypeName;
33     
34     private int fDimension;
35     
36     /**
37      * Cast intruction constructor.
38      *
39      * @param typeTypeId the id of the type to cast into.
40      * @param baseTypeName the base type name of the type (the type name if the
41      * type is not an array type.
42      * @param dimension the dimension of the array type, 0 if the type is not an
43      * array type.
44      */

45     public Cast(int typeTypeId, String JavaDoc baseTypeName, int dimension, int start) {
46         super(start);
47         fTypeTypeId= typeTypeId;
48         fBaseTypeName= baseTypeName;
49         fDimension= dimension;
50     }
51
52     /*
53      * @see Instruction#execute()
54      */

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             // null value can be cast to all non-primitive types (bug 31637).
86
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 JavaDoc[]{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 JavaDoc[]{value.toString(), typeName()}), null));
100             }
101             
102             push(value);
103         }
104     }
105     
106     private String JavaDoc typeName() {
107         String JavaDoc result= fBaseTypeName;
108         for (int i= 0; i < fDimension; i++) {
109             result+= "[]"; //$NON-NLS-1$
110
}
111         return result;
112     }
113
114     /*
115      * @see Object#toString()
116      */

117     public String JavaDoc toString() {
118         return InstructionsEvaluationMessages.Cast_cast_3;
119     }
120
121 }
122
Popular Tags