001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.expression.spel.ast;
018
019import java.math.BigDecimal;
020import java.math.BigInteger;
021
022import org.springframework.asm.MethodVisitor;
023import org.springframework.expression.EvaluationException;
024import org.springframework.expression.Operation;
025import org.springframework.expression.TypedValue;
026import org.springframework.expression.spel.CodeFlow;
027import org.springframework.expression.spel.ExpressionState;
028import org.springframework.util.Assert;
029import org.springframework.util.NumberUtils;
030
031/**
032 * Implements the modulus operator.
033 *
034 * @author Andy Clement
035 * @author Juergen Hoeller
036 * @author Giovanni Dall'Oglio Risso
037 * @since 3.0
038 */
039public class OpModulus extends Operator {
040
041        public OpModulus(int startPos, int endPos, SpelNodeImpl... operands) {
042                super("%", startPos, endPos, operands);
043        }
044
045
046        @Override
047        public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
048                Object leftOperand = getLeftOperand().getValueInternal(state).getValue();
049                Object rightOperand = getRightOperand().getValueInternal(state).getValue();
050
051                if (leftOperand instanceof Number && rightOperand instanceof Number) {
052                        Number leftNumber = (Number) leftOperand;
053                        Number rightNumber = (Number) rightOperand;
054
055                        if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
056                                BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
057                                BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
058                                return new TypedValue(leftBigDecimal.remainder(rightBigDecimal));
059                        }
060                        else if (leftNumber instanceof Double || rightNumber instanceof Double) {
061                                this.exitTypeDescriptor = "D";
062                                return new TypedValue(leftNumber.doubleValue() % rightNumber.doubleValue());
063                        }
064                        else if (leftNumber instanceof Float || rightNumber instanceof Float) {
065                                this.exitTypeDescriptor = "F";
066                                return new TypedValue(leftNumber.floatValue() % rightNumber.floatValue());
067                        }
068                        else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) {
069                                BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
070                                BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
071                                return new TypedValue(leftBigInteger.remainder(rightBigInteger));
072                        }
073                        else if (leftNumber instanceof Long || rightNumber instanceof Long) {
074                                this.exitTypeDescriptor = "J";
075                                return new TypedValue(leftNumber.longValue() % rightNumber.longValue());
076                        }
077                        else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) {
078                                this.exitTypeDescriptor = "I";
079                                return new TypedValue(leftNumber.intValue() % rightNumber.intValue());
080                        }
081                        else {
082                                // Unknown Number subtypes -> best guess is double division
083                                return new TypedValue(leftNumber.doubleValue() % rightNumber.doubleValue());
084                        }
085                }
086
087                return state.operate(Operation.MODULUS, leftOperand, rightOperand);
088        }
089
090        @Override
091        public boolean isCompilable() {
092                if (!getLeftOperand().isCompilable()) {
093                        return false;
094                }
095                if (this.children.length > 1) {
096                        if (!getRightOperand().isCompilable()) {
097                                return false;
098                        }
099                }
100                return (this.exitTypeDescriptor != null);
101        }
102
103        @Override
104        public void generateCode(MethodVisitor mv, CodeFlow cf) {
105                getLeftOperand().generateCode(mv, cf);
106                String leftDesc = getLeftOperand().exitTypeDescriptor;
107                String exitDesc = this.exitTypeDescriptor;
108                Assert.state(exitDesc != null, "No exit type descriptor");
109                char targetDesc = exitDesc.charAt(0);
110                CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, leftDesc, targetDesc);
111                if (this.children.length > 1) {
112                        cf.enterCompilationScope();
113                        getRightOperand().generateCode(mv, cf);
114                        String rightDesc = getRightOperand().exitTypeDescriptor;
115                        cf.exitCompilationScope();
116                        CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, rightDesc, targetDesc);
117                        switch (targetDesc) {
118                                case 'I':
119                                        mv.visitInsn(IREM);
120                                        break;
121                                case 'J':
122                                        mv.visitInsn(LREM);
123                                        break;
124                                case 'F':
125                                        mv.visitInsn(FREM);
126                                        break;
127                                case 'D':
128                                        mv.visitInsn(DREM);
129                                        break;
130                                default:
131                                        throw new IllegalStateException(
132                                                        "Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
133                        }
134                }
135                cf.pushDescriptor(this.exitTypeDescriptor);
136        }
137
138}