ARouter 系列 3:继续学习(手写一个 Arouter 框架)
// 上下文对象
private Context context;
public void init(Context context) {
this.context = context;
}
private ARouterTest() {
map = new HashMap<>();
}
public static ARouterTest getInstance() {
if (aRouterTest == null) {
synchronized (ARouterTest.class) {
if (aRouterTest == null) {
aRouterTest = new ARouterTest();
}
}
}
return aRouterTest;
}
/**
向路由表中添加信息
@param key 键
@param clazz 值
*/
public void addActivity(String key, Class<? extends Activity> clazz) {
if (key != null && clazz != null && !map.containsKey(key)) {
map.put(key, clazz);
}
}
/**
跳转窗体的方法
@param key 键
@param bundle 值
*/
public void jumpActivity(String key, Bundle bundle) {
Class<? extends Activity> classActivity = map.get(key);
if (classActivity != null) {
Intent intent = new Intent(context, classActivity);
if (bundle != null) {
intent.putExtras(bundle);
}
context.startActivity(intent);
}
}
}
接下来,我们需要把每个 module 的 activity 类信息存储到 ARouterTest 中,写一个接口类。
2.2.2、IRouterTest.java
public interface IRouterTest {
void putActivity();
}
2.2.3、ARouterConstant.java
public class ARouterConstant {
public static final String AROUTER_PARAM_LOGIN_ACTIVITY = "login/LoginActivity";
public static final String AROUTER_PARAM_UCMAIN_ACTIVITY = "uc/UCMainActivity";
}
2.2.4、ActivityUtil.java
public class ActivityUtil implements IRouterTest {
@Override
public void putActivity() {
ARouterTest.getInstance().addActivity(ARouterConstant.AROUTER_PARAM_LOGIN_ACTIVITY, LoginActivity.class);
}
}
但是这种手动添加的方式并不利于后期的维护。因此我们需要 APT 技术来帮助我们自动生成这些类。?
3、APT 技术
=======
APT(Annotation Processing Tool)即注解处理器,是一种处理注解的工具,确切的说它是 javac 的一个工具,它用来在编译时扫描和处理注解。注解处理器以 Java 代码(或者编译过的字节码)作为输入,生成**.java 文件**作为输出。
简单来说就是在编译期,通过注解生成**.java**文件。
APT 技术的核心有两块:注解和注解处理器。
4、手写一个 ARouter 框架
===============
接下来我们利用 APT 技术自己手写一个 ARouter 框架。
结构目录:
这个项目架构,一共四个 android module:app、login、usercenter、arouter,其中 app 依赖另外三个 module,arouter 被其他三个 module 依赖。
apt-annotation 和 apt-processor 是 java module,这两个 module 被 app、login 和 usercenter 依赖。如图:
implementation project(path: ':apt-annotation')
annotationProcessor project(path: ':apt-processor')
4.1、module:apt-annotation
该 module 只新建了一个注解类。
// 声明注解的作用域
@Target(ElementType.TYPE)
// 声明注解的生命周期
@Retention(RetentionPolicy.CLASS)
public @interface BindPath {
String path();
}
4.2、module:apt-processor
apply plugin: 'java-library'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(path: ':apt-annotation')
annotationProcessor 'com.google.auto.service:auto-service:1.0-rc4'
compileOnly 'com.google.auto.service:auto-service:1.0-rc3'
}
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
@AutoService(Processor.class)
public class Test1Processor extends AbstractProcessor {
Filer filer;
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
filer = processingEnv.getFiler();
}
@Override
public SourceVersion getSupportedSourceVersion() {
return processingEnv.getSourceVersion();
}
@Override
public Set<String> getSupportedAnnotationTypes() {
Set<String> types=new HashSet<>();
types.add(BindPath.class.getCanonicalName());
return types;
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
// 获取到当前模块中用到了 BindPath 注解的 activity 的类对象(类节点) (有几个模块中依赖了 apt-processor,该方法就会执行几次)
// 类节点 TypeElement 方法节点 ExecutableElement 方法节点 VariableElement
Set<? extends Element> elementAnnotationWith = roundEnv.getElementsAnnotatedWith(BindPath.class);
Map<String, String> map = new HashMap<>();
// 遍历整个模块中用到了 BindPath 注解的节点
for (Element element : elementAnnotationWith) {
TypeElement typeElement = (TypeElement) element;
// 获取到 activity 上面的 BindPath 的注解
BindPath annotation = typeElement.getAnnotation(BindPath.class);
// 获取到注解里面带的值 中间容器 map 的 activity 所对应的 key
String key = annotation.path();
// 获取到包名和类名
Name activityName = typeElement.getQualifiedName();
map.put(key, activityName + ".class");
}
// 写文件
if (map.size() > 0) {
Writer writer = null;
// 需要生成的文件名 让类名不重复
String activityName = "ActivityUtil" + System.currentTimeMillis();
try {
// 生成一个 Java 文件
JavaFileObject sourceFile = filer.createSourceFile("com.gs.util." + activityName);
// 从生成的这个文件开始写
writer = sourceFile.openWriter();
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("package com.gs.util;\n");
stringBuffer.append("import com.gs.arouter.ARouterTest;\n" +
"import com.gs.arouter.IRouterTest;\n" +
"\n" +
"public class " + activityName + " implements IRouterTest{\n" +
" @Override\n" +
" public void putActivity(){\n");
Iterator<String> iterator = map.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
String className = map.get(key);
stringBuffer.append(" ARouterTest.getInstance().addActivity("" + key + "", " +
className + ");");
}
stringBuffer.append("\n }\n}");
writer.write(stringBuffer.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return false;
}
}
这个很重要,不要搞错了。
4.3、module:arouter
4.3.1、IRouterTest.java
public interface IRouterTest {
void putActivity();
}
4.3.2、ARouterConstant.java
public class ARouterConstant {
public static final String AROUTER_PARAM_LOGIN_ACTIVITY = "login/LoginActivity";
public static final String AROUTER_PARAM_UCMAIN_ACTIVITY = "uc/UCMainActivity";
}
4.3.3、 ARouterTest.java
public class ARouterTest {
public volatile static ARouterTest aRouterTest;
private Context context;
private Map<String, Class<? extends Activity>> map;
private ARouterTest() {
map = new HashMap<>();
}
public static ARouterTest getInstance() {
if (aRouterTest == null) {
synchronized (ARouterTest.class) {
if (aRouterTest == null) {
aRouterTest = new ARouterTest();
}
}
}
return aRouterTest;
}
public void init(Context context) {
this.context = context;
List<String> classNames = getClassName("com.gs.util");
for (String className : classNames) {
try {
Class<?> clazz = Class.forName(className);
if (IRouterTest.class.isAssignableFrom(clazz)) {
IRouterTest iRouterTest = (IRouterTest) clazz.newInstance();
iRouterTest.putActivity();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void addActivity(String key, Class<? extends Activity> clazz) {
if (key != null && clazz != null && !map.containsKey(key)) {
map.put(key, clazz);
}
}
public void jumpActivity(String key, Bundle bundle) {
Class<? extends Activity> classActivity = map.get(key);
if
(classActivity != null) {
Intent intent = new Intent(context, classActivity);
if (bundle != null) {
intent.putExtras(bundle);
}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
/**
通过包名获取这个包下面的所有类名
@param packageName 包名
@return a list of class
*/
private List<String> getClassName(String packageName) {
List<String> classList = new ArrayList<>();
String path = null;
try {
path = context.getPackageManager()
.getApplicationInfo(context.getPackageName(), 0)
.sourceDir;
DexFile dexFile = new DexFile(path);
Enumeration entries = dexFile.entries();
while (entries.hasMoreElements()) {
String name = (String) entries.nextElement();
if (name.contains(packageName)) {
classList.add(name);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return classList;
}
}
4.4、使用
4.4.1、module:app
App.java?
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
ARouterTest.getInstance().init(this);
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ARouterTest.getInstance().jumpActivity(ARouterConstant.AROUTER_PARAM_LOGIN_ACTIVITY, null);
}
});
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
评论