View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Decrement operator.  Can be used in a prefix or postfix form. This will throw
32   * appropriate exceptions if the operand in question does not support decrement.
33   *
34   * @author Andy Clement
35   * @author Juergen Hoeller
36   * @author Giovanni Dall'Oglio Risso
37   * @since 3.2
38   */
39  public class OpDec extends Operator {
40  
41  	private final boolean postfix;  // false means prefix
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  		// The operand is going to be read and then assigned to, we don't want to evaluate it twice.
56  		ValueRef lvalue = operand.getValueRef(state);
57  
58  		TypedValue operandTypedValue = lvalue.getValue();  //operand.getValueInternal(state);
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  				// Unknown Number subtype -> best guess is double decrement
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 					// This means the operand is not decrementable
102 					throw new SpelEvaluationException(operand.getStartPosition(),
103 							SpelMessage.OPERAND_NOT_DECREMENTABLE, operand.toStringAST());
104 				}
105 				else {
106 					throw ex;
107 				}
108 			}
109 		}
110 
111 		// set the new value
112 		try {
113 			lvalue.setValue(newValue.getValue());
114 		}
115 		catch (SpelEvaluationException see) {
116 			// if unable to set the value the operand is not writable (e.g. 1-- )
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 			// the return value is the new value, not the original value
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 }