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.aop;
017
018import com.jfinal.aop.Aop;
019import com.jfinal.aop.Interceptor;
020import com.jfinal.aop.Invocation;
021import io.jboot.aop.annotation.AutoLoad;
022import io.jboot.aop.annotation.FilterBy;
023
024import java.lang.reflect.Method;
025import java.lang.reflect.Parameter;
026
027@AutoLoad
028public class ValueFilterInterceptor implements Interceptor, InterceptorBuilder {
029
030    @Override
031    public void intercept(Invocation inv) {
032        Parameter[] parameters = inv.getMethod().getParameters();
033        for (int index = 0; index < parameters.length; index++) {
034            FilterBy filter = parameters[index].getAnnotation(FilterBy.class);
035            if (filter != null) {
036                Object original = inv.getArg(index);
037
038                Class<? extends ValueFilter>[] classes = filter.value();
039                for (Class<? extends ValueFilter> aClass : classes) {
040                    ValueFilter vf = Aop.get(aClass);
041                    original = vf.doFilter(original);
042                }
043
044                inv.setArg(index, original);
045            }
046        }
047
048        inv.invoke();
049    }
050
051
052    @Override
053    public void build(Class<?> targetClass, Method method, Interceptors interceptors) {
054        Parameter[] parameters = method.getParameters();
055        if (parameters != null && parameters.length > 0) {
056            for (Parameter p : parameters) {
057                if (p.getAnnotation(FilterBy.class) != null) {
058                    interceptors.addIfNotExist(this);
059                    break;
060                }
061            }
062        }
063    }
064}