1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.expression.spel.ast;
18
19 import java.math.BigDecimal;
20 import java.math.BigInteger;
21
22 import org.springframework.expression.EvaluationException;
23 import org.springframework.expression.Operation;
24 import org.springframework.expression.TypedValue;
25 import org.springframework.expression.spel.ExpressionState;
26 import org.springframework.expression.spel.SpelEvaluationException;
27 import org.springframework.expression.spel.SpelMessage;
28 import org.springframework.util.Assert;
29
30
31
32
33
34
35
36
37
38
39 public class OpDec extends Operator {
40
41 private final boolean postfix;
42
43
44 public OpDec(int pos, boolean postfix, SpelNodeImpl... operands) {
45 super("--", pos, operands);
46 Assert.notEmpty(operands);
47 this.postfix = postfix;
48 }
49
50
51 @Override
52 public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
53 SpelNodeImpl operand = getLeftOperand();
54
55
56 ValueRef lvalue = operand.getValueRef(state);
57
58 TypedValue operandTypedValue = lvalue.getValue();
59 Object operandValue = operandTypedValue.getValue();
60 TypedValue returnValue = operandTypedValue;
61 TypedValue newValue = null;
62
63 if (operandValue instanceof Number) {
64 Number op1 = (Number) operandValue;
65 if (op1 instanceof BigDecimal) {
66 newValue = new TypedValue(((BigDecimal) op1).subtract(BigDecimal.ONE), operandTypedValue.getTypeDescriptor());
67 }
68 else if (op1 instanceof Double) {
69 newValue = new TypedValue(op1.doubleValue() - 1.0d, operandTypedValue.getTypeDescriptor());
70 }
71 else if (op1 instanceof Float) {
72 newValue = new TypedValue(op1.floatValue() - 1.0f, operandTypedValue.getTypeDescriptor());
73 }
74 else if (op1 instanceof BigInteger) {
75 newValue = new TypedValue(((BigInteger) op1).subtract(BigInteger.ONE), operandTypedValue.getTypeDescriptor());
76 }
77 else if (op1 instanceof Long) {
78 newValue = new TypedValue(op1.longValue() - 1L, operandTypedValue.getTypeDescriptor());
79 }
80 else if (op1 instanceof Integer) {
81 newValue = new TypedValue(op1.intValue() - 1, operandTypedValue.getTypeDescriptor());
82 }
83 else if (op1 instanceof Short) {
84 newValue = new TypedValue(op1.shortValue() - (short) 1, operandTypedValue.getTypeDescriptor());
85 }
86 else if (op1 instanceof Byte) {
87 newValue = new TypedValue(op1.byteValue() - (byte) 1, operandTypedValue.getTypeDescriptor());
88 }
89 else {
90
91 newValue = new TypedValue(op1.doubleValue() - 1.0d, operandTypedValue.getTypeDescriptor());
92 }
93 }
94
95 if (newValue == null) {
96 try {
97 newValue = state.operate(Operation.SUBTRACT, returnValue.getValue(), 1);
98 }
99 catch (SpelEvaluationException ex) {
100 if (ex.getMessageCode() == SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES) {
101
102 throw new SpelEvaluationException(operand.getStartPosition(),
103 SpelMessage.OPERAND_NOT_DECREMENTABLE, operand.toStringAST());
104 }
105 else {
106 throw ex;
107 }
108 }
109 }
110
111
112 try {
113 lvalue.setValue(newValue.getValue());
114 }
115 catch (SpelEvaluationException see) {
116
117 if (see.getMessageCode() == SpelMessage.SETVALUE_NOT_SUPPORTED) {
118 throw new SpelEvaluationException(operand.getStartPosition(),
119 SpelMessage.OPERAND_NOT_DECREMENTABLE);
120 }
121 else {
122 throw see;
123 }
124 }
125
126 if (!this.postfix) {
127
128 returnValue = newValue;
129 }
130
131 return returnValue;
132 }
133
134 @Override
135 public String toStringAST() {
136 return getLeftOperand().toStringAST() + "--";
137 }
138
139 @Override
140 public SpelNodeImpl getRightOperand() {
141 return null;
142 }
143
144 }