/** * 弹出(组装好的)操作符 * * @return 操作符 */ Operator popOperator() { if (operatorStack.isEmpty()) throw I18nExceptionFactory.createIllegalStateException( "ExpressionReducer.miss.left.parenthesis"); Operator operator = (Operator) operatorStack.pop(); // 从参数栈弹出所需参数组装操作表达式 if (operator instanceof BinaryOperator) { BinaryOperator binaryOperator = (BinaryOperator) operator; if (parameterStack.isEmpty()) throw I18nExceptionFactory.createIllegalStateException( "ExpressionReducer.operator.miss.parameter"); Expression rightParameter = (Expression) parameterStack.pop(); // 调整参数顺序 if (parameterStack.isEmpty()) throw I18nExceptionFactory.createIllegalStateException( "ExpressionReducer.operator.miss.parameter"); Expression leftParameter = (Expression) parameterStack.pop(); parameterStack.push( populateBinaryOperator( (BinaryOperatorImpl) binaryOperator, leftParameter, rightParameter)); // 将组装好的表达式作为参数压入 } else if (operator instanceof UnaryOperator) { UnaryOperator unaryOperator = (UnaryOperator) operator; if (parameterStack.isEmpty()) throw I18nExceptionFactory.createIllegalStateException( "ExpressionReducer.operator.miss.parameter"); Expression parameter = (Expression) parameterStack.pop(); parameterStack.push( populateUnaryOperator((UnaryOperatorImpl) unaryOperator, parameter)); // 将组装好的表达式作为参数压入 } return operator; }
/** * 弹出(最后的)结果 * * @return 结果 */ Expression popResult() { while (!operatorStack.isEmpty()) { Operator operator = popOperator(); // 弹出栈中所有操作符 if (operator == Parenthesis.LEFT_PARENTHESIS) throw I18nExceptionFactory.createIllegalStateException( "ExpressionReducer.miss.right.parenthesis"); } Expression result = (Expression) parameterStack.pop(); Assert.assertTrue( parameterStack.isEmpty(), "ExpressionReducer.operator.miss.parameter"); // 后验条件 return result; }
/** * 处理属性 * * @param propertyName 属性或类名 * @return 类元或类静态属性值 * @throws Exception 类或属性不存在时抛出 */ public Object doEvaluateProperties(String propertyName) throws Exception { String className = propertyName; Class clazz = ClassUtils.safeForName(className); Stack properties = new LinkedStack(); int index; while (clazz == null && (index = className.lastIndexOf('.')) != -1) { properties.push(className.substring(index + 1)); className = className.substring(0, index); clazz = ClassUtils.safeForName(className); } Assert.assertNotNull(clazz, "不存在类:{0}", new Object[] {propertyName}); Object result = convertResult(clazz); while (!properties.isEmpty()) { result = dotBinaryOperatorHandler.doEvaluate(result, properties.pop()); } return result; }
/** * 处理函数 * * @param function 函数 * @return 函数值 * @throws Exception 类或属性不存在时抛出 */ public Object doEvaluateFunction(Function function) throws Exception { String name = function.getName(); int index = name.lastIndexOf('.'); Assert.assertTrue(index != -1, "不存在类:{0}", new Object[] {name}); String propertyName = name.substring(0, index); // 最点号前的作为类名或静态属性名 String className = propertyName; Function callFunction = new Function(name.substring(index + 1), function.getArgument()); // 将点号最后一截作为静态函数名 Class clazz = ClassUtils.safeForName(className); Stack properties = new LinkedStack(); while (clazz == null && (index = className.lastIndexOf('.')) != -1) { properties.push(className.substring(index + 1)); className = className.substring(0, index); clazz = ClassUtils.safeForName(className); } Assert.assertNotNull(clazz, "不存在类:{0}", new Object[] {propertyName}); Object result = convertResult(clazz); while (!properties.isEmpty()) { result = dotBinaryOperatorHandler.doEvaluate(result, properties.pop()); } return dotBinaryOperatorHandler.doEvaluate(result, callFunction); }