lvalue.java
来自「jpda例子文件」· Java 代码 · 共 1,049 行 · 第 1/3 页
JAVA
1,049 行
static LValue make(VirtualMachine vm, double val) { return new LValueConstant(vm.mirrorOf(val)); } static LValue make(VirtualMachine vm, String val) throws ParseException { return new LValueConstant(vm.mirrorOf(val)); } static LValue makeBoolean(VirtualMachine vm, Token token) { return make(vm, token.image.charAt(0) == 't'); } static LValue makeCharacter(VirtualMachine vm, Token token) { return make(vm, token.image.charAt(1)); } static LValue makeFloat(VirtualMachine vm, Token token) { return make(vm, Float.valueOf(token.image).floatValue()); } static LValue makeDouble(VirtualMachine vm, Token token) { return make(vm, Double.valueOf(token.image).doubleValue()); } static LValue makeInteger(VirtualMachine vm, Token token) { return make(vm, Integer.parseInt(token.image)); } static LValue makeShort(VirtualMachine vm, Token token) { return make(vm, Short.parseShort(token.image)); } static LValue makeLong(VirtualMachine vm, Token token) { return make(vm, Long.parseLong(token.image)); } static LValue makeByte(VirtualMachine vm, Token token) { return make(vm, Byte.parseByte(token.image)); } static LValue makeString(VirtualMachine vm, Token token) throws ParseException { int len = token.image.length(); return make(vm, token.image.substring(1,len-1)); } static LValue makeNull(VirtualMachine vm, Token token) throws ParseException { return new LValueConstant(null); } static LValue makeThisObject(VirtualMachine vm, ExpressionParser.GetFrame frameGetter, Token token) throws ParseException { if (frameGetter == null) { throw new ParseException("No current thread"); } else { try { StackFrame frame = frameGetter.get(); ObjectReference thisObject = frame.thisObject(); if (thisObject==null) { throw new ParseException( "No 'this'. In native or static method"); } else { return new LValueConstant(thisObject); } } catch (IncompatibleThreadStateException exc) { throw new ParseException("Thread not suspended"); } } } static LValue makeNewObject(VirtualMachine vm, ExpressionParser.GetFrame frameGetter, String className, List arguments) throws ParseException { List classes = vm.classesByName(className); if (classes.size() == 0) { throw new ParseException("No class named: " + className); } if (classes.size() > 1) { throw new ParseException("More than one class named: " + className); } ReferenceType refType = (ReferenceType)classes.get(0); if (!(refType instanceof ClassType)) { throw new ParseException("Cannot create instance of interface " + className); } ClassType classType = (ClassType)refType; List methods = new ArrayList(classType.methods()); // writable Iterator iter = methods.iterator(); while (iter.hasNext()) { Method method = (Method)iter.next(); if (!method.isConstructor()) { iter.remove(); } } Method constructor = LValue.resolveOverload(methods, arguments); ObjectReference newObject; try { ThreadReference thread = frameGetter.get().thread(); newObject = classType.newInstance(thread, constructor, arguments, 0); } catch (InvocationException ie) { throw new ParseException("Exception in " + className + " constructor: " + ie.exception().referenceType().name()); } catch (IncompatibleThreadStateException exc) { throw new ParseException("Thread not suspended"); } catch (Exception e) { /* * TO DO: Better error handling */ throw new ParseException("Unable to create " + className + " instance"); } return new LValueConstant(newObject); } private static LValue nFields(LValue lval, StringTokenizer izer, ThreadReference thread) throws ParseException { if (!izer.hasMoreTokens()) { return lval; } else { return nFields(lval.memberLValue(izer.nextToken(), thread), izer, thread); } } static LValue makeName(VirtualMachine vm, ExpressionParser.GetFrame frameGetter, String name) throws ParseException { StringTokenizer izer = new StringTokenizer(name, "."); String first = izer.nextToken(); // check local variables if (frameGetter != null) { try { StackFrame frame = frameGetter.get(); ThreadReference thread = frame.thread(); LocalVariable var; try { var = frame.visibleVariableByName(first); } catch (AbsentInformationException e) { var = null; } if (var != null) { return nFields(new LValueLocal(frame, var), izer, thread); } else { ObjectReference thisObject = frame.thisObject(); if (thisObject != null) { // check if it is a field of 'this' LValue thisLValue = new LValueConstant(thisObject); LValue fv; try { fv = thisLValue.memberLValue(first, thread); } catch (ParseException exc) { fv = null; } if (fv != null) { return nFields(fv, izer, thread); } } } // check for class name while (izer.hasMoreTokens()) { List classes = vm.classesByName(first); if (classes.size() > 0) { if (classes.size() > 1) { throw new ParseException("More than one class named: " + first); } else { ReferenceType refType = (ReferenceType)classes.get(0); LValue lval = new LValueStaticMember(refType, izer.nextToken(), thread); return nFields(lval, izer, thread); } } first = first + '.' + izer.nextToken(); } } catch (IncompatibleThreadStateException exc) { throw new ParseException("Thread not suspended"); } } throw new ParseException("Name unknown: " + name); } static String stringValue(LValue lval, ExpressionParser.GetFrame frameGetter ) throws ParseException { Value val = lval.getMassagedValue(frameGetter); if (val == null) { return "null"; } if (val instanceof StringReference) { return ((StringReference)val).value(); } return val.toString(); // is this correct in all cases? } static LValue booleanOperation(VirtualMachine vm, Token token, LValue rightL, LValue leftL) throws ParseException { String op = token.image; Value right = rightL.interiorGetValue(); Value left = leftL.interiorGetValue(); if ( !(right instanceof PrimitiveValue) || !(left instanceof PrimitiveValue) ) { if (op.equals("==")) { return make(vm, right.equals(left)); } else if (op.equals("!=")) { return make(vm, !right.equals(left)); } else { throw new ParseException("Operands or '" + op + "' must be primitive"); } } // can compare any numeric doubles double rr = ((PrimitiveValue)right).doubleValue(); double ll = ((PrimitiveValue)left).doubleValue(); boolean res; if (op.equals("<")) { res = rr < ll; } else if (op.equals(">")) { res = rr > ll; } else if (op.equals("<=")) { res = rr <= ll; } else if (op.equals(">=")) { res = rr >= ll; } else if (op.equals("==")) { res = rr == ll; } else if (op.equals("!=")) { res = rr != ll; } else { throw new ParseException("Unknown operation: " + op); } return make(vm, res); } static LValue operation(VirtualMachine vm, Token token, LValue rightL, LValue leftL, ExpressionParser.GetFrame frameGetter ) throws ParseException { String op = token.image; Value right = rightL.interiorGetValue(); Value left = leftL.interiorGetValue(); if ((right instanceof StringReference) || (left instanceof StringReference)) { if (op.equals("+")) { // If one is an ObjectRef, we will need to invoke // toString on it, so we need the thread. return make(vm, stringValue(rightL, frameGetter) + stringValue(leftL, frameGetter)); } } if ((right instanceof ObjectReference) || (left instanceof ObjectReference)) { if (op.equals("==")) { return make(vm, right.equals(left)); } else if (op.equals("!=")) { return make(vm, !right.equals(left)); } else { throw new ParseException("Invalid operation '" + op + "' on an Object"); } } if ((right instanceof BooleanValue) || (left instanceof BooleanValue)) { throw new ParseException("Invalid operation '" + op + "' on a Boolean"); } // from here on, we know it is a integer kind of type PrimitiveValue primRight = (PrimitiveValue)right; PrimitiveValue primLeft = (PrimitiveValue)left; if ((primRight instanceof DoubleValue) || (primLeft instanceof DoubleValue)) { double rr = primRight.doubleValue(); double ll = primLeft.doubleValue(); double res; if (op.equals("+")) { res = rr + ll; } else if (op.equals("-")) { res = rr - ll; } else if (op.equals("*")) { res = rr * ll; } else if (op.equals("/")) { res = rr / ll; } else { throw new ParseException("Unknown operation: " + op); } return make(vm, res); } if ((primRight instanceof FloatValue) || (primLeft instanceof FloatValue)) { float rr = primRight.floatValue(); float ll = primLeft.floatValue(); float res; if (op.equals("+")) { res = rr + ll; } else if (op.equals("-")) { res = rr - ll; } else if (op.equals("*")) { res = rr * ll; } else if (op.equals("/")) { res = rr / ll; } else { throw new ParseException("Unknown operation: " + op); } return make(vm, res); } if ((primRight instanceof LongValue) || (primLeft instanceof LongValue)) { long rr = primRight.longValue(); long ll = primLeft.longValue(); long res; if (op.equals("+")) { res = rr + ll; } else if (op.equals("-")) { res = rr - ll; } else if (op.equals("*")) { res = rr * ll; } else if (op.equals("/")) { res = rr / ll; } else { throw new ParseException("Unknown operation: " + op); } return make(vm, res); } else { int rr = primRight.intValue(); int ll = primLeft.intValue(); int res; if (op.equals("+")) { res = rr + ll; } else if (op.equals("-")) { res = rr - ll; } else if (op.equals("*")) { res = rr * ll; } else if (op.equals("/")) { res = rr / ll; } else { throw new ParseException("Unknown operation: " + op); } return make(vm, res); } } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?