package com.agilezhu.processor;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.auto.service.AutoService;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import javax.tools.Diagnostic;
/**
* Generate ARouterPage.java for ARouter
*/
@AutoService(Processor.class)
public class ARouterProcessor extends AbstractProcessor {
private Filer mFiler; //文件相关工具类:用于保存生成的java类文件
private Elements mElementUtils; //元素相关工具类:用于获取java类文件
private Messager mMessager;//用于打印日志
@Override
public synchronized void init(ProcessingEnvironment processingEnvironment) {
super.init(processingEnvironment);
mFiler = processingEnv.getFiler();
mElementUtils = processingEnv.getElementUtils();
mMessager = processingEnv.getMessager();
}
@Override
public Set<String> getSupportedAnnotationTypes() {
//返回该注解处理器能够处理哪些注解
Set<String> types = new LinkedHashSet<>();
types.add(Route.class.getName());
return types;
}
@Override
public SourceVersion getSupportedSourceVersion() {
//返回当前注解处理器支持的java版本号
return SourceVersion.latest();
}
@Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
if (set == null || set.size() == 0) {
return false;
}
//将要生成的java完整类名
String targetClassName = "ARouterPage";
String packageName = "com.agilezhu.processor.generate";
//创建方法(构造方法)
MethodSpec.Builder bindMethodBuilder = MethodSpec.methodBuilder("<init>")
.addModifiers(Modifier.PUBLIC);
//创建类
TypeSpec.Builder aRouterPageClassBuilder = TypeSpec.classBuilder(targetClassName)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
//获取所有的源码文件
Set<? extends Element> elements = roundEnvironment.getRootElements();
for (Element element : elements) {
if (!(element instanceof TypeElement)) {//判断是否class类
continue;
}
//转换成class类型
TypeElement typeElement = (TypeElement) element;
//当前文件的类名
String classSimpleName = element.getSimpleName().toString();
Route routeAnnotation = element.getAnnotation(Route.class);
if (routeAnnotation == null) {
continue;
}
mMessager.printMessage(Diagnostic.Kind.WARNING, "myProcess=====>>>>>" + classSimpleName);
ClassName innerClassName = ClassName.get(packageName, targetClassName, classSimpleName);
try {
aRouterPageClassBuilder.addField(innerClassName, classSimpleName)
.addType(TypeSpec.classBuilder(innerClassName.simpleName())
.addModifiers(Modifier.STATIC, Modifier.PUBLIC)
.addField(String.class, "path", Modifier.PUBLIC, Modifier.STATIC)
.addStaticBlock(CodeBlock.builder().addStatement("path= \"" + routeAnnotation.path() + "\"").build())
.addMethod(MethodSpec
.methodBuilder("navigation")
.addParameter(String.class,"key")
.addParameter(ClassName.get("android.os","Bundle"), "params")
.addStatement("com.alibaba.android.arouter.launcher.ARouter.getInstance().build(path).withBundle(key,params).navigation()")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.build())
.addMethod(MethodSpec
.methodBuilder("navigation")
.addStatement("com.alibaba.android.arouter.launcher.ARouter.getInstance().build(path).navigation()")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.build())
.build());
} catch (Exception e) {
e.printStackTrace();
}
//构造方法中添加初始化代码
bindMethodBuilder.addStatement(classSimpleName + ".path = \"" + routeAnnotation.path() + "\"");
}
aRouterPageClassBuilder.addMethod(bindMethodBuilder.build());
//创建java文件
JavaFile bindProxyFile = JavaFile
.builder(packageName, aRouterPageClassBuilder.build())
.build();
try {
//保存java类文件
bindProxyFile.writeTo(mFiler);
} catch (Throwable e) {
e.printStackTrace();
}
return false;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
processor.zip (10个子文件)
src
main
java
com
agilezhu
processor
ARouterProcessor.java 6KB
.gitignore 6B
build
tmp
jar
MANIFEST.MF 25B
compileJava
previous-compilation-data.bin 4KB
classes
java
main
META-INF
services
javax.annotation.processing.Processor 41B
com
agilezhu
processor
ARouterProcessor.class 8KB
generated
sources
annotationProcessor
java
main
headers
java
main
.transforms
8d8352708473e8fcb53e7b2b23dcb36a
transformed
jetified-processor.jar 4KB
results.bin 26B
libs
processor.jar 4KB
build.gradle 652B
libs
共 10 条
- 1
guangdeshishe
- 粉丝: 211
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0