創建註解處理器
創建兩個java Library
- 創建註解lib_navanotation
@Target(ElementType.TYPE)
public @interface ActivityDestination {
String pageUrl();
boolean needLogin() default false;
boolean asStarter() default false;
}
@Target(ElementType.TYPE)
public @interface FragmentDestination {
String pageUrl();
boolean needLogin() default false;
boolean asStarter() default false;
}
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();
isFragment = false;
}
if (destMap.containsKey(pageUrl)) {
messager.printMessage(Diagnostic.Kind.ERROR, "不同的頁面不允許使用相同的pageUrl:" + clazName);
} else {
JSONObject object = new JSONObject();
object.put("id", id);
object.put("needLogin", needLogin);
object.put("asStarter", asStarter);
object.put("pageUrl", pageUrl);
object.put("clazName", clazName);
object.put("isFragment", isFragment);
destMap.put(pageUrl, object);
}
}
}
}
apply plugin: 'java-library'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
// AS-3.4.1 + geadles5.1.1-all+ auto-service:1.0-rc6
annotationProcessor 'com.google.auto.service:auto-service:1.0-rc7'
compileOnly 'com.google.auto.service:auto-service:1.0-rc7'
//3.4及其以下
// api 'com.google.auto.service:auto-service:1.0-rc6'
implementation 'com.alibaba:fastjson:1.2.68'
implementation project(path: ':lib_navannotation')
}
//中文亂碼問題(錯誤:編碼GBK不可映射字符)
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
sourceCompatibility = "8"
targetCompatibility = "8"
- 在Fragment上註冊註解
- app gradle下
compileOptions {
sourceCompatibility "1.8"
targetCompatibility "1.8"
}