java注释跳转方法,Java自定义注解实现Router跳转

简介

Android项目架构中,随着业务的复杂越来越大,模块化/组件化是必须的。对于Activity之间的跳转希望通过注解自动实现,模仿ARouter做法,实现简单路由组件。

思路

创建注解lib模块,自定义注解

创建注解编译处理lib模块,实现添加注解标识的类和注解的映射关系

创建公用跳转的Router工具类

实现

项目中创建自定义注解module,类型为Java Library,如lib_annotation.创建自定义注解IRouter,如下

@Retention(RetentionPolicy.CLASS)

@Target(ElementType.TYPE)

public @interface IRouter {

// 注解参数

String path();

}

8558e9ae5dfe

image.png

项目中创建注解编译module,类型为Java Library,如lib_compiler。创建继承自AbstractProcessor的处理类并且重写方法process。

lib_compiler模块的build.gradle添加依赖:

implementation 'com.squareup:javapoet:1.11.1' //一款可以自动生成Java文件的第三方依赖

implementation 'com.google.auto.service:auto-service:1.0-rc5' //自动在META-INF文件夹下生成Processor配置信息文件

annotationProcessor 'com.google.auto.service:auto-service:1.0-rc5'

implementation project(":lib_annotation")

重写process方法

@AutoService(value = {Processor.class})

@SupportedAnnotationTypes(value = {"com.supcon.mes.lib_annotation.IRouter"})

@SupportedSourceVersion(value = SourceVersion.RELEASE_8)

public class IActivityProcess extends AbstractProcessor {

private Filer mFiler;

private static final String ROUTER_PACKAGE = "com.supcon.mes.router";

private static final String ROUTER_NAME = "Activity_Router_Mapper";

@Override

public synchronized void init(ProcessingEnvironment processingEnv) {

super.init(processingEnv);

mFiler = processingEnv.getFiler();

}

@Override

public boolean process(Set extends TypeElement> annotations, RoundEnvironment roundEnv) {

// 获取添加IRouter注解的所有类

Set extends Element> annotatedElements = roundEnv.getElementsAnnotatedWith(IRouter.class);

// 使用JavaPoet工具 创建注解映射类

TypeSpec.Builder builder = TypeSpec.classBuilder(ROUTER_NAME).addModifiers(PUBLIC);

for (Element annotatedElement : annotatedElements) {

// 获取注解参数path

String path = annotatedElement.getAnnotation(IRouter.class).path();

// 获取被注解的类名

String name = annotatedElement.getSimpleName().toString();

// 获取类名全称

TypeElement typeElement = (TypeElement) annotatedElement;

String className = typeElement.getQualifiedName().toString();

// 创建字段:以path为变量名,以类名为值

FieldSpec fieldSpec = FieldSpec.builder(String.class,path).addModifiers(PUBLIC).addModifiers(STATIC)

.initializer("$S",className).build();

// 将字段添加到类中

builder.addField(fieldSpec);

}

try {

// 创建Java文件

JavaFile javaFile = JavaFile.builder(ROUTER_PACKAGE,builder.build()).build();

javaFile.writeTo(mFiler);

} catch (Exception e) {

e.printStackTrace();

}

return true;

}

}

8558e9ae5dfe

image.png

3.需要的模块引入依赖,rebuild后在相应的模块路径.../build/generated/source/apt/debug/com.supcon.mes.router/下,创建一个名为Activity_Router_Mapper的映射java文件

implementation project(":lib_annotation")

implementation project(":lib_compiler")

annotationProcessor project(":lib_compiler")

8558e9ae5dfe

image.png

4.实现公用Router工具类

public class RouterHelper {

public static void goActivity(Context context,String path, Bundle extra){

Intent intent = new Intent(context,getActivity(path));

// intent.setClass(context,getActivity(path));

if (extra != null){

intent.putExtras(extra);

}

context.startActivity(intent);

}

private static Class> getActivity(String path) {

try {

Class> routerClass = Activity_Router_Mapper.class;

Field field = routerClass.getField(path);

return Class.forName(field.get(routerClass).toString());

} catch (NoSuchFieldException | ClassNotFoundException | IllegalAccessException e) {

e.printStackTrace();

return null;

}

}

}

5.使用

@IRouter(path = Constant.Router.LOGIN)

public class LoginActivity extends AppCompatActivity {

// ...TODO

}

@IRouter(path = Constant.Router.SETTING)

public class SettingsActivity extends AppCompatActivity {

// ...TODO

}

RouterHelper.goActivity(LoginActivity.this,Constant.Router.SETTING,null);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值