Java Code Examples for org.springframework.expression.spel.ExpressionState#operate()
The following examples show how to use
org.springframework.expression.spel.ExpressionState#operate() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OperatorPower.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { SpelNodeImpl leftOp = getLeftOperand(); SpelNodeImpl rightOp = getRightOperand(); Object leftOperand = leftOp.getValueInternal(state).getValue(); Object rightOperand = rightOp.getValueInternal(state).getValue(); if (leftOperand instanceof Number && rightOperand instanceof Number) { Number leftNumber = (Number) leftOperand; Number rightNumber = (Number) rightOperand; if (leftNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); return new TypedValue(leftBigDecimal.pow(rightNumber.intValue())); } else if (leftNumber instanceof BigInteger) { BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class); return new TypedValue(leftBigInteger.pow(rightNumber.intValue())); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { return new TypedValue(Math.pow(leftNumber.doubleValue(), rightNumber.doubleValue())); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { return new TypedValue(Math.pow(leftNumber.floatValue(), rightNumber.floatValue())); } double d = Math.pow(leftNumber.doubleValue(), rightNumber.doubleValue()); if (d > Integer.MAX_VALUE || leftNumber instanceof Long || rightNumber instanceof Long) { return new TypedValue((long) d); } else { return new TypedValue((int) d); } } return state.operate(Operation.POWER, leftOperand, rightOperand); }
Example 2
Source File: OperatorPower.java From java-technology-stack with MIT License | 5 votes |
@Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { SpelNodeImpl leftOp = getLeftOperand(); SpelNodeImpl rightOp = getRightOperand(); Object leftOperand = leftOp.getValueInternal(state).getValue(); Object rightOperand = rightOp.getValueInternal(state).getValue(); if (leftOperand instanceof Number && rightOperand instanceof Number) { Number leftNumber = (Number) leftOperand; Number rightNumber = (Number) rightOperand; if (leftNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); return new TypedValue(leftBigDecimal.pow(rightNumber.intValue())); } else if (leftNumber instanceof BigInteger) { BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class); return new TypedValue(leftBigInteger.pow(rightNumber.intValue())); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { return new TypedValue(Math.pow(leftNumber.doubleValue(), rightNumber.doubleValue())); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { return new TypedValue(Math.pow(leftNumber.floatValue(), rightNumber.floatValue())); } double d = Math.pow(leftNumber.doubleValue(), rightNumber.doubleValue()); if (d > Integer.MAX_VALUE || leftNumber instanceof Long || rightNumber instanceof Long) { return new TypedValue((long) d); } else { return new TypedValue((int) d); } } return state.operate(Operation.POWER, leftOperand, rightOperand); }
Example 3
Source File: OpPlus.java From java-technology-stack with MIT License | 4 votes |
@Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { SpelNodeImpl leftOp = getLeftOperand(); if (this.children.length < 2) { // if only one operand, then this is unary plus Object operandOne = leftOp.getValueInternal(state).getValue(); if (operandOne instanceof Number) { if (operandOne instanceof Double) { this.exitTypeDescriptor = "D"; } else if (operandOne instanceof Float) { this.exitTypeDescriptor = "F"; } else if (operandOne instanceof Long) { this.exitTypeDescriptor = "J"; } else if (operandOne instanceof Integer) { this.exitTypeDescriptor = "I"; } return new TypedValue(operandOne); } return state.operate(Operation.ADD, operandOne, null); } TypedValue operandOneValue = leftOp.getValueInternal(state); Object leftOperand = operandOneValue.getValue(); TypedValue operandTwoValue = getRightOperand().getValueInternal(state); Object rightOperand = operandTwoValue.getValue(); if (leftOperand instanceof Number && rightOperand instanceof Number) { Number leftNumber = (Number) leftOperand; Number rightNumber = (Number) rightOperand; if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); return new TypedValue(leftBigDecimal.add(rightBigDecimal)); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { this.exitTypeDescriptor = "D"; return new TypedValue(leftNumber.doubleValue() + rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { this.exitTypeDescriptor = "F"; return new TypedValue(leftNumber.floatValue() + rightNumber.floatValue()); } else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) { BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class); BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class); return new TypedValue(leftBigInteger.add(rightBigInteger)); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { this.exitTypeDescriptor = "J"; return new TypedValue(leftNumber.longValue() + rightNumber.longValue()); } else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) { this.exitTypeDescriptor = "I"; return new TypedValue(leftNumber.intValue() + rightNumber.intValue()); } else { // Unknown Number subtypes -> best guess is double addition return new TypedValue(leftNumber.doubleValue() + rightNumber.doubleValue()); } } if (leftOperand instanceof String && rightOperand instanceof String) { this.exitTypeDescriptor = "Ljava/lang/String"; return new TypedValue((String) leftOperand + rightOperand); } if (leftOperand instanceof String) { return new TypedValue( leftOperand + (rightOperand == null ? "null" : convertTypedValueToString(operandTwoValue, state))); } if (rightOperand instanceof String) { return new TypedValue( (leftOperand == null ? "null" : convertTypedValueToString(operandOneValue, state)) + rightOperand); } return state.operate(Operation.ADD, leftOperand, rightOperand); }
Example 4
Source File: OpDivide.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { Object leftOperand = getLeftOperand().getValueInternal(state).getValue(); Object rightOperand = getRightOperand().getValueInternal(state).getValue(); if (leftOperand instanceof Number && rightOperand instanceof Number) { Number leftNumber = (Number) leftOperand; Number rightNumber = (Number) rightOperand; if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); int scale = Math.max(leftBigDecimal.scale(), rightBigDecimal.scale()); return new TypedValue(leftBigDecimal.divide(rightBigDecimal, scale, RoundingMode.HALF_EVEN)); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { this.exitTypeDescriptor = "D"; return new TypedValue(leftNumber.doubleValue() / rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { this.exitTypeDescriptor = "F"; return new TypedValue(leftNumber.floatValue() / rightNumber.floatValue()); } else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) { BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class); BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class); return new TypedValue(leftBigInteger.divide(rightBigInteger)); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { this.exitTypeDescriptor = "J"; return new TypedValue(leftNumber.longValue() / rightNumber.longValue()); } else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) { this.exitTypeDescriptor = "I"; return new TypedValue(leftNumber.intValue() / rightNumber.intValue()); } else { // Unknown Number subtypes -> best guess is double division return new TypedValue(leftNumber.doubleValue() / rightNumber.doubleValue()); } } return state.operate(Operation.DIVIDE, leftOperand, rightOperand); }
Example 5
Source File: OpMinus.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { SpelNodeImpl leftOp = getLeftOperand(); SpelNodeImpl rightOp = getRightOperand(); if (rightOp == null) { // if only one operand, then this is unary minus Object operand = leftOp.getValueInternal(state).getValue(); if (operand instanceof Number) { if (operand instanceof BigDecimal) { return new TypedValue(((BigDecimal) operand).negate()); } else if (operand instanceof Double) { this.exitTypeDescriptor = "D"; return new TypedValue(0 - ((Number) operand).doubleValue()); } else if (operand instanceof Float) { this.exitTypeDescriptor = "F"; return new TypedValue(0 - ((Number) operand).floatValue()); } else if (operand instanceof BigInteger) { return new TypedValue(((BigInteger) operand).negate()); } else if (operand instanceof Long) { this.exitTypeDescriptor = "J"; return new TypedValue(0 - ((Number) operand).longValue()); } else if (operand instanceof Integer) { this.exitTypeDescriptor = "I"; return new TypedValue(0 - ((Number) operand).intValue()); } else if (operand instanceof Short) { return new TypedValue(0 - ((Number) operand).shortValue()); } else if (operand instanceof Byte) { return new TypedValue(0 - ((Number) operand).byteValue()); } else { // Unknown Number subtypes -> best guess is double subtraction return new TypedValue(0 - ((Number) operand).doubleValue()); } } return state.operate(Operation.SUBTRACT, operand, null); } Object left = leftOp.getValueInternal(state).getValue(); Object right = rightOp.getValueInternal(state).getValue(); if (left instanceof Number && right instanceof Number) { Number leftNumber = (Number) left; Number rightNumber = (Number) right; if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); return new TypedValue(leftBigDecimal.subtract(rightBigDecimal)); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { this.exitTypeDescriptor = "D"; return new TypedValue(leftNumber.doubleValue() - rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { this.exitTypeDescriptor = "F"; return new TypedValue(leftNumber.floatValue() - rightNumber.floatValue()); } else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) { BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class); BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class); return new TypedValue(leftBigInteger.subtract(rightBigInteger)); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { this.exitTypeDescriptor = "J"; return new TypedValue(leftNumber.longValue() - rightNumber.longValue()); } else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) { this.exitTypeDescriptor = "I"; return new TypedValue(leftNumber.intValue() - rightNumber.intValue()); } else { // Unknown Number subtypes -> best guess is double subtraction return new TypedValue(leftNumber.doubleValue() - rightNumber.doubleValue()); } } if (left instanceof String && right instanceof Integer && ((String) left).length() == 1) { String theString = (String) left; Integer theInteger = (Integer) right; // Implements character - int (ie. b - 1 = a) return new TypedValue(Character.toString((char) (theString.charAt(0) - theInteger))); } return state.operate(Operation.SUBTRACT, left, right); }
Example 6
Source File: OpMultiply.java From lams with GNU General Public License v2.0 | 4 votes |
/** * Implements the {@code multiply} operator directly here for certain types * of supported operands and otherwise delegates to any registered overloader * for types not supported here. * <p>Supported operand types: * <ul> * <li>numbers * <li>String and int ('abc' * 2 == 'abcabc') * </ul> */ @Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { Object leftOperand = getLeftOperand().getValueInternal(state).getValue(); Object rightOperand = getRightOperand().getValueInternal(state).getValue(); if (leftOperand instanceof Number && rightOperand instanceof Number) { Number leftNumber = (Number) leftOperand; Number rightNumber = (Number) rightOperand; if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); return new TypedValue(leftBigDecimal.multiply(rightBigDecimal)); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { this.exitTypeDescriptor = "D"; return new TypedValue(leftNumber.doubleValue() * rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { this.exitTypeDescriptor = "F"; return new TypedValue(leftNumber.floatValue() * rightNumber.floatValue()); } else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) { BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class); BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class); return new TypedValue(leftBigInteger.multiply(rightBigInteger)); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { this.exitTypeDescriptor = "J"; return new TypedValue(leftNumber.longValue() * rightNumber.longValue()); } else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) { this.exitTypeDescriptor = "I"; return new TypedValue(leftNumber.intValue() * rightNumber.intValue()); } else { // Unknown Number subtypes -> best guess is double multiplication return new TypedValue(leftNumber.doubleValue() * rightNumber.doubleValue()); } } if (leftOperand instanceof String && rightOperand instanceof Integer) { int repeats = (Integer) rightOperand; StringBuilder result = new StringBuilder(); for (int i = 0; i < repeats; i++) { result.append(leftOperand); } return new TypedValue(result.toString()); } return state.operate(Operation.MULTIPLY, leftOperand, rightOperand); }
Example 7
Source File: OpPlus.java From lams with GNU General Public License v2.0 | 4 votes |
@Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { SpelNodeImpl leftOp = getLeftOperand(); SpelNodeImpl rightOp = getRightOperand(); if (rightOp == null) { // if only one operand, then this is unary plus Object operandOne = leftOp.getValueInternal(state).getValue(); if (operandOne instanceof Number) { if (operandOne instanceof Double) { this.exitTypeDescriptor = "D"; } else if (operandOne instanceof Float) { this.exitTypeDescriptor = "F"; } else if (operandOne instanceof Long) { this.exitTypeDescriptor = "J"; } else if (operandOne instanceof Integer) { this.exitTypeDescriptor = "I"; } return new TypedValue(operandOne); } return state.operate(Operation.ADD, operandOne, null); } TypedValue operandOneValue = leftOp.getValueInternal(state); Object leftOperand = operandOneValue.getValue(); TypedValue operandTwoValue = rightOp.getValueInternal(state); Object rightOperand = operandTwoValue.getValue(); if (leftOperand instanceof Number && rightOperand instanceof Number) { Number leftNumber = (Number) leftOperand; Number rightNumber = (Number) rightOperand; if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); return new TypedValue(leftBigDecimal.add(rightBigDecimal)); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { this.exitTypeDescriptor = "D"; return new TypedValue(leftNumber.doubleValue() + rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { this.exitTypeDescriptor = "F"; return new TypedValue(leftNumber.floatValue() + rightNumber.floatValue()); } else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) { BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class); BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class); return new TypedValue(leftBigInteger.add(rightBigInteger)); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { this.exitTypeDescriptor = "J"; return new TypedValue(leftNumber.longValue() + rightNumber.longValue()); } else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) { this.exitTypeDescriptor = "I"; return new TypedValue(leftNumber.intValue() + rightNumber.intValue()); } else { // Unknown Number subtypes -> best guess is double addition return new TypedValue(leftNumber.doubleValue() + rightNumber.doubleValue()); } } if (leftOperand instanceof String && rightOperand instanceof String) { this.exitTypeDescriptor = "Ljava/lang/String"; return new TypedValue((String) leftOperand + rightOperand); } if (leftOperand instanceof String) { return new TypedValue( leftOperand + (rightOperand == null ? "null" : convertTypedValueToString(operandTwoValue, state))); } if (rightOperand instanceof String) { return new TypedValue( (leftOperand == null ? "null" : convertTypedValueToString(operandOneValue, state)) + rightOperand); } return state.operate(Operation.ADD, leftOperand, rightOperand); }
Example 8
Source File: OpModulus.java From lams with GNU General Public License v2.0 | 4 votes |
@Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { Object leftOperand = getLeftOperand().getValueInternal(state).getValue(); Object rightOperand = getRightOperand().getValueInternal(state).getValue(); if (leftOperand instanceof Number && rightOperand instanceof Number) { Number leftNumber = (Number) leftOperand; Number rightNumber = (Number) rightOperand; if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); return new TypedValue(leftBigDecimal.remainder(rightBigDecimal)); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { this.exitTypeDescriptor = "D"; return new TypedValue(leftNumber.doubleValue() % rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { this.exitTypeDescriptor = "F"; return new TypedValue(leftNumber.floatValue() % rightNumber.floatValue()); } else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) { BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class); BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class); return new TypedValue(leftBigInteger.remainder(rightBigInteger)); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { this.exitTypeDescriptor = "J"; return new TypedValue(leftNumber.longValue() % rightNumber.longValue()); } else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) { this.exitTypeDescriptor = "I"; return new TypedValue(leftNumber.intValue() % rightNumber.intValue()); } else { // Unknown Number subtypes -> best guess is double division return new TypedValue(leftNumber.doubleValue() % rightNumber.doubleValue()); } } return state.operate(Operation.MODULUS, leftOperand, rightOperand); }
Example 9
Source File: OpDivide.java From lams with GNU General Public License v2.0 | 4 votes |
@Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { Object leftOperand = getLeftOperand().getValueInternal(state).getValue(); Object rightOperand = getRightOperand().getValueInternal(state).getValue(); if (leftOperand instanceof Number && rightOperand instanceof Number) { Number leftNumber = (Number) leftOperand; Number rightNumber = (Number) rightOperand; if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); int scale = Math.max(leftBigDecimal.scale(), rightBigDecimal.scale()); return new TypedValue(leftBigDecimal.divide(rightBigDecimal, scale, RoundingMode.HALF_EVEN)); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { this.exitTypeDescriptor = "D"; return new TypedValue(leftNumber.doubleValue() / rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { this.exitTypeDescriptor = "F"; return new TypedValue(leftNumber.floatValue() / rightNumber.floatValue()); } else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) { BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class); BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class); return new TypedValue(leftBigInteger.divide(rightBigInteger)); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { this.exitTypeDescriptor = "J"; return new TypedValue(leftNumber.longValue() / rightNumber.longValue()); } else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) { this.exitTypeDescriptor = "I"; return new TypedValue(leftNumber.intValue() / rightNumber.intValue()); } else { // Unknown Number subtypes -> best guess is double division return new TypedValue(leftNumber.doubleValue() / rightNumber.doubleValue()); } } return state.operate(Operation.DIVIDE, leftOperand, rightOperand); }
Example 10
Source File: OpMinus.java From lams with GNU General Public License v2.0 | 4 votes |
@Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { SpelNodeImpl leftOp = getLeftOperand(); SpelNodeImpl rightOp = getRightOperand(); if (rightOp == null) { // if only one operand, then this is unary minus Object operand = leftOp.getValueInternal(state).getValue(); if (operand instanceof Number) { if (operand instanceof BigDecimal) { return new TypedValue(((BigDecimal) operand).negate()); } else if (operand instanceof Double) { this.exitTypeDescriptor = "D"; return new TypedValue(0 - ((Number) operand).doubleValue()); } else if (operand instanceof Float) { this.exitTypeDescriptor = "F"; return new TypedValue(0 - ((Number) operand).floatValue()); } else if (operand instanceof BigInteger) { return new TypedValue(((BigInteger) operand).negate()); } else if (operand instanceof Long) { this.exitTypeDescriptor = "J"; return new TypedValue(0 - ((Number) operand).longValue()); } else if (operand instanceof Integer) { this.exitTypeDescriptor = "I"; return new TypedValue(0 - ((Number) operand).intValue()); } else if (operand instanceof Short) { return new TypedValue(0 - ((Number) operand).shortValue()); } else if (operand instanceof Byte) { return new TypedValue(0 - ((Number) operand).byteValue()); } else { // Unknown Number subtypes -> best guess is double subtraction return new TypedValue(0 - ((Number) operand).doubleValue()); } } return state.operate(Operation.SUBTRACT, operand, null); } Object left = leftOp.getValueInternal(state).getValue(); Object right = rightOp.getValueInternal(state).getValue(); if (left instanceof Number && right instanceof Number) { Number leftNumber = (Number) left; Number rightNumber = (Number) right; if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); return new TypedValue(leftBigDecimal.subtract(rightBigDecimal)); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { this.exitTypeDescriptor = "D"; return new TypedValue(leftNumber.doubleValue() - rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { this.exitTypeDescriptor = "F"; return new TypedValue(leftNumber.floatValue() - rightNumber.floatValue()); } else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) { BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class); BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class); return new TypedValue(leftBigInteger.subtract(rightBigInteger)); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { this.exitTypeDescriptor = "J"; return new TypedValue(leftNumber.longValue() - rightNumber.longValue()); } else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) { this.exitTypeDescriptor = "I"; return new TypedValue(leftNumber.intValue() - rightNumber.intValue()); } else { // Unknown Number subtypes -> best guess is double subtraction return new TypedValue(leftNumber.doubleValue() - rightNumber.doubleValue()); } } if (left instanceof String && right instanceof Integer && ((String) left).length() == 1) { String theString = (String) left; Integer theInteger = (Integer) right; // Implements character - int (ie. b - 1 = a) return new TypedValue(Character.toString((char) (theString.charAt(0) - theInteger))); } return state.operate(Operation.SUBTRACT, left, right); }
Example 11
Source File: OpMultiply.java From java-technology-stack with MIT License | 4 votes |
/** * Implements the {@code multiply} operator directly here for certain types * of supported operands and otherwise delegates to any registered overloader * for types not supported here. * <p>Supported operand types: * <ul> * <li>numbers * <li>String and int ('abc' * 2 == 'abcabc') * </ul> */ @Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { Object leftOperand = getLeftOperand().getValueInternal(state).getValue(); Object rightOperand = getRightOperand().getValueInternal(state).getValue(); if (leftOperand instanceof Number && rightOperand instanceof Number) { Number leftNumber = (Number) leftOperand; Number rightNumber = (Number) rightOperand; if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); return new TypedValue(leftBigDecimal.multiply(rightBigDecimal)); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { this.exitTypeDescriptor = "D"; return new TypedValue(leftNumber.doubleValue() * rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { this.exitTypeDescriptor = "F"; return new TypedValue(leftNumber.floatValue() * rightNumber.floatValue()); } else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) { BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class); BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class); return new TypedValue(leftBigInteger.multiply(rightBigInteger)); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { this.exitTypeDescriptor = "J"; return new TypedValue(leftNumber.longValue() * rightNumber.longValue()); } else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) { this.exitTypeDescriptor = "I"; return new TypedValue(leftNumber.intValue() * rightNumber.intValue()); } else { // Unknown Number subtypes -> best guess is double multiplication return new TypedValue(leftNumber.doubleValue() * rightNumber.doubleValue()); } } if (leftOperand instanceof String && rightOperand instanceof Integer) { int repeats = (Integer) rightOperand; StringBuilder result = new StringBuilder(); for (int i = 0; i < repeats; i++) { result.append(leftOperand); } return new TypedValue(result.toString()); } return state.operate(Operation.MULTIPLY, leftOperand, rightOperand); }
Example 12
Source File: OpMinus.java From spring-analysis-note with MIT License | 4 votes |
@Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { SpelNodeImpl leftOp = getLeftOperand(); if (this.children.length < 2) { // if only one operand, then this is unary minus Object operand = leftOp.getValueInternal(state).getValue(); if (operand instanceof Number) { if (operand instanceof BigDecimal) { return new TypedValue(((BigDecimal) operand).negate()); } else if (operand instanceof Double) { this.exitTypeDescriptor = "D"; return new TypedValue(0 - ((Number) operand).doubleValue()); } else if (operand instanceof Float) { this.exitTypeDescriptor = "F"; return new TypedValue(0 - ((Number) operand).floatValue()); } else if (operand instanceof BigInteger) { return new TypedValue(((BigInteger) operand).negate()); } else if (operand instanceof Long) { this.exitTypeDescriptor = "J"; return new TypedValue(0 - ((Number) operand).longValue()); } else if (operand instanceof Integer) { this.exitTypeDescriptor = "I"; return new TypedValue(0 - ((Number) operand).intValue()); } else if (operand instanceof Short) { return new TypedValue(0 - ((Number) operand).shortValue()); } else if (operand instanceof Byte) { return new TypedValue(0 - ((Number) operand).byteValue()); } else { // Unknown Number subtypes -> best guess is double subtraction return new TypedValue(0 - ((Number) operand).doubleValue()); } } return state.operate(Operation.SUBTRACT, operand, null); } Object left = leftOp.getValueInternal(state).getValue(); Object right = getRightOperand().getValueInternal(state).getValue(); if (left instanceof Number && right instanceof Number) { Number leftNumber = (Number) left; Number rightNumber = (Number) right; if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); return new TypedValue(leftBigDecimal.subtract(rightBigDecimal)); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { this.exitTypeDescriptor = "D"; return new TypedValue(leftNumber.doubleValue() - rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { this.exitTypeDescriptor = "F"; return new TypedValue(leftNumber.floatValue() - rightNumber.floatValue()); } else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) { BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class); BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class); return new TypedValue(leftBigInteger.subtract(rightBigInteger)); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { this.exitTypeDescriptor = "J"; return new TypedValue(leftNumber.longValue() - rightNumber.longValue()); } else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) { this.exitTypeDescriptor = "I"; return new TypedValue(leftNumber.intValue() - rightNumber.intValue()); } else { // Unknown Number subtypes -> best guess is double subtraction return new TypedValue(leftNumber.doubleValue() - rightNumber.doubleValue()); } } if (left instanceof String && right instanceof Integer && ((String) left).length() == 1) { String theString = (String) left; Integer theInteger = (Integer) right; // Implements character - int (ie. b - 1 = a) return new TypedValue(Character.toString((char) (theString.charAt(0) - theInteger))); } return state.operate(Operation.SUBTRACT, left, right); }
Example 13
Source File: OpModulus.java From java-technology-stack with MIT License | 4 votes |
@Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { Object leftOperand = getLeftOperand().getValueInternal(state).getValue(); Object rightOperand = getRightOperand().getValueInternal(state).getValue(); if (leftOperand instanceof Number && rightOperand instanceof Number) { Number leftNumber = (Number) leftOperand; Number rightNumber = (Number) rightOperand; if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); return new TypedValue(leftBigDecimal.remainder(rightBigDecimal)); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { this.exitTypeDescriptor = "D"; return new TypedValue(leftNumber.doubleValue() % rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { this.exitTypeDescriptor = "F"; return new TypedValue(leftNumber.floatValue() % rightNumber.floatValue()); } else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) { BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class); BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class); return new TypedValue(leftBigInteger.remainder(rightBigInteger)); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { this.exitTypeDescriptor = "J"; return new TypedValue(leftNumber.longValue() % rightNumber.longValue()); } else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) { this.exitTypeDescriptor = "I"; return new TypedValue(leftNumber.intValue() % rightNumber.intValue()); } else { // Unknown Number subtypes -> best guess is double division return new TypedValue(leftNumber.doubleValue() % rightNumber.doubleValue()); } } return state.operate(Operation.MODULUS, leftOperand, rightOperand); }
Example 14
Source File: OpDivide.java From java-technology-stack with MIT License | 4 votes |
@Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { Object leftOperand = getLeftOperand().getValueInternal(state).getValue(); Object rightOperand = getRightOperand().getValueInternal(state).getValue(); if (leftOperand instanceof Number && rightOperand instanceof Number) { Number leftNumber = (Number) leftOperand; Number rightNumber = (Number) rightOperand; if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); int scale = Math.max(leftBigDecimal.scale(), rightBigDecimal.scale()); return new TypedValue(leftBigDecimal.divide(rightBigDecimal, scale, RoundingMode.HALF_EVEN)); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { this.exitTypeDescriptor = "D"; return new TypedValue(leftNumber.doubleValue() / rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { this.exitTypeDescriptor = "F"; return new TypedValue(leftNumber.floatValue() / rightNumber.floatValue()); } else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) { BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class); BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class); return new TypedValue(leftBigInteger.divide(rightBigInteger)); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { this.exitTypeDescriptor = "J"; return new TypedValue(leftNumber.longValue() / rightNumber.longValue()); } else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) { this.exitTypeDescriptor = "I"; return new TypedValue(leftNumber.intValue() / rightNumber.intValue()); } else { // Unknown Number subtypes -> best guess is double division return new TypedValue(leftNumber.doubleValue() / rightNumber.doubleValue()); } } return state.operate(Operation.DIVIDE, leftOperand, rightOperand); }
Example 15
Source File: OpMinus.java From java-technology-stack with MIT License | 4 votes |
@Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { SpelNodeImpl leftOp = getLeftOperand(); if (this.children.length < 2) { // if only one operand, then this is unary minus Object operand = leftOp.getValueInternal(state).getValue(); if (operand instanceof Number) { if (operand instanceof BigDecimal) { return new TypedValue(((BigDecimal) operand).negate()); } else if (operand instanceof Double) { this.exitTypeDescriptor = "D"; return new TypedValue(0 - ((Number) operand).doubleValue()); } else if (operand instanceof Float) { this.exitTypeDescriptor = "F"; return new TypedValue(0 - ((Number) operand).floatValue()); } else if (operand instanceof BigInteger) { return new TypedValue(((BigInteger) operand).negate()); } else if (operand instanceof Long) { this.exitTypeDescriptor = "J"; return new TypedValue(0 - ((Number) operand).longValue()); } else if (operand instanceof Integer) { this.exitTypeDescriptor = "I"; return new TypedValue(0 - ((Number) operand).intValue()); } else if (operand instanceof Short) { return new TypedValue(0 - ((Number) operand).shortValue()); } else if (operand instanceof Byte) { return new TypedValue(0 - ((Number) operand).byteValue()); } else { // Unknown Number subtypes -> best guess is double subtraction return new TypedValue(0 - ((Number) operand).doubleValue()); } } return state.operate(Operation.SUBTRACT, operand, null); } Object left = leftOp.getValueInternal(state).getValue(); Object right = getRightOperand().getValueInternal(state).getValue(); if (left instanceof Number && right instanceof Number) { Number leftNumber = (Number) left; Number rightNumber = (Number) right; if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); return new TypedValue(leftBigDecimal.subtract(rightBigDecimal)); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { this.exitTypeDescriptor = "D"; return new TypedValue(leftNumber.doubleValue() - rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { this.exitTypeDescriptor = "F"; return new TypedValue(leftNumber.floatValue() - rightNumber.floatValue()); } else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) { BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class); BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class); return new TypedValue(leftBigInteger.subtract(rightBigInteger)); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { this.exitTypeDescriptor = "J"; return new TypedValue(leftNumber.longValue() - rightNumber.longValue()); } else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) { this.exitTypeDescriptor = "I"; return new TypedValue(leftNumber.intValue() - rightNumber.intValue()); } else { // Unknown Number subtypes -> best guess is double subtraction return new TypedValue(leftNumber.doubleValue() - rightNumber.doubleValue()); } } if (left instanceof String && right instanceof Integer && ((String) left).length() == 1) { String theString = (String) left; Integer theInteger = (Integer) right; // Implements character - int (ie. b - 1 = a) return new TypedValue(Character.toString((char) (theString.charAt(0) - theInteger))); } return state.operate(Operation.SUBTRACT, left, right); }
Example 16
Source File: OpPlus.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { SpelNodeImpl leftOp = getLeftOperand(); SpelNodeImpl rightOp = getRightOperand(); if (rightOp == null) { // if only one operand, then this is unary plus Object operandOne = leftOp.getValueInternal(state).getValue(); if (operandOne instanceof Number) { if (operandOne instanceof Double) { this.exitTypeDescriptor = "D"; } else if (operandOne instanceof Float) { this.exitTypeDescriptor = "F"; } else if (operandOne instanceof Long) { this.exitTypeDescriptor = "J"; } else if (operandOne instanceof Integer) { this.exitTypeDescriptor = "I"; } return new TypedValue(operandOne); } return state.operate(Operation.ADD, operandOne, null); } TypedValue operandOneValue = leftOp.getValueInternal(state); Object leftOperand = operandOneValue.getValue(); TypedValue operandTwoValue = rightOp.getValueInternal(state); Object rightOperand = operandTwoValue.getValue(); if (leftOperand instanceof Number && rightOperand instanceof Number) { Number leftNumber = (Number) leftOperand; Number rightNumber = (Number) rightOperand; if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); return new TypedValue(leftBigDecimal.add(rightBigDecimal)); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { this.exitTypeDescriptor = "D"; return new TypedValue(leftNumber.doubleValue() + rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { this.exitTypeDescriptor = "F"; return new TypedValue(leftNumber.floatValue() + rightNumber.floatValue()); } else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) { BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class); BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class); return new TypedValue(leftBigInteger.add(rightBigInteger)); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { this.exitTypeDescriptor = "J"; return new TypedValue(leftNumber.longValue() + rightNumber.longValue()); } else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) { this.exitTypeDescriptor = "I"; return new TypedValue(leftNumber.intValue() + rightNumber.intValue()); } else { // Unknown Number subtypes -> best guess is double addition return new TypedValue(leftNumber.doubleValue() + rightNumber.doubleValue()); } } if (leftOperand instanceof String && rightOperand instanceof String) { this.exitTypeDescriptor = "Ljava/lang/String"; return new TypedValue((String) leftOperand + rightOperand); } if (leftOperand instanceof String) { return new TypedValue( leftOperand + (rightOperand == null ? "null" : convertTypedValueToString(operandTwoValue, state))); } if (rightOperand instanceof String) { return new TypedValue( (leftOperand == null ? "null" : convertTypedValueToString(operandOneValue, state)) + rightOperand); } return state.operate(Operation.ADD, leftOperand, rightOperand); }
Example 17
Source File: OpPlus.java From spring-analysis-note with MIT License | 4 votes |
@Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { SpelNodeImpl leftOp = getLeftOperand(); if (this.children.length < 2) { // if only one operand, then this is unary plus Object operandOne = leftOp.getValueInternal(state).getValue(); if (operandOne instanceof Number) { if (operandOne instanceof Double) { this.exitTypeDescriptor = "D"; } else if (operandOne instanceof Float) { this.exitTypeDescriptor = "F"; } else if (operandOne instanceof Long) { this.exitTypeDescriptor = "J"; } else if (operandOne instanceof Integer) { this.exitTypeDescriptor = "I"; } return new TypedValue(operandOne); } return state.operate(Operation.ADD, operandOne, null); } TypedValue operandOneValue = leftOp.getValueInternal(state); Object leftOperand = operandOneValue.getValue(); TypedValue operandTwoValue = getRightOperand().getValueInternal(state); Object rightOperand = operandTwoValue.getValue(); if (leftOperand instanceof Number && rightOperand instanceof Number) { Number leftNumber = (Number) leftOperand; Number rightNumber = (Number) rightOperand; if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); return new TypedValue(leftBigDecimal.add(rightBigDecimal)); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { this.exitTypeDescriptor = "D"; return new TypedValue(leftNumber.doubleValue() + rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { this.exitTypeDescriptor = "F"; return new TypedValue(leftNumber.floatValue() + rightNumber.floatValue()); } else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) { BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class); BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class); return new TypedValue(leftBigInteger.add(rightBigInteger)); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { this.exitTypeDescriptor = "J"; return new TypedValue(leftNumber.longValue() + rightNumber.longValue()); } else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) { this.exitTypeDescriptor = "I"; return new TypedValue(leftNumber.intValue() + rightNumber.intValue()); } else { // Unknown Number subtypes -> best guess is double addition return new TypedValue(leftNumber.doubleValue() + rightNumber.doubleValue()); } } if (leftOperand instanceof String && rightOperand instanceof String) { this.exitTypeDescriptor = "Ljava/lang/String"; return new TypedValue((String) leftOperand + rightOperand); } if (leftOperand instanceof String) { return new TypedValue( leftOperand + (rightOperand == null ? "null" : convertTypedValueToString(operandTwoValue, state))); } if (rightOperand instanceof String) { return new TypedValue( (leftOperand == null ? "null" : convertTypedValueToString(operandOneValue, state)) + rightOperand); } return state.operate(Operation.ADD, leftOperand, rightOperand); }
Example 18
Source File: OpModulus.java From spring-analysis-note with MIT License | 4 votes |
@Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { Object leftOperand = getLeftOperand().getValueInternal(state).getValue(); Object rightOperand = getRightOperand().getValueInternal(state).getValue(); if (leftOperand instanceof Number && rightOperand instanceof Number) { Number leftNumber = (Number) leftOperand; Number rightNumber = (Number) rightOperand; if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); return new TypedValue(leftBigDecimal.remainder(rightBigDecimal)); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { this.exitTypeDescriptor = "D"; return new TypedValue(leftNumber.doubleValue() % rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { this.exitTypeDescriptor = "F"; return new TypedValue(leftNumber.floatValue() % rightNumber.floatValue()); } else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) { BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class); BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class); return new TypedValue(leftBigInteger.remainder(rightBigInteger)); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { this.exitTypeDescriptor = "J"; return new TypedValue(leftNumber.longValue() % rightNumber.longValue()); } else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) { this.exitTypeDescriptor = "I"; return new TypedValue(leftNumber.intValue() % rightNumber.intValue()); } else { // Unknown Number subtypes -> best guess is double division return new TypedValue(leftNumber.doubleValue() % rightNumber.doubleValue()); } } return state.operate(Operation.MODULUS, leftOperand, rightOperand); }
Example 19
Source File: OpModulus.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { Object leftOperand = getLeftOperand().getValueInternal(state).getValue(); Object rightOperand = getRightOperand().getValueInternal(state).getValue(); if (leftOperand instanceof Number && rightOperand instanceof Number) { Number leftNumber = (Number) leftOperand; Number rightNumber = (Number) rightOperand; if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); return new TypedValue(leftBigDecimal.remainder(rightBigDecimal)); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { this.exitTypeDescriptor = "D"; return new TypedValue(leftNumber.doubleValue() % rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { this.exitTypeDescriptor = "F"; return new TypedValue(leftNumber.floatValue() % rightNumber.floatValue()); } else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) { BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class); BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class); return new TypedValue(leftBigInteger.remainder(rightBigInteger)); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { this.exitTypeDescriptor = "J"; return new TypedValue(leftNumber.longValue() % rightNumber.longValue()); } else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) { this.exitTypeDescriptor = "I"; return new TypedValue(leftNumber.intValue() % rightNumber.intValue()); } else { // Unknown Number subtypes -> best guess is double division return new TypedValue(leftNumber.doubleValue() % rightNumber.doubleValue()); } } return state.operate(Operation.MODULUS, leftOperand, rightOperand); }
Example 20
Source File: OpMultiply.java From spring4-understanding with Apache License 2.0 | 4 votes |
/** * Implements the {@code multiply} operator directly here for certain types * of supported operands and otherwise delegates to any registered overloader * for types not supported here. * <p>Supported operand types: * <ul> * <li>numbers * <li>String and int ('abc' * 2 == 'abcabc') * </ul> */ @Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { Object leftOperand = getLeftOperand().getValueInternal(state).getValue(); Object rightOperand = getRightOperand().getValueInternal(state).getValue(); if (leftOperand instanceof Number && rightOperand instanceof Number) { Number leftNumber = (Number) leftOperand; Number rightNumber = (Number) rightOperand; if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); return new TypedValue(leftBigDecimal.multiply(rightBigDecimal)); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { this.exitTypeDescriptor = "D"; return new TypedValue(leftNumber.doubleValue() * rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { this.exitTypeDescriptor = "F"; return new TypedValue(leftNumber.floatValue() * rightNumber.floatValue()); } else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) { BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class); BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class); return new TypedValue(leftBigInteger.multiply(rightBigInteger)); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { this.exitTypeDescriptor = "J"; return new TypedValue(leftNumber.longValue() * rightNumber.longValue()); } else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) { this.exitTypeDescriptor = "I"; return new TypedValue(leftNumber.intValue() * rightNumber.intValue()); } else { // Unknown Number subtypes -> best guess is double multiplication return new TypedValue(leftNumber.doubleValue() * rightNumber.doubleValue()); } } if (leftOperand instanceof String && rightOperand instanceof Integer) { int repeats = (Integer) rightOperand; StringBuilder result = new StringBuilder(); for (int i = 0; i < repeats; i++) { result.append(leftOperand); } return new TypedValue(result.toString()); } return state.operate(Operation.MULTIPLY, leftOperand, rightOperand); }