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);
}
}
Aggregations