第一章:navigation注解生成器生成json配置文件

apply plugin: ‘java-library’

dependencies {

implementation fileTree(dir: ‘libs’, include: [‘*.jar’])

}

//中文乱码问题(错误:编码GBK不可映射字符)

tasks.withType(JavaCompile) {

options.encoding = “UTF-8”

}

sourceCompatibility = “8”

targetCompatibility = “8”

  • 创建生成器navcompiler

在这里插入图片描述

package cn.example.lib_navcompiler;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;

import com.google.auto.service.AutoService;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.lang.annotation.Annotation;

import java.util.HashMap;

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.annotation.processing.SupportedAnnotationTypes;

import javax.annotation.processing.SupportedSourceVersion;

import javax.lang.model.SourceVersion;

import javax.lang.model.element.Element;

import javax.lang.model.element.TypeElement;

import javax.tools.Diagnostic;

import javax.tools.FileObject;

import javax.tools.StandardLocation;

import cn.example.lib_navannotation.ActivityDestination;

import cn.example.lib_navannotation.FragmentDestination;

@AutoService(Processor.class)

@SupportedSourceVersion(SourceVersion.RELEASE_8)

@SupportedAnnotationTypes({“cn.example.lib_navannotation.FragmentDestination”,

“cn.example.lib_navannotation.ActivityDestination”})

public class NavProcessor extends AbstractProcessor {

private Messager messager;

private Filer filer;

private static final String OUTPUT_FILE_NAME = “destination.json”;

@Override

public synchronized void init(ProcessingEnvironment processingEnvironment) {

super.init(processingEnvironment);

//日志打印,在java环境下不能使用android.util.log.e()

messager = processingEnv.getMessager();

//文件处理工具

filer = processingEnv.getFiler();

}

@Override

public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnv) {

//通过处理器环境上下文roundEnv分别获取 项目中标记的FragmentDestination.class 和ActivityDestination.class注解。

//此目的就是为了收集项目中哪些类 被注解标记了

Set<? extends Element> fragmentElements = roundEnv.getElementsAnnotatedWith(FragmentDestination.class);

Set<? extends Element> activityElements = roundEnv.getElementsAnnotatedWith(ActivityDestination.class);

if (!fragmentElements.isEmpty() || !activityElements.isEmpty()) {

HashMap<String, JSONObject> destMap = new HashMap<>();

//分别 处理FragmentDestination 和 ActivityDestination 注解类型

//并收集到destMap 这个map中。以此就能记录下所有的页面信息了

handleDestination(fragmentElements, FragmentDestination.class, destMap);

handleDestination(activityElements, ActivityDestination.class, destMap);

//app/src/main/assets

FileOutputStream fos = null;

OutputStreamWriter writer = null;

try {

//filer.createResource()意思是创建源文件

//我们可以指定为class文件输出的地方,

//StandardLocation.CLASS_OUTPUT:java文件生成class文件的位置,/app/build/intermediates/javac/debug/classes/目录下

//StandardLocation.SOURCE_OUTPUT:java文件的位置,一般在/app/build/generated/source/apt/目录下

//StandardLocation.CLASS_PATH 和 StandardLocation.SOURCE_PATH用的不多,指的了这个参数,就要指定生成文件的pkg包名了

FileObject resource = filer.createResource(StandardLocation.CLASS_OUTPUT, “”, OUTPUT_FILE_NAME);

String resourcePath = resource.toUri().getPath();

messager.printMessage(Diagnostic.Kind.NOTE, “resourcePath:” + resourcePath);

//由于我们想要把json文件生成在app/src/main/assets/目录下,所以这里可以对字符串做一个截取,

//以此便能准确获取项目在每个电脑上的 /app/src/main/assets/的路径

String appPath = resourcePath.substring(0, resourcePath.indexOf(“app”) + 4);

String assetsPath = appPath + “src/main/assets/”;

File file = new File(assetsPath);

if (!file.exists()) {

file.mkdir();

}

//此处就是稳健的写入了

File outPutFile = new File(file, OUTPUT_FILE_NAME);

if (outPutFile.exists()) {

outPutFile.delete();

}

outPutFile.createNewFile();

//利用fastjson把收集到的所有的页面信息 转换成JSON格式的。并输出到文件中

String content = JSON.toJSONString(destMap);

messager.printMessage(Diagnostic.Kind.NOTE, “content:” + content);

fos = new FileOutputStream(outPutFile);

writer = new OutputStreamWriter(fos, “UTF-8”);

writer.write(content);

writer.flush();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (writer != null) {

try {

writer.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (fos != null) {

try {

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

return true;

}

private void handleDestination(Set<? extends Element> elements,

Class<? extends Annotation> annotationClazz,

HashMap<String, JSONObject> destMap) {

for (Element element : elements) {

//TypeElement是Element的一种。

//如果我们的注解标记在了类名上。所以可以直接强转一下。使用它得到全类名

TypeElement typeElement = (TypeElement) element;

//全类名cn.example.jetpackdemo.ui.home.HomeFragment

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

messager.printMessage(Diagnostic.Kind.NOTE, “clazName:” + clazName);

//页面的id.此处不能重复,使用页面的类名做hascode即可

int id = Math.abs(clazName.hashCode());

//页面的pageUrl相当于隐士跳转意图中的host://schem/path格式

String pageUrl = null;

//是否需要登录

boolean needLogin = false;

//是否作为首页的第一个展示的页面

boolean asStarter = false;

//标记该页面是fragment 还是activity类型的

boolean isFragment = false;

Annotation annotation = typeElement.getAnnotation(annotationClazz);

if (annotation instanceof FragmentDestination) {

FragmentDestination dest = (FragmentDestination) annotation;

pageUrl = dest.pageUrl();

asStarter = dest.asStarter();

needLogin = dest.needLogin();

isFragment = true;

} else if (annotation instanceof ActivityDestination) {

ActivityDestination dest = (ActivityDestination) annotation;

pageUrl = dest.pageUrl();

asStarter = dest.asStarter();

needLogin = dest.needLogin();

Android开发除了flutter还有什么是必须掌握的吗?

相信大多数从事Android开发的朋友们越来越发现,找工作越来越难了,面试的要求越来越高了

除了基础扎实的java知识,数据结构算法,设计模式还要求会底层源码,NDK技术,性能调优,还有会些小程序和跨平台,比如说flutter,以思维脑图的方式展示在下图;

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
();

needLogin = dest.needLogin();

Android开发除了flutter还有什么是必须掌握的吗?

相信大多数从事Android开发的朋友们越来越发现,找工作越来越难了,面试的要求越来越高了

除了基础扎实的java知识,数据结构算法,设计模式还要求会底层源码,NDK技术,性能调优,还有会些小程序和跨平台,比如说flutter,以思维脑图的方式展示在下图;

[外链图片转存中…(img-uudNAjfn-1715122698277)]

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

  • 22
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值