001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
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 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
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 */
016package io.jboot.components.valid.interceptor;
017
018import com.jfinal.aop.Interceptor;
019import com.jfinal.aop.Invocation;
020import com.jfinal.kit.Ret;
021import io.jboot.components.valid.ValidUtil;
022import io.jboot.utils.ClassUtil;
023
024import javax.validation.constraints.DecimalMax;
025import java.lang.reflect.Parameter;
026import java.math.BigDecimal;
027import java.math.BigInteger;
028
029public class DecimalMaxInterceptor implements Interceptor {
030
031    @Override
032    public void intercept(Invocation inv) {
033        Parameter[] parameters = inv.getMethod().getParameters();
034
035        for (int index = 0; index < parameters.length; index++) {
036            DecimalMax decimalMax = parameters[index].getAnnotation(DecimalMax.class);
037            if (decimalMax == null) {
038                continue;
039            }
040            
041            Object validObject = inv.getArg(index);
042            if (validObject != null && !matches(decimalMax, validObject)) {
043                String reason = parameters[index].getName() + " max value is " + decimalMax.value()
044                        + ", but current value is " + validObject + " at method: " + ClassUtil.buildMethodString(inv.getMethod());
045                Ret paras = Ret.by("value", decimalMax.value());
046                ValidUtil.throwValidException(parameters[index].getName(), decimalMax.message(), paras, reason);
047            }
048        }
049
050        inv.invoke();
051    }
052
053    private boolean matches(DecimalMax decimalMax, Object validObject) {
054        if (validObject instanceof BigInteger) {
055            return ((BigInteger) validObject).compareTo(new BigInteger(decimalMax.value())) <= 0;
056        } else if (validObject instanceof BigDecimal) {
057            return ((BigDecimal) validObject).compareTo(new BigDecimal(decimalMax.value())) <= 0;
058        } else if (validObject instanceof CharSequence) {
059            return (new BigDecimal(validObject.toString())).compareTo(new BigDecimal(decimalMax.value())) <= 0;
060        } else if (validObject instanceof Float) {
061            return ((Float) validObject) <= Float.parseFloat(decimalMax.value());
062        } else if (validObject instanceof Double) {
063            return ((Double) validObject) <= Double.parseDouble(decimalMax.value());
064        } else if (validObject instanceof Number) {
065            return ((Number) validObject).longValue() <= Long.parseLong(decimalMax.value());
066        }
067        return false;
068    }
069
070
071}