public static Value eval(ThreadReference threadRef, Expression exp,
ObjectReference thisObj,boolean hasParents) {
if (exp == null)
return null;
VirtualMachine vm = threadRef.virtualMachine();
String expType = exp.getExpType();
String expName = exp.getName();
if (expType.equals(Expression.EXP_TYPE_BOOL)) {
if (expName.equals("true"))
return vm.mirrorOf(true);
else
return vm.mirrorOf(false);
} else if (expType.equals(Expression.EXP_TYPE_STR)) {
return vm.mirrorOf(expName);
} else if (expType.equals(Expression.EXP_TYPE_NULL)) {
return null;
} else if (expType.equals(Expression.EXP_TYPE_NUM)) {
if (expName.indexOf(".") > -1) {
return vm.mirrorOf(Float.parseFloat(expName));
} else {
return vm.mirrorOf(Integer.parseInt(expName));
}
}
Value basicExpValue = null;
if (!exp.isMethod()) {
basicExpValue = findValueInFrame(threadRef, expName,thisObj,hasParents);
} else {
List<Expression> params = exp.getParams();
List<Value> arguments = new ArrayList<Value>();
if (params.size() != 0) {
for (Expression param : params) {
Value paramValue = eval(threadRef, param, thisObj,false);
arguments.add(paramValue);
}
}
basicExpValue = invoke((ObjectReference) thisObj, expName, arguments);
}
if (exp.isArrayExp()) {
if (basicExpValue instanceof ArrayReference) {
ArrayReference array = (ArrayReference)basicExpValue;
Value arrayIdxValue = eval(threadRef, exp.getArrayIdxExp(), thisObj,false);
if (arrayIdxValue instanceof IntegerValue ) {
int idx = ((IntegerValue)arrayIdxValue).value();
basicExpValue = array.getValue(idx);
} else {
throw new ExpressionEvalException("eval expression error, array index is not int type.");
}
}
}
List<Expression> members = exp.getMembers();
if (members.size() == 0)
return basicExpValue;
if (exp.isStaticMember()) {
Expression memberExp = members.get(0);
List<Expression> params = memberExp.getParams();
List<Value> arguments = new ArrayList<Value>();
if (params.size() != 0) {
for (Expression param : params) {
Value paramValue = eval(threadRef, param, thisObj,false);
arguments.add(paramValue);
}
}
List<ReferenceType> refTypes = vm.classesByName(exp.getName());
if (refTypes == null || refTypes.size() == 0) {
throw new ExpressionEvalException("eval expression error, type '" + exp.getName() + "' can't be found ");
}
basicExpValue = invoke(refTypes.get(0), memberExp.getName(), arguments);