/**
 * Copyright (c) 2015-2018, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jboot.aop.interceptor;


import com.jfinal.aop.Interceptor;
import com.jfinal.aop.InterceptorManager;
import com.jfinal.core.Controller;
import io.jboot.utils.ClassKits;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import java.lang.reflect.Method;

/**
 * 兼容 JFinal Service 的拦截器
 */
public class JFinalBeforeInterceptor implements MethodInterceptor {

    InterceptorManager manger = InterceptorManager.me();

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {

        Class targetClass = methodInvocation.getThis().getClass();

        //过滤掉controller，因为controller由action去执行@Before相关注解了
        if (Controller.class.isAssignableFrom(targetClass)) {
            return methodInvocation.proceed();
        }

        targetClass = ClassKits.getUsefulClass(targetClass);
        Method method = methodInvocation.getMethod();

        Interceptor[] finalInters = manger.buildServiceMethodInterceptor(InterceptorManager.NULL_INTERS, targetClass, method);
        JFinalBeforeInvocation invocation = new JFinalBeforeInvocation(methodInvocation, finalInters, methodInvocation.getArguments());
        invocation.invoke();
        return invocation.getReturnValue();
    }

}

/**
 * Copyright (c) 2015-2018, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jboot.aop.interceptor;


import com.jfinal.aop.Interceptor;
import com.jfinal.aop.InterceptorManager;
import com.jfinal.core.Controller;
import io.jboot.utils.ClassKits;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import java.lang.reflect.Method;

/**
 * 兼容 JFinal Service 的拦截器
 */
public class JFinalBeforeInterceptor implements MethodInterceptor {


    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {

        Class targetClass = methodInvocation.getThis().getClass();

        //过滤掉controller，因为controller由action去执行@Before相关注解了
        if (Controller.class.isAssignableFrom(targetClass)) {
            return methodInvocation.proceed();
        }

        targetClass = ClassKits.getUsefulClass(targetClass);
        Method method = methodInvocation.getMethod();

        //service层的所有拦截器，包含了全局的拦截器 和 @Before 的拦截器
        Interceptor[] serviceInterceptors = InterceptorManager.me().buildServiceMethodInterceptor(InterceptorManager.NULL_INTERS, targetClass, method);
        JFinalBeforeInvocation invocation = new JFinalBeforeInvocation(methodInvocation, serviceInterceptors, methodInvocation.getArguments());
        invocation.invoke();
        return invocation.getReturnValue();
    }

}

optimize JFinalBeforeInterceptor