KickJava   Java API By Example, From Geeks To Geeks.

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


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
14 import com.ibm.icu.text.MessageFormat;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.debug.core.model.IVariable;
20 import org.eclipse.jdt.debug.core.IJavaArrayType;
21 import org.eclipse.jdt.debug.core.IJavaClassObject;
22 import org.eclipse.jdt.debug.core.IJavaDebugTarget;
23 import org.eclipse.jdt.debug.core.IJavaObject;
24 import org.eclipse.jdt.debug.core.IJavaReferenceType;
25 import org.eclipse.jdt.debug.core.IJavaType;
26 import org.eclipse.jdt.debug.core.IJavaValue;
27 import org.eclipse.jdt.debug.core.IJavaVariable;
28 import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin;
29 import org.eclipse.jdt.internal.debug.eval.ast.engine.IRuntimeContext;
30 import org.eclipse.jdt.internal.debug.eval.ast.engine.Interpreter;
31
32 /**
33  * Common behavior for instructions.
34  */

35 public abstract class Instruction {
36
37     private Interpreter fInterpreter;
38
39     public abstract int getSize();
40
41     public void setInterpreter(Interpreter interpreter) {
42         fInterpreter= interpreter;
43     }
44
45     public void setLastValue(IJavaValue value) {
46         fInterpreter.setLastValue(value);
47     }
48
49     public void stop() {
50         fInterpreter.stop();
51     }
52
53     public static int getBinaryPromotionType(int left, int right) {
54         return fTypeTable[left][right];
55     }
56     public abstract void execute() throws CoreException;
57
58     protected IRuntimeContext getContext() {
59         return fInterpreter.getContext();
60     }
61
62     protected IJavaDebugTarget getVM() {
63         return getContext().getVM();
64     }
65
66     /**
67      * Return the internal variable with the given name.
68      *
69      * @see Interpreter#getInternalVariable(String)
70      */

71     protected IVariable getInternalVariable(String JavaDoc name) {
72         return fInterpreter.getInternalVariable(name);
73     }
74
75     /**
76      * Create and return a new internal variable with the given name
77      * and the given type.
78      *
79      * @see Interpreter#createInternalVariable(String, String)
80      */

81     protected IVariable createInternalVariable(String JavaDoc name, IJavaType referencType) {
82         return fInterpreter.createInternalVariable(name, referencType);
83     }
84
85
86     /**
87      * Answers the instance of Class that the given type represents.
88      */

89     protected IJavaObject getClassObject(IJavaType type) throws CoreException {
90         if (type instanceof IJavaReferenceType) {
91             return ((IJavaReferenceType)type).getClassObject();
92         }
93         return null;
94     }
95
96     protected void jump(int offset) {
97         fInterpreter.jump(offset);
98     }
99
100     protected void push(Object JavaDoc object) {
101         fInterpreter.push(object);
102     }
103
104     protected Object JavaDoc pop() {
105         return fInterpreter.pop();
106     }
107
108     protected IJavaValue popValue() throws CoreException {
109         Object JavaDoc element = fInterpreter.pop();
110         if (element instanceof IJavaVariable) {
111             return (IJavaValue)((IJavaVariable)element).getValue();
112         }
113         return (IJavaValue)element;
114     }
115
116     protected void pushNewValue(boolean value) {
117         fInterpreter.push(newValue(value));
118     }
119
120     protected IJavaValue newValue(boolean value) {
121         return getVM().newValue(value);
122     }
123
124     protected void pushNewValue(byte value) {
125         fInterpreter.push(newValue(value));
126     }
127
128     protected IJavaValue newValue(byte value) {
129         return getVM().newValue(value);
130     }
131
132     protected void pushNewValue(short value) {
133         fInterpreter.push(newValue(value));
134     }
135
136     protected IJavaValue newValue(short value) {
137         return getVM().newValue(value);
138     }
139
140     protected void pushNewValue(int value) {
141         fInterpreter.push(newValue(value));
142     }
143
144     protected IJavaValue newValue(int value) {
145         return getVM().newValue(value);
146     }
147
148     protected void pushNewValue(long value) {
149         fInterpreter.push(newValue(value));
150     }
151
152     protected IJavaValue newValue(long value) {
153         return getVM().newValue(value);
154     }
155
156     protected void pushNewValue(char value) {
157         fInterpreter.push(newValue(value));
158     }
159
160     protected IJavaValue newValue(char value) {
161         return getVM().newValue(value);
162     }
163
164     protected void pushNewValue(float value) {
165         fInterpreter.push(newValue(value));
166     }
167
168     protected IJavaValue newValue(float value) {
169         return getVM().newValue(value);
170     }
171
172     protected void pushNewValue(double value) {
173         fInterpreter.push(newValue(value));
174     }
175
176     protected IJavaValue newValue(double value) {
177         return getVM().newValue(value);
178     }
179
180     protected void pushNewValue(String JavaDoc value) {
181         fInterpreter.push(newValue(value));
182     }
183
184     protected IJavaValue newValue(String JavaDoc value) {
185         return getVM().newValue(value);
186     }
187
188     protected void pushNullValue() {
189         fInterpreter.push(nullValue());
190     }
191
192     protected IJavaValue nullValue() {
193         return getVM().nullValue();
194     }
195
196     public static int getUnaryPromotionType(int typeId) {
197         return fTypeTable[typeId][T_int];
198     }
199
200     protected IJavaType getType(String JavaDoc qualifiedName) throws CoreException {
201         // Force the class to be loaded, and record the class reference
202
// for later use if there are multiple classes with the same name.
203
IJavaClassObject classReference= getContext().classForName(qualifiedName);
204         // Found many classes, look for the right one for this scope.
205
if (classReference == null) {
206             throw new CoreException(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), IStatus.OK, MessageFormat.format(InstructionsEvaluationMessages.Instruction_No_type, new String JavaDoc[]{qualifiedName}), null));
207         }
208         return classReference.getInstanceType();
209     }
210
211     protected IJavaArrayType getArrayType(String JavaDoc typeSignature, int dimension) throws CoreException {
212         String JavaDoc qualifiedName = RuntimeSignature.toString(typeSignature);
213         String JavaDoc braces = ""; //$NON-NLS-1$
214
for (int i = 0; i < dimension; i++) {
215             qualifiedName += "[]"; //$NON-NLS-1$
216
braces += "["; //$NON-NLS-1$
217
}
218         String JavaDoc signature = braces + typeSignature;
219         // Force the class to be loaded, and record the class reference
220
// for later use if there are multiple classes with the same name.
221
IJavaObject classReference= getContext().classForName(signature);
222         if (classReference == null) {
223             throw new CoreException(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), IStatus.OK, MessageFormat.format(InstructionsEvaluationMessages.Instruction_No_type, new String JavaDoc[]{qualifiedName}), null));
224         }
225         IJavaType[] types= getVM().getJavaTypes(qualifiedName);
226         checkTypes(types, qualifiedName);
227         if (types.length == 1) {
228             // Found only one class.
229
return (IJavaArrayType)types[0];
230         }
231         // Found many classes, look for the right one for this scope.
232
for(int i= 0, length= types.length; i < length; i++) {
233             IJavaType type= types[i];
234             if (classReference.equals(getClassObject(type))) {
235                 return (IJavaArrayType)type;
236             }
237         }
238
239         // At this point a very strange thing has happened,
240
// the VM was able to return multiple types in the classesByName
241
// call, but none of them were the class that was returned in
242
// the classForName call.
243

244         throw new CoreException(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), IStatus.OK, MessageFormat.format(InstructionsEvaluationMessages.Instruction_No_type, new String JavaDoc[]{qualifiedName}), null));
245     }
246
247     protected void checkTypes(IJavaType[] types, String JavaDoc qualifiedName) throws CoreException {
248         if (types == null || types.length == 0) {
249             throw new CoreException(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), IStatus.OK, MessageFormat.format(InstructionsEvaluationMessages.Instruction_No_type, new String JavaDoc[]{qualifiedName}), null));
250         }
251     }
252
253     static public final int T_undefined =0;
254     static public final int T_Object =1;
255     static public final int T_char =2;
256     static public final int T_byte =3;
257     static public final int T_short =4;
258     static public final int T_boolean =5;
259     static public final int T_void =6;
260     static public final int T_long =7;
261     static public final int T_double =8;
262     static public final int T_float =9;
263     static public final int T_int =10;
264     static public final int T_String =11;
265     static public final int T_null =12;
266
267     private static final int[][] fTypeTable= {
268 /* undefined */ {T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined},
269 /* object */ {T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_String, T_undefined},
270 /* char */ {T_undefined, T_undefined, T_int, T_int, T_int, T_undefined, T_undefined, T_long, T_double, T_float, T_int, T_String, T_undefined},
271 /* byte */ {T_undefined, T_undefined, T_int, T_int, T_int, T_undefined, T_undefined, T_long, T_double, T_float, T_int, T_String, T_undefined},
272 /* short */ {T_undefined, T_undefined, T_int, T_int, T_int, T_undefined, T_undefined, T_long, T_double, T_float, T_int, T_String, T_undefined},
273 /* boolean */ {T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_boolean, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_String, T_undefined},
274 /* void */ {T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined},
275 /* long */ {T_undefined, T_undefined, T_long, T_long, T_long, T_undefined, T_undefined, T_long, T_double, T_float, T_long, T_String, T_undefined},
276 /* double */ {T_undefined, T_undefined, T_double, T_double, T_double, T_undefined, T_undefined, T_double, T_double, T_double, T_double, T_String, T_undefined},
277 /* float */ {T_undefined, T_undefined, T_float, T_float, T_float, T_undefined, T_undefined, T_float, T_double, T_float, T_float, T_String, T_undefined},
278 /* int */ {T_undefined, T_undefined, T_int, T_int, T_int, T_undefined, T_undefined, T_long, T_double, T_float, T_int, T_String, T_undefined},
279 /* String */ {T_undefined, T_String, T_String, T_String, T_String, T_String, T_undefined, T_String, T_String, T_String, T_String, T_String, T_String},
280 /* null */ {T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_undefined, T_String, T_undefined},
281     };
282 }
283
284
Popular Tags