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.core.Controller;
019import io.jboot.web.controller.JbootController;
020
021import java.lang.annotation.Annotation;
022import java.lang.reflect.Method;
023
024/**
025 * @author michael yang (fuhai999@gmail.com)
026 * <p>
027 * InterceptorBuilder 用于控制某个方法已经添加好的拦截器,可以对其删除或者添加
028 *
029 * <pre>
030 * 配置方法:
031 * public void onInit() {
032 *     InterceptorBuilderManager.me().addInterceptorBuilder(new MyInterceptorBuilder());
033 * }
034 *
035 * 或者给 MyInterceptorBuilder 类添加 @AutoLoad 注解
036 * </pre>
037 */
038public interface InterceptorBuilder {
039
040    void build(Class<?> targetClass, Method method, Interceptors interceptors);
041
042    class Util {
043
044        public static boolean isChildClassOf(Class<?> childClass, Class<?> parentClass) {
045            return parentClass.isAssignableFrom(childClass);
046        }
047
048        public static boolean isController(Class<?> serviceClass) {
049            return Controller.class.isAssignableFrom(serviceClass);
050        }
051
052        public static boolean isJbootController(Class<?> serviceClass) {
053            return JbootController.class.isAssignableFrom(serviceClass);
054        }
055
056        public static <A extends Annotation> boolean hasAnnotation(Class<?> targetClass, Class<A> annotationClass) {
057            return targetClass.getAnnotation(annotationClass) != null;
058        }
059
060
061        public static <A extends Annotation> boolean hasAnnotation(Method method, Class<A> annotationClass) {
062            return method.getAnnotation(annotationClass) != null;
063        }
064
065
066        public static boolean hasAnyAnnotation(Class<?> targetClass, Class<? extends Annotation>... annotationClass) {
067            for (Class<? extends Annotation> clazz : annotationClass) {
068                if (hasAnnotation(targetClass, clazz)) {
069                    return true;
070                }
071            }
072            return false;
073        }
074
075
076        public static boolean hasAnyAnnotation(Method method, Class<? extends Annotation>... annotationClass) {
077            for (Class<? extends Annotation> clazz : annotationClass) {
078                if (hasAnnotation(method, clazz)) {
079                    return true;
080                }
081            }
082            return false;
083        }
084
085
086        public static Annotation getAnyAnnotation(Class<?> targetClass, Class<? extends Annotation>... annotationClass) {
087            for (Class<? extends Annotation> clazz : annotationClass) {
088                Annotation a = targetClass.getAnnotation(clazz);
089                if (a != null) {
090                    return a;
091                }
092            }
093            return null;
094        }
095
096
097        public static Annotation getAnyAnnotation(Method method, Class<? extends Annotation>... annotationClass) {
098            for (Class<? extends Annotation> clazz : annotationClass) {
099                Annotation a = method.getAnnotation(clazz);
100                if (a != null) {
101                    return a;
102                }
103            }
104            return null;
105        }
106
107
108        public static <A extends Annotation> boolean hasAnnotation(Class<?> targetClass, Method method, Class<A> annotationClass) {
109            return hasAnnotation(targetClass, annotationClass) || hasAnnotation(method, annotationClass);
110        }
111    }
112}