Search in sources :

Example 1 with ForestInterceptorDefineException

use of com.dtflys.forest.exceptions.ForestInterceptorDefineException in project forest by dromara.

the class TestExceptions method testInterceptorDefineException.

@Test
public void testInterceptorDefineException() {
    ForestInterceptorDefineException exception = new ForestInterceptorDefineException(TestErrorInterceptor.class);
    assertThat(exception.getMessage()).isEqualTo("[Forest] Interceptor class 'com.dtflys.test.exception.TestExceptions$TestErrorInterceptor' cannot be initialized, because interceptor class must implements com.dtflys.forest.interceptor.Interceptor");
    assertThat(exception.getInterceptorClass()).isEqualTo(TestErrorInterceptor.class);
}
Also used : ForestInterceptorDefineException(com.dtflys.forest.exceptions.ForestInterceptorDefineException) Test(org.junit.Test)

Example 2 with ForestInterceptorDefineException

use of com.dtflys.forest.exceptions.ForestInterceptorDefineException in project forest by dromara.

the class ForestMethod method processParameterAnnotation.

/**
 * 处理参数的注解
 * @param parameter 方法参数-字符串模板解析对象,{@link MappingParameter}类实例
 * @param anns 方法参数注解的二维数组
 */
private void processParameterAnnotation(MappingParameter parameter, Annotation[] anns) {
    for (int i = 0; i < anns.length; i++) {
        Annotation ann = anns[i];
        Class annType = ann.annotationType();
        if (annType.getPackage().getName().startsWith("java.")) {
            continue;
        }
        Annotation[] subAnnArray = annType.getAnnotations();
        if (subAnnArray.length > 0) {
            processParameterAnnotation(parameter, subAnnArray);
        }
        ParamLifeCycle paramLifeCycleAnn = (ParamLifeCycle) annType.getAnnotation(ParamLifeCycle.class);
        if (paramLifeCycleAnn != null) {
            Class<? extends ParameterAnnotationLifeCycle> interceptorClass = paramLifeCycleAnn.value();
            if (!Interceptor.class.isAssignableFrom(interceptorClass)) {
                throw new ForestInterceptorDefineException(interceptorClass);
            }
            ParameterAnnotationLifeCycle lifeCycle = addInterceptor(interceptorClass);
            lifeCycle.onParameterInitialized(this, parameter, ann);
        }
    }
}
Also used : ForestInterceptorDefineException(com.dtflys.forest.exceptions.ForestInterceptorDefineException) ParamLifeCycle(com.dtflys.forest.annotation.ParamLifeCycle) ParameterAnnotationLifeCycle(com.dtflys.forest.lifecycles.ParameterAnnotationLifeCycle) Interceptor(com.dtflys.forest.interceptor.Interceptor) Annotation(java.lang.annotation.Annotation)

Example 3 with ForestInterceptorDefineException

use of com.dtflys.forest.exceptions.ForestInterceptorDefineException in project forest by dromara.

the class ForestMethod method getAnnotationLifeCycleClass.

private Class<? extends Interceptor> getAnnotationLifeCycleClass(Annotation annotation) {
    Class<? extends Annotation> annType = annotation.annotationType();
    Class<? extends MethodAnnotationLifeCycle> interceptorClass = null;
    MethodLifeCycle methodLifeCycleAnn = annType.getAnnotation(MethodLifeCycle.class);
    if (methodLifeCycleAnn == null) {
        BaseLifeCycle baseLifeCycle = annType.getAnnotation(BaseLifeCycle.class);
        if (baseLifeCycle != null) {
            Class<? extends BaseAnnotationLifeCycle> baseAnnLifeCycleClass = baseLifeCycle.value();
            if (baseAnnLifeCycleClass != null) {
                if (MethodAnnotationLifeCycle.class.isAssignableFrom(baseAnnLifeCycleClass)) {
                    interceptorClass = (Class<? extends MethodAnnotationLifeCycle>) baseAnnLifeCycleClass;
                } else {
                    return baseAnnLifeCycleClass;
                }
            }
        }
    }
    if (methodLifeCycleAnn != null || interceptorClass != null) {
        if (interceptorClass == null) {
            interceptorClass = methodLifeCycleAnn.value();
            if (!Interceptor.class.isAssignableFrom(interceptorClass)) {
                throw new ForestInterceptorDefineException(interceptorClass);
            }
        }
    }
    return interceptorClass;
}
Also used : MethodLifeCycle(com.dtflys.forest.annotation.MethodLifeCycle) ForestInterceptorDefineException(com.dtflys.forest.exceptions.ForestInterceptorDefineException) Interceptor(com.dtflys.forest.interceptor.Interceptor) BaseLifeCycle(com.dtflys.forest.annotation.BaseLifeCycle)

Aggregations

ForestInterceptorDefineException (com.dtflys.forest.exceptions.ForestInterceptorDefineException)3 Interceptor (com.dtflys.forest.interceptor.Interceptor)2 BaseLifeCycle (com.dtflys.forest.annotation.BaseLifeCycle)1 MethodLifeCycle (com.dtflys.forest.annotation.MethodLifeCycle)1 ParamLifeCycle (com.dtflys.forest.annotation.ParamLifeCycle)1 ParameterAnnotationLifeCycle (com.dtflys.forest.lifecycles.ParameterAnnotationLifeCycle)1 Annotation (java.lang.annotation.Annotation)1 Test (org.junit.Test)1