Search in sources :

Example 1 with MethodAnnotationLifeCycle

use of com.dtflys.forest.lifecycles.MethodAnnotationLifeCycle in project forest by dromara.

the class ForestMethod method addMetaRequestAnnotation.

/**
 * 添加元请求注释
 * @param annotation 注解
 * @param interceptorClass 拦截器类
 */
private void addMetaRequestAnnotation(Annotation annotation, Class interceptorClass) {
    Class<? extends Annotation> annType = annotation.annotationType();
    RequestAttributes requestAttributesAnn = annType.getAnnotation(RequestAttributes.class);
    if (requestAttributesAnn != null) {
        Map<String, Object> attrTemplates = ReflectUtils.getAttributesFromAnnotation(annotation);
        for (String key : attrTemplates.keySet()) {
            Object value = attrTemplates.get(key);
            if (value instanceof CharSequence) {
                MappingTemplate template = makeTemplate(annType, key, value.toString());
                attrTemplates.put(key, template);
            } else if (String[].class.isAssignableFrom(value.getClass())) {
                String[] stringArray = (String[]) value;
                int len = stringArray.length;
                MappingTemplate[] templates = new MappingTemplate[stringArray.length];
                for (int i = 0; i < len; i++) {
                    String item = stringArray[i];
                    MappingTemplate template = makeTemplate(annType, key, item);
                    templates[i] = template;
                }
                attrTemplates.put(key, templates);
            }
        }
        InterceptorAttributes attributes = new InterceptorAttributes(interceptorClass, attrTemplates);
        if (interceptorAttributesList == null) {
            interceptorAttributesList = new LinkedList<>();
        }
        interceptorAttributesList.add(attributes);
    }
    Interceptor interceptor = addInterceptor(interceptorClass);
    if (interceptor instanceof MethodAnnotationLifeCycle) {
        MethodAnnotationLifeCycle lifeCycle = (MethodAnnotationLifeCycle) interceptor;
        lifeCycle.onMethodInitialized(this, annotation);
    }
}
Also used : InterceptorAttributes(com.dtflys.forest.interceptor.InterceptorAttributes) MethodAnnotationLifeCycle(com.dtflys.forest.lifecycles.MethodAnnotationLifeCycle) MappingTemplate(com.dtflys.forest.mapping.MappingTemplate) RequestAttributes(com.dtflys.forest.annotation.RequestAttributes) Interceptor(com.dtflys.forest.interceptor.Interceptor)

Aggregations

RequestAttributes (com.dtflys.forest.annotation.RequestAttributes)1 Interceptor (com.dtflys.forest.interceptor.Interceptor)1 InterceptorAttributes (com.dtflys.forest.interceptor.InterceptorAttributes)1 MethodAnnotationLifeCycle (com.dtflys.forest.lifecycles.MethodAnnotationLifeCycle)1 MappingTemplate (com.dtflys.forest.mapping.MappingTemplate)1