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 io.jboot.aop.InterceptorBuilder;
019import io.jboot.aop.Interceptors;
020import io.jboot.aop.annotation.AutoLoad;
021import io.jboot.core.weight.Weight;
022
023import javax.validation.Valid;
024import javax.validation.constraints.*;
025import java.lang.reflect.Method;
026import java.lang.reflect.Parameter;
027
028@AutoLoad
029@Weight(100)
030public class ValidInterceptorBuilder implements InterceptorBuilder {
031
032    @Override
033    public void build(Class<?> targetClass, Method method, Interceptors interceptors) {
034        Parameter[] parameters = method.getParameters();
035        if (parameters != null && parameters.length > 0) {
036            for (Parameter p : parameters) {
037
038                if (p.getAnnotation(DecimalMax.class) != null) {
039                    interceptors.addIfNotExist(DecimalMaxInterceptor.class);
040                }
041
042                if (p.getAnnotation(DecimalMin.class) != null) {
043                    interceptors.addIfNotExist(DecimalMinInterceptor.class);
044                }
045
046                if (p.getAnnotation(Digits.class) != null) {
047                    interceptors.addIfNotExist(DigitsInterceptor.class);
048                }
049
050                if (p.getAnnotation(Email.class) != null) {
051                    interceptors.addIfNotExist(EmailInterceptor.class);
052                }
053
054                if (p.getAnnotation(Max.class) != null) {
055                    interceptors.addIfNotExist(MaxInterceptor.class);
056                }
057
058                if (p.getAnnotation(Min.class) != null) {
059                    interceptors.addIfNotExist(MinInterceptor.class);
060                }
061
062                if (p.getAnnotation(Negative.class) != null) {
063                    interceptors.addIfNotExist(NegativeInterceptor.class);
064                }
065
066                if (p.getAnnotation(NegativeOrZero.class) != null) {
067                    interceptors.addIfNotExist(NegativeOrZeroInterceptor.class);
068                }
069
070                if (p.getAnnotation(NotBlank.class) != null) {
071                    interceptors.addIfNotExist(NotBlankInterceptor.class);
072                }
073
074                if (p.getAnnotation(NotEmpty.class) != null) {
075                    interceptors.addIfNotExist(NotEmptyInterceptor.class);
076                }
077
078                if (p.getAnnotation(NotNull.class) != null) {
079                    interceptors.addIfNotExist(NotNullInterceptor.class);
080                }
081
082                if (p.getAnnotation(Pattern.class) != null) {
083                    interceptors.addIfNotExist(PatternInterceptor.class);
084                }
085
086                if (p.getAnnotation(Positive.class) != null) {
087                    interceptors.addIfNotExist(PositiveInterceptor.class);
088                }
089
090                if (p.getAnnotation(PositiveOrZero.class) != null) {
091                    interceptors.addIfNotExist(PositiveOrZeroInterceptor.class);
092                }
093
094                if (p.getAnnotation(Size.class) != null) {
095                    interceptors.addIfNotExist(SizeInterceptor.class);
096                }
097
098                if (p.getAnnotation(Valid.class) != null) {
099                    interceptors.addIfNotExist(ValidInterceptor.class);
100                }
101            }
102        }
103    }
104}