author | nloodin |
Wed, 31 Aug 2011 13:48:04 +0200 | |
changeset 10292 | ed7db6a12c2a |
parent 9520 | 99d378796e54 |
child 14342 | 8435a30053c1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
10292
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
26 |
/* |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
27 |
* This source code is provided to illustrate the usage of a given feature |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
28 |
* or technique and has been deliberately simplified. Additional steps |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
29 |
* required for a production-quality application, such as security checks, |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
30 |
* input validation and proper error handling, might not be present in |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
31 |
* this sample code. |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
32 |
*/ |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
33 |
|
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
34 |
|
2 | 35 |
package com.sun.tools.example.debug.expr; |
36 |
||
37 |
import com.sun.jdi.*; |
|
38 |
import java.util.*; |
|
39 |
||
40 |
abstract class LValue { |
|
41 |
||
42 |
// The JDI Value object for this LValue. Once we have this Value, |
|
43 |
// we have to remember it since after we return the LValue object |
|
44 |
// to the ExpressionParser, it might decide that it needs |
|
45 |
// the 'toString' value for the LValue in which case it will |
|
46 |
// call getMassagedValue to get this toString value. At that |
|
47 |
// point, we don't want to call JDI a 2nd time to get the Value |
|
48 |
// for the LValue. This is especially wrong when the LValue |
|
49 |
// represents a member function. We would end up calling it |
|
50 |
// a 2nd time. |
|
51 |
// |
|
52 |
// Unfortunately, there are several levels of calls to |
|
53 |
// get/set values in this file. To minimize confusion, |
|
54 |
// jdiValue is set/tested at the lowest level - right |
|
55 |
// next to the actual calls to JDI methods to get/set the |
|
56 |
// value in the debuggee. |
|
57 |
protected Value jdiValue; |
|
58 |
||
59 |
abstract Value getValue() throws InvocationException, |
|
60 |
IncompatibleThreadStateException, |
|
61 |
InvalidTypeException, |
|
62 |
ClassNotLoadedException, |
|
63 |
ParseException; |
|
64 |
||
65 |
abstract void setValue0(Value value) |
|
66 |
throws ParseException, InvalidTypeException, |
|
67 |
ClassNotLoadedException; |
|
68 |
||
69 |
abstract void invokeWith(List<Value> arguments) throws ParseException; |
|
70 |
||
71 |
void setValue(Value value) throws ParseException { |
|
72 |
try { |
|
73 |
setValue0(value); |
|
74 |
} catch (InvalidTypeException exc) { |
|
75 |
throw new ParseException( |
|
76 |
"Attempt to set value of incorrect type" + |
|
77 |
exc); |
|
78 |
} catch (ClassNotLoadedException exc) { |
|
79 |
throw new ParseException( |
|
80 |
"Attempt to set value before " + exc.className() + " was loaded" + |
|
81 |
exc); |
|
82 |
} |
|
83 |
} |
|
84 |
||
85 |
void setValue(LValue lval) throws ParseException { |
|
86 |
setValue(lval.interiorGetValue()); |
|
87 |
} |
|
88 |
||
89 |
LValue memberLValue(ExpressionParser.GetFrame frameGetter, |
|
90 |
String fieldName) throws ParseException { |
|
91 |
try { |
|
92 |
return memberLValue(fieldName, frameGetter.get().thread()); |
|
93 |
} catch (IncompatibleThreadStateException exc) { |
|
94 |
throw new ParseException("Thread not suspended"); |
|
95 |
} |
|
96 |
} |
|
97 |
||
98 |
LValue memberLValue(String fieldName, ThreadReference thread) throws ParseException { |
|
99 |
||
100 |
Value val = interiorGetValue(); |
|
101 |
if ((val instanceof ArrayReference) && |
|
102 |
"length".equals(fieldName)){ |
|
103 |
return new LValueArrayLength((ArrayReference)val); |
|
104 |
} |
|
105 |
return new LValueInstanceMember(val, fieldName, thread); |
|
106 |
} |
|
107 |
||
108 |
// Return the Value for this LValue that would be used to concatenate |
|
109 |
// to a String. IE, if it is an Object, call toString in the debuggee. |
|
110 |
Value getMassagedValue(ExpressionParser.GetFrame frameGetter) throws ParseException { |
|
111 |
Value vv = interiorGetValue(); |
|
112 |
||
113 |
// If vv is an ObjectReference, then we have to |
|
114 |
// do the implicit call to toString(). |
|
115 |
if (vv instanceof ObjectReference && |
|
116 |
!(vv instanceof StringReference) && |
|
117 |
!(vv instanceof ArrayReference)) { |
|
118 |
StackFrame frame; |
|
119 |
try { |
|
120 |
frame = frameGetter.get(); |
|
121 |
} catch (IncompatibleThreadStateException exc) { |
|
122 |
throw new ParseException("Thread not suspended"); |
|
123 |
} |
|
124 |
||
125 |
ThreadReference thread = frame.thread(); |
|
126 |
LValue toStringMember = memberLValue("toString", thread); |
|
127 |
toStringMember.invokeWith(new ArrayList<Value>()); |
|
128 |
return toStringMember.interiorGetValue(); |
|
129 |
} |
|
130 |
return vv; |
|
131 |
} |
|
132 |
||
133 |
Value interiorGetValue() throws ParseException { |
|
134 |
Value value; |
|
135 |
try { |
|
136 |
value = getValue(); |
|
137 |
} catch (InvocationException e) { |
|
138 |
throw new ParseException("Unable to complete expression. Exception " + |
|
139 |
e.exception() + " thrown"); |
|
140 |
} catch (IncompatibleThreadStateException itse) { |
|
141 |
throw new ParseException("Unable to complete expression. Thread " + |
|
142 |
"not suspended for method invoke"); |
|
143 |
} catch (InvalidTypeException ite) { |
|
144 |
throw new ParseException("Unable to complete expression. Method " + |
|
145 |
"argument type mismatch"); |
|
146 |
} catch (ClassNotLoadedException tnle) { |
|
147 |
throw new ParseException("Unable to complete expression. Method " + |
|
148 |
"argument type " + tnle.className() + |
|
149 |
" not yet loaded"); |
|
150 |
} |
|
151 |
return value; |
|
152 |
} |
|
153 |
||
154 |
LValue arrayElementLValue(LValue lval) throws ParseException { |
|
155 |
Value indexValue = lval.interiorGetValue(); |
|
156 |
int index; |
|
157 |
if ( (indexValue instanceof IntegerValue) || |
|
158 |
(indexValue instanceof ShortValue) || |
|
159 |
(indexValue instanceof ByteValue) || |
|
160 |
(indexValue instanceof CharValue) ) { |
|
161 |
index = ((PrimitiveValue)indexValue).intValue(); |
|
162 |
} else { |
|
163 |
throw new ParseException("Array index must be a integer type"); |
|
164 |
} |
|
165 |
return new LValueArrayElement(interiorGetValue(), index); |
|
166 |
} |
|
167 |
||
9520 | 168 |
@Override |
2 | 169 |
public String toString() { |
170 |
try { |
|
171 |
return interiorGetValue().toString(); |
|
172 |
} catch (ParseException e) { |
|
173 |
return "<Parse Exception>"; |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
static final int STATIC = 0; |
|
178 |
static final int INSTANCE = 1; |
|
179 |
||
180 |
static Field fieldByName(ReferenceType refType, String name, int kind) { |
|
181 |
/* |
|
182 |
* TO DO: Note that this currently fails to find superclass |
|
183 |
* or implemented interface fields. This is due to a temporary |
|
184 |
* limititation of RefType.fieldByName. Once that method is |
|
185 |
* fixed, superclass fields will be found. |
|
186 |
*/ |
|
187 |
Field field = refType.fieldByName(name); |
|
188 |
if (field != null) { |
|
189 |
boolean isStatic = field.isStatic(); |
|
190 |
if (((kind == STATIC) && !isStatic) || |
|
191 |
((kind == INSTANCE) && isStatic)) { |
|
192 |
field = null; |
|
193 |
} |
|
194 |
} |
|
195 |
/*** |
|
196 |
System.err.println("fieldByName: " + refType.name() + " " + |
|
197 |
name + " " + |
|
198 |
kind + " " + |
|
199 |
(field != null)); |
|
200 |
***/ |
|
201 |
return field; |
|
202 |
} |
|
203 |
||
51 | 204 |
static List<Method> methodsByName(ReferenceType refType, |
205 |
String name, int kind) { |
|
206 |
List<Method> list = refType.methodsByName(name); |
|
207 |
Iterator<Method> iter = list.iterator(); |
|
2 | 208 |
while (iter.hasNext()) { |
51 | 209 |
Method method = iter.next(); |
2 | 210 |
boolean isStatic = method.isStatic(); |
211 |
if (((kind == STATIC) && !isStatic) || |
|
212 |
((kind == INSTANCE) && isStatic)) { |
|
213 |
iter.remove(); |
|
214 |
} |
|
215 |
} |
|
216 |
return list; |
|
217 |
} |
|
218 |
||
219 |
static List<String> primitiveTypeNames = new ArrayList<String>(); |
|
220 |
static { |
|
221 |
primitiveTypeNames.add("boolean"); |
|
222 |
primitiveTypeNames.add("byte"); |
|
223 |
primitiveTypeNames.add("char"); |
|
224 |
primitiveTypeNames.add("short"); |
|
225 |
primitiveTypeNames.add("int"); |
|
226 |
primitiveTypeNames.add("long"); |
|
227 |
primitiveTypeNames.add("float"); |
|
228 |
primitiveTypeNames.add("double"); |
|
229 |
} |
|
230 |
||
231 |
||
232 |
static final int SAME = 0; |
|
233 |
static final int ASSIGNABLE = 1; |
|
234 |
static final int DIFFERENT = 2; |
|
235 |
/* |
|
236 |
* Return SAME, DIFFERENT or ASSIGNABLE. |
|
237 |
* SAME means each arg type is the same as type of the corr. arg. |
|
238 |
* ASSIGNABLE means that not all the pairs are the same, but |
|
239 |
* for those that aren't, at least the argType is assignable |
|
240 |
* from the type of the argument value. |
|
241 |
* DIFFERENT means that in at least one pair, the |
|
242 |
* argType is not assignable from the type of the argument value. |
|
243 |
* IE, one is an Apple and the other is an Orange. |
|
244 |
*/ |
|
51 | 245 |
static int argumentsMatch(List<Type> argTypes, List<Value> arguments) { |
2 | 246 |
if (argTypes.size() != arguments.size()) { |
247 |
return DIFFERENT; |
|
248 |
} |
|
249 |
||
51 | 250 |
Iterator<Type> typeIter = argTypes.iterator(); |
251 |
Iterator<Value> valIter = arguments.iterator(); |
|
2 | 252 |
int result = SAME; |
253 |
||
254 |
// If any pair aren't the same, change the |
|
255 |
// result to ASSIGNABLE. If any pair aren't |
|
256 |
// assignable, return DIFFERENT |
|
257 |
while (typeIter.hasNext()) { |
|
51 | 258 |
Type argType = typeIter.next(); |
259 |
Value value = valIter.next(); |
|
2 | 260 |
if (value == null) { |
261 |
// Null values can be passed to any non-primitive argument |
|
262 |
if (primitiveTypeNames.contains(argType.name())) { |
|
263 |
return DIFFERENT; |
|
264 |
} |
|
265 |
// Else, we will assume that a null value |
|
266 |
// exactly matches an object type. |
|
267 |
} |
|
268 |
if (!value.type().equals(argType)) { |
|
269 |
if (isAssignableTo(value.type(), argType)) { |
|
270 |
result = ASSIGNABLE; |
|
271 |
} else { |
|
272 |
return DIFFERENT; |
|
273 |
} |
|
274 |
} |
|
275 |
} |
|
276 |
return result; |
|
277 |
} |
|
278 |
||
279 |
||
280 |
// These is...AssignableTo methods are based on similar code in the JDI |
|
281 |
// implementations of ClassType, ArrayType, and InterfaceType |
|
282 |
||
283 |
static boolean isComponentAssignable(Type fromType, Type toType) { |
|
284 |
if (fromType instanceof PrimitiveType) { |
|
285 |
// Assignment of primitive arrays requires identical |
|
286 |
// component types. |
|
287 |
return fromType.equals(toType); |
|
288 |
} |
|
289 |
if (toType instanceof PrimitiveType) { |
|
290 |
return false; |
|
291 |
} |
|
292 |
// Assignment of object arrays requires availability |
|
293 |
// of widening conversion of component types |
|
294 |
return isAssignableTo(fromType, toType); |
|
295 |
} |
|
296 |
||
297 |
static boolean isArrayAssignableTo(ArrayType fromType, Type toType) { |
|
298 |
if (toType instanceof ArrayType) { |
|
299 |
try { |
|
300 |
Type toComponentType = ((ArrayType)toType).componentType(); |
|
301 |
return isComponentAssignable(fromType.componentType(), toComponentType); |
|
302 |
} catch (ClassNotLoadedException e) { |
|
303 |
// One or both component types has not yet been |
|
304 |
// loaded => can't assign |
|
305 |
return false; |
|
306 |
} |
|
307 |
} |
|
308 |
if (toType instanceof InterfaceType) { |
|
309 |
// Only valid InterfaceType assignee is Cloneable |
|
310 |
return toType.name().equals("java.lang.Cloneable"); |
|
311 |
} |
|
312 |
// Only valid ClassType assignee is Object |
|
313 |
return toType.name().equals("java.lang.Object"); |
|
314 |
} |
|
315 |
||
316 |
static boolean isAssignableTo(Type fromType, Type toType) { |
|
317 |
if (fromType.equals(toType)) { |
|
318 |
return true; |
|
319 |
} |
|
320 |
||
321 |
// If one is boolean, so must be the other. |
|
322 |
if (fromType instanceof BooleanType) { |
|
323 |
if (toType instanceof BooleanType) { |
|
324 |
return true; |
|
325 |
} |
|
326 |
return false; |
|
327 |
} |
|
328 |
if (toType instanceof BooleanType) { |
|
329 |
return false; |
|
330 |
} |
|
331 |
||
332 |
// Other primitive types are intermixable only with each other. |
|
333 |
if (fromType instanceof PrimitiveType) { |
|
334 |
if (toType instanceof PrimitiveType) { |
|
335 |
return true; |
|
336 |
} |
|
337 |
return false; |
|
338 |
} |
|
339 |
if (toType instanceof PrimitiveType) { |
|
340 |
return false; |
|
341 |
} |
|
342 |
||
343 |
// neither one is primitive. |
|
344 |
if (fromType instanceof ArrayType) { |
|
345 |
return isArrayAssignableTo((ArrayType)fromType, toType); |
|
346 |
} |
|
51 | 347 |
List<InterfaceType> interfaces; |
2 | 348 |
if (fromType instanceof ClassType) { |
349 |
ClassType superclazz = ((ClassType)fromType).superclass(); |
|
350 |
if ((superclazz != null) && isAssignableTo(superclazz, toType)) { |
|
351 |
return true; |
|
352 |
} |
|
353 |
interfaces = ((ClassType)fromType).interfaces(); |
|
354 |
} else { |
|
355 |
// fromType must be an InterfaceType |
|
356 |
interfaces = ((InterfaceType)fromType).superinterfaces(); |
|
357 |
} |
|
51 | 358 |
for (InterfaceType interfaze : interfaces) { |
2 | 359 |
if (isAssignableTo(interfaze, toType)) { |
360 |
return true; |
|
361 |
} |
|
362 |
} |
|
363 |
return false; |
|
364 |
} |
|
365 |
||
51 | 366 |
static Method resolveOverload(List<Method> overloads, |
367 |
List<Value> arguments) |
|
2 | 368 |
throws ParseException { |
369 |
||
370 |
// If there is only one method to call, we'll just choose |
|
371 |
// that without looking at the args. If they aren't right |
|
372 |
// the invoke will return a better error message than we |
|
373 |
// could generate here. |
|
374 |
if (overloads.size() == 1) { |
|
51 | 375 |
return overloads.get(0); |
2 | 376 |
} |
377 |
||
378 |
// Resolving overloads is beyond the scope of this exercise. |
|
379 |
// So, we will look for a method that matches exactly the |
|
380 |
// types of the arguments. If we can't find one, then |
|
381 |
// if there is exactly one method whose param types are assignable |
|
382 |
// from the arg types, we will use that. Otherwise, |
|
383 |
// it is an error. We won't guess which of multiple possible |
|
384 |
// methods to call. And, since casts aren't implemented, |
|
385 |
// the user can't use them to pick a particular overload to call. |
|
386 |
// IE, the user is out of luck in this case. |
|
387 |
Method retVal = null; |
|
388 |
int assignableCount = 0; |
|
51 | 389 |
for (Method mm : overloads) { |
390 |
List<Type> argTypes; |
|
2 | 391 |
try { |
392 |
argTypes = mm.argumentTypes(); |
|
393 |
} catch (ClassNotLoadedException ee) { |
|
394 |
// This probably won't happen for the |
|
395 |
// method that we are really supposed to |
|
396 |
// call. |
|
397 |
continue; |
|
398 |
} |
|
399 |
int compare = argumentsMatch(argTypes, arguments); |
|
400 |
if (compare == SAME) { |
|
401 |
return mm; |
|
402 |
} |
|
403 |
if (compare == DIFFERENT) { |
|
404 |
continue; |
|
405 |
} |
|
406 |
// Else, it is assignable. Remember it. |
|
407 |
retVal = mm; |
|
408 |
assignableCount++; |
|
409 |
} |
|
410 |
||
411 |
// At this point, we didn't find an exact match, |
|
412 |
// but we found one for which the args are assignable. |
|
413 |
// |
|
414 |
if (retVal != null) { |
|
415 |
if (assignableCount == 1) { |
|
416 |
return retVal; |
|
417 |
} |
|
418 |
throw new ParseException("Arguments match multiple methods"); |
|
419 |
} |
|
420 |
throw new ParseException("Arguments match no method"); |
|
421 |
} |
|
422 |
||
423 |
private static class LValueLocal extends LValue { |
|
424 |
final StackFrame frame; |
|
425 |
final LocalVariable var; |
|
426 |
||
427 |
LValueLocal(StackFrame frame, LocalVariable var) { |
|
428 |
this.frame = frame; |
|
429 |
this.var = var; |
|
430 |
} |
|
431 |
||
9520 | 432 |
@Override |
2 | 433 |
Value getValue() { |
434 |
if (jdiValue == null) { |
|
435 |
jdiValue = frame.getValue(var); |
|
436 |
} |
|
437 |
return jdiValue; |
|
438 |
} |
|
439 |
||
9520 | 440 |
@Override |
2 | 441 |
void setValue0(Value val) throws InvalidTypeException, |
442 |
ClassNotLoadedException { |
|
443 |
frame.setValue(var, val); |
|
444 |
jdiValue = val; |
|
445 |
} |
|
446 |
||
9520 | 447 |
@Override |
2 | 448 |
void invokeWith(List<Value> arguments) throws ParseException { |
449 |
throw new ParseException(var.name() + " is not a method"); |
|
450 |
} |
|
451 |
} |
|
452 |
||
453 |
private static class LValueInstanceMember extends LValue { |
|
454 |
final ObjectReference obj; |
|
455 |
final ThreadReference thread; |
|
456 |
final Field matchingField; |
|
51 | 457 |
final List<Method> overloads; |
2 | 458 |
Method matchingMethod = null; |
459 |
List<Value> methodArguments = null; |
|
460 |
||
461 |
LValueInstanceMember(Value value, |
|
462 |
String memberName, |
|
463 |
ThreadReference thread) throws ParseException { |
|
464 |
if (!(value instanceof ObjectReference)) { |
|
465 |
throw new ParseException( |
|
466 |
"Cannot access field of primitive type: " + value); |
|
467 |
} |
|
468 |
this.obj = (ObjectReference)value; |
|
469 |
this.thread = thread; |
|
470 |
ReferenceType refType = obj.referenceType(); |
|
471 |
/* |
|
472 |
* Can't tell yet whether this LValue will be accessed as a |
|
473 |
* field or method, so we keep track of all the possibilities |
|
474 |
*/ |
|
475 |
matchingField = LValue.fieldByName(refType, memberName, |
|
476 |
LValue.INSTANCE); |
|
477 |
overloads = LValue.methodsByName(refType, memberName, |
|
478 |
LValue.INSTANCE); |
|
479 |
if ((matchingField == null) && overloads.size() == 0) { |
|
480 |
throw new ParseException("No instance field or method with the name " |
|
481 |
+ memberName + " in " + refType.name()); |
|
482 |
} |
|
483 |
} |
|
484 |
||
9520 | 485 |
@Override |
2 | 486 |
Value getValue() throws InvocationException, InvalidTypeException, |
487 |
ClassNotLoadedException, IncompatibleThreadStateException, |
|
488 |
ParseException { |
|
489 |
if (jdiValue != null) { |
|
490 |
return jdiValue; |
|
491 |
} |
|
492 |
if (matchingMethod == null) { |
|
493 |
if (matchingField == null) { |
|
494 |
throw new ParseException("No such field in " + obj.referenceType().name()); |
|
495 |
} |
|
496 |
return jdiValue = obj.getValue(matchingField); |
|
497 |
} else { |
|
498 |
return jdiValue = obj.invokeMethod(thread, matchingMethod, methodArguments, 0); |
|
499 |
} |
|
500 |
} |
|
501 |
||
9520 | 502 |
@Override |
2 | 503 |
void setValue0(Value val) throws ParseException, |
504 |
InvalidTypeException, |
|
505 |
ClassNotLoadedException { |
|
506 |
if (matchingMethod != null) { |
|
507 |
throw new ParseException("Cannot assign to a method invocation"); |
|
508 |
} |
|
509 |
obj.setValue(matchingField, val); |
|
510 |
jdiValue = val; |
|
511 |
} |
|
512 |
||
9520 | 513 |
@Override |
2 | 514 |
void invokeWith(List<Value> arguments) throws ParseException { |
515 |
if (matchingMethod != null) { |
|
516 |
throw new ParseException("Invalid consecutive invocations"); |
|
517 |
} |
|
518 |
methodArguments = arguments; |
|
519 |
matchingMethod = LValue.resolveOverload(overloads, arguments); |
|
520 |
} |
|
521 |
} |
|
522 |
||
523 |
private static class LValueStaticMember extends LValue { |
|
524 |
final ReferenceType refType; |
|
525 |
final ThreadReference thread; |
|
526 |
final Field matchingField; |
|
51 | 527 |
final List<Method> overloads; |
2 | 528 |
Method matchingMethod = null; |
529 |
List<Value> methodArguments = null; |
|
530 |
||
531 |
LValueStaticMember(ReferenceType refType, |
|
532 |
String memberName, |
|
533 |
ThreadReference thread) throws ParseException { |
|
534 |
this.refType = refType; |
|
535 |
this.thread = thread; |
|
536 |
/* |
|
537 |
* Can't tell yet whether this LValue will be accessed as a |
|
538 |
* field or method, so we keep track of all the possibilities |
|
539 |
*/ |
|
540 |
matchingField = LValue.fieldByName(refType, memberName, |
|
541 |
LValue.STATIC); |
|
542 |
overloads = LValue.methodsByName(refType, memberName, |
|
543 |
LValue.STATIC); |
|
544 |
if ((matchingField == null) && overloads.size() == 0) { |
|
545 |
throw new ParseException("No static field or method with the name " |
|
546 |
+ memberName + " in " + refType.name()); |
|
547 |
} |
|
548 |
} |
|
549 |
||
9520 | 550 |
@Override |
2 | 551 |
Value getValue() throws InvocationException, InvalidTypeException, |
552 |
ClassNotLoadedException, IncompatibleThreadStateException, |
|
553 |
ParseException { |
|
554 |
if (jdiValue != null) { |
|
555 |
return jdiValue; |
|
556 |
} |
|
557 |
if (matchingMethod == null) { |
|
558 |
return jdiValue = refType.getValue(matchingField); |
|
559 |
} else if (refType instanceof ClassType) { |
|
560 |
ClassType clazz = (ClassType)refType; |
|
561 |
return jdiValue = clazz.invokeMethod(thread, matchingMethod, methodArguments, 0); |
|
562 |
} else { |
|
563 |
throw new InvalidTypeException("Cannot invoke static method on " + |
|
564 |
refType.name()); |
|
565 |
} |
|
566 |
} |
|
567 |
||
9520 | 568 |
@Override |
2 | 569 |
void setValue0(Value val) |
570 |
throws ParseException, InvalidTypeException, |
|
571 |
ClassNotLoadedException { |
|
572 |
if (matchingMethod != null) { |
|
573 |
throw new ParseException("Cannot assign to a method invocation"); |
|
574 |
} |
|
575 |
if (!(refType instanceof ClassType)) { |
|
576 |
throw new ParseException( |
|
577 |
"Cannot set interface field: " + refType); |
|
578 |
} |
|
579 |
((ClassType)refType).setValue(matchingField, val); |
|
580 |
jdiValue = val; |
|
581 |
} |
|
582 |
||
9520 | 583 |
@Override |
2 | 584 |
void invokeWith(List<Value> arguments) throws ParseException { |
585 |
if (matchingMethod != null) { |
|
586 |
throw new ParseException("Invalid consecutive invocations"); |
|
587 |
} |
|
588 |
methodArguments = arguments; |
|
589 |
matchingMethod = LValue.resolveOverload(overloads, arguments); |
|
590 |
} |
|
591 |
} |
|
592 |
||
593 |
private static class LValueArrayLength extends LValue { |
|
594 |
/* |
|
595 |
* Since one can code "int myLen = myArray.length;", |
|
596 |
* one might expect that these JDI calls would get a Value |
|
597 |
* object for the length of an array in the debugee: |
|
598 |
* Field xxx = ArrayType.fieldByName("length") |
|
599 |
* Value lenVal= ArrayReference.getValue(xxx) |
|
600 |
* |
|
601 |
* However, this doesn't work because the array length isn't |
|
602 |
* really stored as a field, and can't be accessed as such |
|
603 |
* via JDI. Instead, the arrayRef.length() method has to be |
|
604 |
* used. |
|
605 |
*/ |
|
606 |
final ArrayReference arrayRef; |
|
607 |
LValueArrayLength (ArrayReference value) { |
|
608 |
this.arrayRef = value; |
|
609 |
} |
|
610 |
||
9520 | 611 |
@Override |
2 | 612 |
Value getValue() { |
613 |
if (jdiValue == null) { |
|
614 |
jdiValue = arrayRef.virtualMachine().mirrorOf(arrayRef.length()); |
|
615 |
} |
|
616 |
return jdiValue; |
|
617 |
} |
|
618 |
||
9520 | 619 |
@Override |
2 | 620 |
void setValue0(Value value) throws ParseException { |
621 |
throw new ParseException("Cannot set constant: " + value); |
|
622 |
} |
|
623 |
||
9520 | 624 |
@Override |
2 | 625 |
void invokeWith(List<Value> arguments) throws ParseException { |
626 |
throw new ParseException("Array element is not a method"); |
|
627 |
} |
|
628 |
} |
|
629 |
||
630 |
private static class LValueArrayElement extends LValue { |
|
631 |
final ArrayReference array; |
|
632 |
final int index; |
|
633 |
||
634 |
LValueArrayElement(Value value, int index) throws ParseException { |
|
635 |
if (!(value instanceof ArrayReference)) { |
|
636 |
throw new ParseException( |
|
637 |
"Must be array type: " + value); |
|
638 |
} |
|
639 |
this.array = (ArrayReference)value; |
|
640 |
this.index = index; |
|
641 |
} |
|
642 |
||
9520 | 643 |
@Override |
2 | 644 |
Value getValue() { |
645 |
if (jdiValue == null) { |
|
646 |
jdiValue = array.getValue(index); |
|
647 |
} |
|
648 |
return jdiValue; |
|
649 |
} |
|
650 |
||
9520 | 651 |
@Override |
2 | 652 |
void setValue0(Value val) throws InvalidTypeException, |
653 |
ClassNotLoadedException { |
|
654 |
array.setValue(index, val); |
|
655 |
jdiValue = val; |
|
656 |
} |
|
657 |
||
9520 | 658 |
@Override |
2 | 659 |
void invokeWith(List<Value> arguments) throws ParseException { |
660 |
throw new ParseException("Array element is not a method"); |
|
661 |
} |
|
662 |
} |
|
663 |
||
664 |
private static class LValueConstant extends LValue { |
|
665 |
final Value value; |
|
666 |
||
667 |
LValueConstant(Value value) { |
|
668 |
this.value = value; |
|
669 |
} |
|
670 |
||
9520 | 671 |
@Override |
2 | 672 |
Value getValue() { |
673 |
if (jdiValue == null) { |
|
674 |
jdiValue = value; |
|
675 |
} |
|
676 |
return jdiValue; |
|
677 |
} |
|
678 |
||
9520 | 679 |
@Override |
2 | 680 |
void setValue0(Value val) throws ParseException { |
681 |
throw new ParseException("Cannot set constant: " + value); |
|
682 |
} |
|
683 |
||
9520 | 684 |
@Override |
2 | 685 |
void invokeWith(List<Value> arguments) throws ParseException { |
686 |
throw new ParseException("Constant is not a method"); |
|
687 |
} |
|
688 |
} |
|
689 |
||
690 |
static LValue make(VirtualMachine vm, boolean val) { |
|
691 |
return new LValueConstant(vm.mirrorOf(val)); |
|
692 |
} |
|
693 |
||
694 |
static LValue make(VirtualMachine vm, byte val) { |
|
695 |
return new LValueConstant(vm.mirrorOf(val)); |
|
696 |
} |
|
697 |
||
698 |
static LValue make(VirtualMachine vm, char val) { |
|
699 |
return new LValueConstant(vm.mirrorOf(val)); |
|
700 |
} |
|
701 |
||
702 |
static LValue make(VirtualMachine vm, short val) { |
|
703 |
return new LValueConstant(vm.mirrorOf(val)); |
|
704 |
} |
|
705 |
||
706 |
static LValue make(VirtualMachine vm, int val) { |
|
707 |
return new LValueConstant(vm.mirrorOf(val)); |
|
708 |
} |
|
709 |
||
710 |
static LValue make(VirtualMachine vm, long val) { |
|
711 |
return new LValueConstant(vm.mirrorOf(val)); |
|
712 |
} |
|
713 |
||
714 |
static LValue make(VirtualMachine vm, float val) { |
|
715 |
return new LValueConstant(vm.mirrorOf(val)); |
|
716 |
} |
|
717 |
||
718 |
static LValue make(VirtualMachine vm, double val) { |
|
719 |
return new LValueConstant(vm.mirrorOf(val)); |
|
720 |
} |
|
721 |
||
722 |
static LValue make(VirtualMachine vm, String val) throws ParseException { |
|
723 |
return new LValueConstant(vm.mirrorOf(val)); |
|
724 |
} |
|
725 |
||
726 |
static LValue makeBoolean(VirtualMachine vm, Token token) { |
|
727 |
return make(vm, token.image.charAt(0) == 't'); |
|
728 |
} |
|
729 |
||
730 |
static LValue makeCharacter(VirtualMachine vm, Token token) { |
|
731 |
return make(vm, token.image.charAt(1)); |
|
732 |
} |
|
733 |
||
734 |
static LValue makeFloat(VirtualMachine vm, Token token) { |
|
735 |
return make(vm, Float.valueOf(token.image).floatValue()); |
|
736 |
} |
|
737 |
||
738 |
static LValue makeDouble(VirtualMachine vm, Token token) { |
|
739 |
return make(vm, Double.valueOf(token.image).doubleValue()); |
|
740 |
} |
|
741 |
||
742 |
static LValue makeInteger(VirtualMachine vm, Token token) { |
|
743 |
return make(vm, Integer.parseInt(token.image)); |
|
744 |
} |
|
745 |
||
746 |
static LValue makeShort(VirtualMachine vm, Token token) { |
|
747 |
return make(vm, Short.parseShort(token.image)); |
|
748 |
} |
|
749 |
||
750 |
static LValue makeLong(VirtualMachine vm, Token token) { |
|
751 |
return make(vm, Long.parseLong(token.image)); |
|
752 |
} |
|
753 |
||
754 |
static LValue makeByte(VirtualMachine vm, Token token) { |
|
755 |
return make(vm, Byte.parseByte(token.image)); |
|
756 |
} |
|
757 |
||
758 |
static LValue makeString(VirtualMachine vm, |
|
759 |
Token token) throws ParseException { |
|
760 |
int len = token.image.length(); |
|
761 |
return make(vm, token.image.substring(1,len-1)); |
|
762 |
} |
|
763 |
||
764 |
static LValue makeNull(VirtualMachine vm, |
|
765 |
Token token) throws ParseException { |
|
766 |
return new LValueConstant(null); |
|
767 |
} |
|
768 |
||
769 |
static LValue makeThisObject(VirtualMachine vm, |
|
770 |
ExpressionParser.GetFrame frameGetter, |
|
771 |
Token token) throws ParseException { |
|
772 |
if (frameGetter == null) { |
|
773 |
throw new ParseException("No current thread"); |
|
774 |
} else { |
|
775 |
try { |
|
776 |
StackFrame frame = frameGetter.get(); |
|
777 |
ObjectReference thisObject = frame.thisObject(); |
|
778 |
||
779 |
if (thisObject==null) { |
|
780 |
throw new ParseException( |
|
781 |
"No 'this'. In native or static method"); |
|
782 |
} else { |
|
783 |
return new LValueConstant(thisObject); |
|
784 |
} |
|
785 |
} catch (IncompatibleThreadStateException exc) { |
|
786 |
throw new ParseException("Thread not suspended"); |
|
787 |
} |
|
788 |
} |
|
789 |
} |
|
790 |
||
791 |
static LValue makeNewObject(VirtualMachine vm, |
|
792 |
ExpressionParser.GetFrame frameGetter, |
|
793 |
String className, List<Value> arguments) throws ParseException { |
|
51 | 794 |
List<ReferenceType> classes = vm.classesByName(className); |
2 | 795 |
if (classes.size() == 0) { |
796 |
throw new ParseException("No class named: " + className); |
|
797 |
} |
|
798 |
||
799 |
if (classes.size() > 1) { |
|
800 |
throw new ParseException("More than one class named: " + |
|
801 |
className); |
|
802 |
} |
|
51 | 803 |
ReferenceType refType = classes.get(0); |
2 | 804 |
|
805 |
||
806 |
if (!(refType instanceof ClassType)) { |
|
807 |
throw new ParseException("Cannot create instance of interface " + |
|
808 |
className); |
|
809 |
} |
|
810 |
||
811 |
ClassType classType = (ClassType)refType; |
|
812 |
List<Method> methods = new ArrayList<Method>(classType.methods()); // writable |
|
51 | 813 |
Iterator<Method> iter = methods.iterator(); |
2 | 814 |
while (iter.hasNext()) { |
51 | 815 |
Method method = iter.next(); |
2 | 816 |
if (!method.isConstructor()) { |
817 |
iter.remove(); |
|
818 |
} |
|
819 |
} |
|
820 |
Method constructor = LValue.resolveOverload(methods, arguments); |
|
821 |
||
822 |
ObjectReference newObject; |
|
823 |
try { |
|
824 |
ThreadReference thread = frameGetter.get().thread(); |
|
825 |
newObject = classType.newInstance(thread, constructor, arguments, 0); |
|
826 |
} catch (InvocationException ie) { |
|
827 |
throw new ParseException("Exception in " + className + " constructor: " + |
|
828 |
ie.exception().referenceType().name()); |
|
829 |
} catch (IncompatibleThreadStateException exc) { |
|
830 |
throw new ParseException("Thread not suspended"); |
|
831 |
} catch (Exception e) { |
|
832 |
/* |
|
833 |
* TO DO: Better error handling |
|
834 |
*/ |
|
835 |
throw new ParseException("Unable to create " + className + " instance"); |
|
836 |
} |
|
837 |
return new LValueConstant(newObject); |
|
838 |
} |
|
839 |
||
840 |
private static LValue nFields(LValue lval, |
|
841 |
StringTokenizer izer, |
|
842 |
ThreadReference thread) |
|
843 |
throws ParseException { |
|
844 |
if (!izer.hasMoreTokens()) { |
|
845 |
return lval; |
|
846 |
} else { |
|
847 |
return nFields(lval.memberLValue(izer.nextToken(), thread), izer, thread); |
|
848 |
} |
|
849 |
} |
|
850 |
||
851 |
static LValue makeName(VirtualMachine vm, |
|
852 |
ExpressionParser.GetFrame frameGetter, |
|
853 |
String name) throws ParseException { |
|
854 |
StringTokenizer izer = new StringTokenizer(name, "."); |
|
855 |
String first = izer.nextToken(); |
|
856 |
// check local variables |
|
857 |
if (frameGetter != null) { |
|
858 |
try { |
|
859 |
StackFrame frame = frameGetter.get(); |
|
860 |
ThreadReference thread = frame.thread(); |
|
861 |
LocalVariable var; |
|
862 |
try { |
|
863 |
var = frame.visibleVariableByName(first); |
|
864 |
} catch (AbsentInformationException e) { |
|
865 |
var = null; |
|
866 |
} |
|
867 |
if (var != null) { |
|
868 |
return nFields(new LValueLocal(frame, var), izer, thread); |
|
869 |
} else { |
|
870 |
ObjectReference thisObject = frame.thisObject(); |
|
871 |
if (thisObject != null) { |
|
872 |
// check if it is a field of 'this' |
|
873 |
LValue thisLValue = new LValueConstant(thisObject); |
|
874 |
LValue fv; |
|
875 |
try { |
|
876 |
fv = thisLValue.memberLValue(first, thread); |
|
877 |
} catch (ParseException exc) { |
|
878 |
fv = null; |
|
879 |
} |
|
880 |
if (fv != null) { |
|
881 |
return nFields(fv, izer, thread); |
|
882 |
} |
|
883 |
} |
|
884 |
} |
|
885 |
// check for class name |
|
886 |
while (izer.hasMoreTokens()) { |
|
51 | 887 |
List<ReferenceType> classes = vm.classesByName(first); |
2 | 888 |
if (classes.size() > 0) { |
889 |
if (classes.size() > 1) { |
|
890 |
throw new ParseException("More than one class named: " + |
|
891 |
first); |
|
892 |
} else { |
|
51 | 893 |
ReferenceType refType = classes.get(0); |
2 | 894 |
LValue lval = new LValueStaticMember(refType, |
895 |
izer.nextToken(), thread); |
|
896 |
return nFields(lval, izer, thread); |
|
897 |
} |
|
898 |
} |
|
899 |
first = first + '.' + izer.nextToken(); |
|
900 |
} |
|
901 |
} catch (IncompatibleThreadStateException exc) { |
|
902 |
throw new ParseException("Thread not suspended"); |
|
903 |
} |
|
904 |
} |
|
905 |
throw new ParseException("Name unknown: " + name); |
|
906 |
} |
|
907 |
||
908 |
static String stringValue(LValue lval, ExpressionParser.GetFrame frameGetter |
|
909 |
) throws ParseException { |
|
910 |
Value val = lval.getMassagedValue(frameGetter); |
|
911 |
if (val == null) { |
|
912 |
return "null"; |
|
913 |
} |
|
914 |
if (val instanceof StringReference) { |
|
915 |
return ((StringReference)val).value(); |
|
916 |
} |
|
917 |
return val.toString(); // is this correct in all cases? |
|
918 |
} |
|
919 |
||
920 |
static LValue booleanOperation(VirtualMachine vm, Token token, |
|
921 |
LValue rightL, |
|
922 |
LValue leftL) throws ParseException { |
|
923 |
String op = token.image; |
|
924 |
Value right = rightL.interiorGetValue(); |
|
925 |
Value left = leftL.interiorGetValue(); |
|
926 |
if ( !(right instanceof PrimitiveValue) || |
|
927 |
!(left instanceof PrimitiveValue) ) { |
|
928 |
if (op.equals("==")) { |
|
929 |
return make(vm, right.equals(left)); |
|
930 |
} else if (op.equals("!=")) { |
|
931 |
return make(vm, !right.equals(left)); |
|
932 |
} else { |
|
933 |
throw new ParseException("Operands or '" + op + |
|
934 |
"' must be primitive"); |
|
935 |
} |
|
936 |
} |
|
937 |
// can compare any numeric doubles |
|
938 |
double rr = ((PrimitiveValue)right).doubleValue(); |
|
939 |
double ll = ((PrimitiveValue)left).doubleValue(); |
|
940 |
boolean res; |
|
941 |
if (op.equals("<")) { |
|
942 |
res = rr < ll; |
|
943 |
} else if (op.equals(">")) { |
|
944 |
res = rr > ll; |
|
945 |
} else if (op.equals("<=")) { |
|
946 |
res = rr <= ll; |
|
947 |
} else if (op.equals(">=")) { |
|
948 |
res = rr >= ll; |
|
949 |
} else if (op.equals("==")) { |
|
950 |
res = rr == ll; |
|
951 |
} else if (op.equals("!=")) { |
|
952 |
res = rr != ll; |
|
953 |
} else { |
|
954 |
throw new ParseException("Unknown operation: " + op); |
|
955 |
} |
|
956 |
return make(vm, res); |
|
957 |
} |
|
958 |
||
959 |
static LValue operation(VirtualMachine vm, Token token, |
|
960 |
LValue rightL, LValue leftL, |
|
961 |
ExpressionParser.GetFrame frameGetter |
|
962 |
) throws ParseException { |
|
963 |
String op = token.image; |
|
964 |
Value right = rightL.interiorGetValue(); |
|
965 |
Value left = leftL.interiorGetValue(); |
|
966 |
if ((right instanceof StringReference) || |
|
967 |
(left instanceof StringReference)) { |
|
968 |
if (op.equals("+")) { |
|
969 |
// If one is an ObjectRef, we will need to invoke |
|
970 |
// toString on it, so we need the thread. |
|
971 |
return make(vm, stringValue(rightL, frameGetter) + |
|
972 |
stringValue(leftL, frameGetter)); |
|
973 |
} |
|
974 |
} |
|
975 |
if ((right instanceof ObjectReference) || |
|
976 |
(left instanceof ObjectReference)) { |
|
977 |
if (op.equals("==")) { |
|
978 |
return make(vm, right.equals(left)); |
|
979 |
} else if (op.equals("!=")) { |
|
980 |
return make(vm, !right.equals(left)); |
|
981 |
} else { |
|
982 |
throw new ParseException("Invalid operation '" + |
|
983 |
op + "' on an Object"); |
|
984 |
} |
|
985 |
} |
|
986 |
if ((right instanceof BooleanValue) || |
|
987 |
(left instanceof BooleanValue)) { |
|
988 |
throw new ParseException("Invalid operation '" + |
|
989 |
op + "' on a Boolean"); |
|
990 |
} |
|
991 |
// from here on, we know it is a integer kind of type |
|
992 |
PrimitiveValue primRight = (PrimitiveValue)right; |
|
993 |
PrimitiveValue primLeft = (PrimitiveValue)left; |
|
994 |
if ((primRight instanceof DoubleValue) || |
|
995 |
(primLeft instanceof DoubleValue)) { |
|
996 |
double rr = primRight.doubleValue(); |
|
997 |
double ll = primLeft.doubleValue(); |
|
998 |
double res; |
|
999 |
if (op.equals("+")) { |
|
1000 |
res = rr + ll; |
|
1001 |
} else if (op.equals("-")) { |
|
1002 |
res = rr - ll; |
|
1003 |
} else if (op.equals("*")) { |
|
1004 |
res = rr * ll; |
|
1005 |
} else if (op.equals("/")) { |
|
1006 |
res = rr / ll; |
|
1007 |
} else { |
|
1008 |
throw new ParseException("Unknown operation: " + op); |
|
1009 |
} |
|
1010 |
return make(vm, res); |
|
1011 |
} |
|
1012 |
if ((primRight instanceof FloatValue) || |
|
1013 |
(primLeft instanceof FloatValue)) { |
|
1014 |
float rr = primRight.floatValue(); |
|
1015 |
float ll = primLeft.floatValue(); |
|
1016 |
float res; |
|
1017 |
if (op.equals("+")) { |
|
1018 |
res = rr + ll; |
|
1019 |
} else if (op.equals("-")) { |
|
1020 |
res = rr - ll; |
|
1021 |
} else if (op.equals("*")) { |
|
1022 |
res = rr * ll; |
|
1023 |
} else if (op.equals("/")) { |
|
1024 |
res = rr / ll; |
|
1025 |
} else { |
|
1026 |
throw new ParseException("Unknown operation: " + op); |
|
1027 |
} |
|
1028 |
return make(vm, res); |
|
1029 |
} |
|
1030 |
if ((primRight instanceof LongValue) || |
|
1031 |
(primLeft instanceof LongValue)) { |
|
1032 |
long rr = primRight.longValue(); |
|
1033 |
long ll = primLeft.longValue(); |
|
1034 |
long res; |
|
1035 |
if (op.equals("+")) { |
|
1036 |
res = rr + ll; |
|
1037 |
} else if (op.equals("-")) { |
|
1038 |
res = rr - ll; |
|
1039 |
} else if (op.equals("*")) { |
|
1040 |
res = rr * ll; |
|
1041 |
} else if (op.equals("/")) { |
|
1042 |
res = rr / ll; |
|
1043 |
} else { |
|
1044 |
throw new ParseException("Unknown operation: " + op); |
|
1045 |
} |
|
1046 |
return make(vm, res); |
|
1047 |
} else { |
|
1048 |
int rr = primRight.intValue(); |
|
1049 |
int ll = primLeft.intValue(); |
|
1050 |
int res; |
|
1051 |
if (op.equals("+")) { |
|
1052 |
res = rr + ll; |
|
1053 |
} else if (op.equals("-")) { |
|
1054 |
res = rr - ll; |
|
1055 |
} else if (op.equals("*")) { |
|
1056 |
res = rr * ll; |
|
1057 |
} else if (op.equals("/")) { |
|
1058 |
res = rr / ll; |
|
1059 |
} else { |
|
1060 |
throw new ParseException("Unknown operation: " + op); |
|
1061 |
} |
|
1062 |
return make(vm, res); |
|
1063 |
} |
|
1064 |
} |
|
1065 |
} |