Search in sources :

Example 1 with Interceptor

use of com.xsm.easy.annotation.Interceptor in project EasyRouter by Xiasm.

the class InterceptorProcessor method parseInterceptor.

/**
 * 解析拦截器
 *
 * @author luoxiaohui
 * @createTime 2019-05-23 20:12
 */
private void parseInterceptor(Set<? extends Element> elements) throws IOException {
    if (!Utils.isEmpty(elements)) {
        for (Element element : elements) {
            if (verify(element)) {
                Interceptor interceptor = element.getAnnotation(Interceptor.class);
                interceptors.put(interceptor.priority(), element);
            }
        }
        TypeElement iInterceptor = elementUtils.getTypeElement(Constant.IINTERCEPTOR);
        TypeElement iInterceptorGroup = elementUtils.getTypeElement(Constant.IINTERCEPTOR_GROUP);
        /**
         * Map<String, Class<? extends IInterceptor></>>
         */
        ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(ClassName.get(Map.class), ClassName.get(Integer.class), ParameterizedTypeName.get(ClassName.get(Class.class), WildcardTypeName.subtypeOf(ClassName.get(iInterceptor))));
        /**
         * 参数+变量名
         * Map<String, Class<? extends IInterceptor>> interceptors
         */
        ParameterSpec parameterSpec = ParameterSpec.builder(parameterizedTypeName, "interceptors").build();
        /**
         * 构建方法
         * public void loadInto(Map<String, Class<? extends IInterceptor>> interceptors){}
         */
        MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(Constant.METHOD_LOAD_INTO).addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).addParameter(parameterSpec);
        if (!interceptors.isEmpty() && interceptors.size() > 0) {
            /**
             * 构建方法体中的语句
             */
            for (Map.Entry<Integer, Element> entry : interceptors.entrySet()) {
                methodBuilder.addStatement("interceptors.put(" + entry.getKey() + ", $T.class)", ClassName.get((TypeElement) entry.getValue()));
            }
        }
        /**
         * 将文件写入磁盘中
         * 路径是在app/build/source/api/debug/PACKAGE_OF_GENERATE_FILE下面
         */
        JavaFile.builder(Constant.PACKAGE_OF_GENERATE_FILE, TypeSpec.classBuilder(Constant.NAME_OF_INTERCEPTOR + moduleName).addModifiers(Modifier.PUBLIC).addMethod(methodBuilder.build()).addSuperinterface(ClassName.get(iInterceptorGroup)).build()).build().writeTo(filerUtils);
    }
}
Also used : ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec(com.squareup.javapoet.MethodSpec) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) Interceptor(com.xsm.easy.annotation.Interceptor) HashMap(java.util.HashMap) Map(java.util.Map) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Aggregations

MethodSpec (com.squareup.javapoet.MethodSpec)1 ParameterSpec (com.squareup.javapoet.ParameterSpec)1 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)1 Interceptor (com.xsm.easy.annotation.Interceptor)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Element (javax.lang.model.element.Element)1 TypeElement (javax.lang.model.element.TypeElement)1