编译注解----实例

作者: 叁点水 | 来源:发表于2019-03-20 15:01 被阅读3次

这个 demo 是这篇博客里的一个例子,光看没看懂,又照着写了一遍也还是很懵,为了更好的理解,几乎把每一句核心代码都打印,都注释了,这样才略懂了一些在编译时生成代码这个流程。

下面这个方法是最懵的,所以注释写了很多,里面的各种元素都比较抽象,看的时候,甚至是第一次写的时候,都不知道在写什么,通过打印把抽象的东西,还原成我们熟悉的东西就好理解多了。

/**
 *
 * @param set  包含的是所有使用的[注解的信息],例如BindView,ContentView
 * @param roundEnvironment 返回的是所有被注解的[元素],例如类,属性等
 * @return
 */
 @Override
 public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
//        mMessager.printMessage(Diagnostic.Kind.NOTE,"process...");
​
 // 防止处理多次,要清空
 mProxyMap.clear();
​
 System.out.println("mProxyMap"+mProxyMap.toString());
 // 获取全部的带有 bindview 注解的 element
 Set<? extends Element> elseWithBind = roundEnvironment.getElementsAnnotatedWith(BindView.class);
 System.out.println("elseWithBind: " + elseWithBind.toString());
 //elseWithBind: [mTextView, mImageView]
​
 // 对bindview 进行循环,构建 proxyInfo 信息
 for (Element element : elseWithBind) {
 // 检查 element 的合法性
 checkSAnnotationValid(element,BindView.class);
​
 // 强转成属性元素
 VariableElement variableElement = (VariableElement) element;
 System.out.println("variableElement: "+variableElement);
//            variableElement: mTextView
​
 System.out.println("-----"+ element.getEnclosingElement());
​
 // 要获取类元素的类名,直接用 element 也可以,强转不是必须的。
 // 属性元素的外层一定是类元素
 TypeElement typeElement = (TypeElement) variableElement.getEnclosingElement();
 System.out.println("typeElement: "+typeElement);
 // typeElement: com.limiao.annotationdemo.MainActivity
​
 // 获取类元素的类名(全路径名)
 String fqClassName = typeElement.getQualifiedName().toString();
 System.out.println("fqClassName: "+fqClassName);
//            fqClassName: com.limiao.annotationdemo.MainActivity
​
 System.out.println("mProxyMap: "+mProxyMap);
​
 ProxyInfo proxyInfo = mProxyMap.get(fqClassName);
 if (proxyInfo == null){
 proxyInfo = new ProxyInfo(mElementUtils,typeElement);
 // 以 class 名称为 key,保存到 mProxy 中
 mProxyMap.put(fqClassName,proxyInfo);
 }
​
 System.out.println("proxyInfo:"+proxyInfo);
​
 // 获取 bindview 注解,把信息放入 proxyInfo 中
 BindView bindAnnotation = element.getAnnotation(BindView.class);
 int id = bindAnnotation.value();
 mMessager.printMessage(Diagnostic.Kind.NOTE,"proxyInfo:" + proxyInfo.toString());
 mMessager.printMessage(Diagnostic.Kind.NOTE,"variableElement:" + variableElement);
 mMessager.printMessage(Diagnostic.Kind.NOTE,"id:" + id);
 // 上面的强转要用到这里,作为参数
 proxyInfo.injectVarialbles.put(id,variableElement);
 }
​
 // 获取所有的 ContentView 注解,操作原理和上面的 bindview 一样
 Set<? extends Element> contentAnnotations = roundEnvironment.getElementsAnnotatedWith(ContentView.class);
 for (Element element : contentAnnotations) {
 TypeElement typeElement = (TypeElement) element;
 String fqClassName  = typeElement.getQualifiedName().toString();
 ProxyInfo proxyInfo = mProxyMap.get(fqClassName);
 if (proxyInfo == null) {
 proxyInfo = new ProxyInfo(mElementUtils,typeElement);
 mProxyMap.put(fqClassName,proxyInfo);
 }
 ContentView contentViewAnnotation = element.getAnnotation(ContentView.class);
 proxyInfo.contentViewId = contentViewAnnotation.value();
 }
​
 // 循环生成源文件
 for (String key : mProxyMap.keySet()) {
 ProxyInfo proxyInfo = mProxyMap.get(key);
 try {
 System.out.println("ProxyClassFullName: "+proxyInfo.getProxyClassFullName());
 System.out.println("TypeElement: "+proxyInfo.getTypeElement());
 // 创建一个 javaFile 文件
 // 第一个参数:创建的文件名,包含全路径
 // 第二个参数:与此文件的创建有因果关联的类型或包或模块元素,可以为null(文档的说明)
//                JavaFileObject jfo = processingEnv.getFiler().createSourceFile(proxyInfo.getProxyClassFullName(),proxyInfo.getTypeElement());
//                试了一下,如果第二个参数为 null ,也没有关系,还能正常编译运行
 JavaFileObject jfo = processingEnv.getFiler().createSourceFile(proxyInfo.getProxyClassFullName(),null);
 Writer writer = jfo.openWriter();
 writer.write(proxyInfo.generateJavaCode());
 writer.flush();
 writer.close();
 } catch (IOException e) {
​
 e.printStackTrace();
 error(proxyInfo.getTypeElement(),"unable to write injector for type %s: %s",proxyInfo.getTypeElement(),e.getMessage());
 }
 }
​
 return true;
​
 }

参考的这篇博客的作者关于编译注解写了一个系列的文章,都很好,这里整理一下贴出来,方便查阅。

深入理解编译注解(一)从实战理解什么是编译注解

深入理解编译注解(二)annotationProcessor与android-apt

深入理解编译注解(三)依赖关系 apt/annotationProcessor与Provided的区别

深入理解编译注解(四)常用接口介绍

深入理解编译注解(五)RetentionPolicy.SOURCE 和 RetentionPolicy.CLASS区别讨论

深入理解编译注解(六)Butterknife的实现原理

demo github 地址

相关文章

  • 编译注解----实例

    这个 demo 是这篇博客里的一个例子,光看没看懂,又照着写了一遍也还是很懵,为了更好的理解,几乎把每一句核心代码...

  • java注解

    使用 定义注解 使用注解 反编译 反编译MyAnnotation.class 反编译注解MyAnnotation$...

  • java-注解

    注解分类: 源码注解,编译注解,运行注解 源码注解:编译后的.class 后没有 boolean exists =...

  • 编译时注解器初探(一)

    编译时注解器初探(一) 注解处理器 (Annotation Processor) 编译时注解和运行时注解定义的方式...

  • 如何写出Java-Friendly的kotlin代码

    本文用实例讲解了各个kotlin用于编译出bytecode的注解的用法。希望大家通过这code出完美兼容java的...

  • java注解

    一、注解分类 源码注解(SOURCE):注解只在源码中存在,编译成.class文件就不存在 编译时注解(CLASS...

  • java常用注解分类

    1)按照运行机制划分: 【源码注解→编译时注解→运行时注解】 源码注解:只在源码中存在,编译成.class文件就不...

  • 注解

    分类 标准注解@override,@SupressWarning这类java自带注解,编译器识别,不会进行编译,也...

  • 注解的使用方式(运行期)

    代码结构 定义一个注解 注解的使用 注解的处理 辅助类 在Main中进行测试 编译 编译结果 运行 运行结果

  • AnnotationProcessor实战

    AnnotationProcessor 就是注解编译,通过它,我们可以在编译阶段拿到注解对象(类、属性、方法等)的...

网友评论

    本文标题:编译注解----实例

    本文链接:https://www.haomeiwen.com/subject/atozmqtx.html