赞
踩
- package com.wust.arouter_annotations;
-
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
-
- /**
- * ClassName: ARouter <br/>
- * Description: <br/>
- * date: 2022/2/10 15:13<br/>
- *
- * @author yiqi<br />
- * @QQ 1820762465
- * @微信 yiqiideallife
- * @技术交流QQ群 928023749
- */
-
- @Target(ElementType.TYPE) // 类上面
- @Retention(RetentionPolicy.CLASS) // 编译期
- public @interface ARouter {
-
- String path();
-
- String group() default "";
- }
- plugins {
- id 'java-library'
- }
-
- dependencies {
- implementation fileTree(dir: 'libs', includes: ['*.jar'])
-
- //背后的服务 --> 帮我们自动监视代码的编译
- compileOnly 'com.google.auto.service:auto-service:1.0-rc3'
- annotationProcessor 'com.google.auto.service:auto-service:1.0-rc4'
-
- //帮助我们通过类调用的形式来生成Java代码【javaPoet】
- implementation 'com.squareup:javapoet:1.9.0'
-
- //依赖注解
- implementation project(":arouter-annotations")
- }
-
- java {
- sourceCompatibility = JavaVersion.VERSION_1_7
- targetCompatibility = JavaVersion.VERSION_1_7
- }
- package com.wust.newmodueljavapoet;
-
- import android.app.Activity;
- import android.os.Bundle;
-
- import androidx.annotation.Nullable;
-
- import com.wust.arouter_annotations.ARouter;
-
- /**
- * ClassName: MainActivity <br/>
- * Description: <br/>
- * date: 2022/2/10 16:14<br/>
- *
- * @author yiqi<br />
- * @QQ 1820762465
- * @微信 yiqiideallife
- * @技术交流QQ群 928023749
- */
-
- @ARouter(path = "MainActivity")
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- }
- package com.wust.compiler;
-
- import com.google.auto.service.AutoService;
-
- import java.io.File;
- 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.RoundEnvironment;
- import javax.annotation.processing.SupportedAnnotationTypes;
- import javax.annotation.processing.SupportedSourceVersion;
- import javax.lang.model.SourceVersion;
- import javax.lang.model.element.TypeElement;
- import javax.lang.model.util.Elements;
- import javax.lang.model.util.Types;
-
- /**
- * ClassName: ARouterProcessor <br/>
- * Description: <br/>
- * date: 2022/2/10 16:20<br/>
- *
- * @author yiqi<br />
- * @QQ 1820762465
- * @微信 yiqiideallife
- * @技术交流QQ群 928023749
- */
-
- @AutoService(Processor.class) //启用服务 ,看清楚,别选择成了 Process.class,否则你死活看不到执行这个注解处理器
- @SupportedSourceVersion(SourceVersion.RELEASE_7) //环境的配置
- @SupportedAnnotationTypes("com.wust.arouter_annotations.ARouter") //明确这个注解处理器 处理 哪个注解
- public class ARouterProcessor extends AbstractProcessor {
-
- //操作 Element 的工具类(类,函数,属性,其实都是Element)
- private Elements elementTool;
-
- //type(类信息)的工具类,包含用于操作TypeMirror的工具方法
- private Types typeTool;
-
- //Message用来打印 日志相关信息
- private Messager messager;
-
- //文件生成器,类 资源 等,就是最终要生成的文件 是需要Filer来完成的
- private Filer filer;
-
- @Override
- public synchronized void init(ProcessingEnvironment processingEnv) {
- super.init(processingEnv);
-
- //初始化
- elementTool = processingEnv.getElementUtils();
- messager = processingEnv.getMessager();
- filer = processingEnv.getFiler();
- }
-
- //在编译的时候干活
- @Override
- public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
- return false;
- }
- }
总体思路:只有通过 gradle 来实现
- //传递参数
- javaCompileOptions {
- annotationProcessorOptions {
- arguments = [MyName: 'yiqi']
- }
- }
- //依赖注解处理器 注解处理器才能工作
- annotationProcessor project(":compiler")
- ···省略···
-
- /**
- * ClassName: ARouterProcessor <br/>
- * Description: <br/>
- * date: 2022/2/10 16:20<br/>
- *
- * @author yiqi<br />
- * @QQ 1820762465
- * @微信 yiqiideallife
- * @技术交流QQ群 928023749
- */
-
- //接收 安卓工程传递过来的参数
- @SupportedOptions("MyName")
-
- ···省略···
- public class ARouterProcessor extends AbstractProcessor {
-
- ···省略···
-
- @Override
- public synchronized void init(ProcessingEnvironment processingEnv) {
- super.init(processingEnv);
-
- ···省略···
-
- //输出获取到的值
- String value = processingEnv.getOptions().get("MyName");
- messager.printMessage(Diagnostic.Kind.NOTE, "heheheh>" + value);
- }
-
- //在编译的时候干活
- @Override
- public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
- messager.printMessage(Diagnostic.Kind.NOTE, "process is run");
- return false;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。