package example.aop.proxy;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;

public class MyMethodInterceptor implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Query query = invocation.getMethod().getAnnotation(Query.class);
        if(query != null) {
            System.out.println("is native: " + query.nativeQuery());
            System.out.println("sql = " + query.value());
        }else{
            System.out.println("name = " + invocation.getMethod().getName());
        }
        return isBaseType(invocation.getMethod().getReturnType()) ? 0 : null;
    }

    /**
     * 判断object是否为基本类型
     * @return
     */
    public static boolean isBaseType(Class clazz) {
        String className = clazz.getTypeName();
        if (className.equals("int") ||
                className.equals("byte") ||
                className.equals("long") ||
                className.equals("double") ||
                className.equals("float") ||
                className.equals("char") ||
                className.equals("short") ||
                className.equals("boolean")) {
            return true;
        }
        return false;
    }
}
