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 OpInc extends Operator {
40
41 private final boolean postfix;
42
43
44 public OpInc(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 ValueRef valueRef = operand.getValueRef(state);
55
56 TypedValue typedValue = valueRef.getValue();
57 Object value = typedValue.getValue();
58 TypedValue returnValue = typedValue;
59 TypedValue newValue = null;
60
61 if (value instanceof Number) {
62 Number op1 = (Number) value;
63 if (op1 instanceof BigDecimal) {
64 newValue = new TypedValue(((BigDecimal) op1).add(BigDecimal.ONE), typedValue.getTypeDescriptor());
65 }
66 else if (op1 instanceof Double) {
67 newValue = new TypedValue(op1.doubleValue() + 1.0d, typedValue.getTypeDescriptor());
68 }
69 else if (op1 instanceof Float) {
70 newValue = new TypedValue(op1.floatValue() + 1.0f, typedValue.getTypeDescriptor());
71 }
72 else if (op1 instanceof BigInteger) {
73 newValue = new TypedValue(((BigInteger) op1).add(BigInteger.ONE), typedValue.getTypeDescriptor());
74 }
75 else if (op1 instanceof Long) {
76 newValue = new TypedValue(op1.longValue() + 1L, typedValue.getTypeDescriptor());
77 }
78 else if (op1 instanceof Integer) {
79 newValue = new TypedValue(op1.intValue() + 1, typedValue.getTypeDescriptor());
80 }
81 else if (op1 instanceof Short) {
82 newValue = new TypedValue(op1.shortValue() + (short) 1, typedValue.getTypeDescriptor());
83 }
84 else if (op1 instanceof Byte) {
85 newValue = new TypedValue(op1.byteValue() + (byte) 1, typedValue.getTypeDescriptor());
86 }
87 else {
88
89 newValue = new TypedValue(op1.doubleValue() + 1.0d, typedValue.getTypeDescriptor());
90 }
91 }
92
93 if (newValue == null) {
94 try {
95 newValue = state.operate(Operation.ADD, returnValue.getValue(), 1);
96 }
97 catch (SpelEvaluationException ex) {
98 if (ex.getMessageCode() == SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES) {
99
100 throw new SpelEvaluationException(operand.getStartPosition(),
101 SpelMessage.OPERAND_NOT_INCREMENTABLE, operand.toStringAST());
102 }
103 throw ex;
104 }
105 }
106
107
108 try {
109 valueRef.setValue(newValue.getValue());
110 }
111 catch (SpelEvaluationException see) {
112
113 if (see.getMessageCode() == SpelMessage.SETVALUE_NOT_SUPPORTED) {
114 throw new SpelEvaluationException(operand.getStartPosition(), SpelMessage.OPERAND_NOT_INCREMENTABLE);
115 }
116 else {
117 throw see;
118 }
119 }
120
121 if (!this.postfix) {
122
123 returnValue = newValue;
124 }
125
126 return returnValue;
127 }
128
129 @Override
130 public String toStringAST() {
131 return getLeftOperand().toStringAST() + "++";
132 }
133
134 @Override
135 public SpelNodeImpl getRightOperand() {
136 return null;
137 }
138
139 }