atom
This commit is contained in:
1
arouter_annotation/.gitignore
vendored
Normal file
1
arouter_annotation/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/build
|
||||
14
arouter_annotation/build.gradle
Normal file
14
arouter_annotation/build.gradle
Normal file
@@ -0,0 +1,14 @@
|
||||
apply plugin: 'java-library'
|
||||
|
||||
dependencies {
|
||||
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
||||
}
|
||||
|
||||
// java控制台输出中文乱码
|
||||
tasks.withType(JavaCompile) {
|
||||
options.encoding = "UTF-8"
|
||||
}
|
||||
|
||||
|
||||
sourceCompatibility = "7"
|
||||
targetCompatibility = "7"
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.xiangxue.arouter_annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* <strong>Activity使用的布局文件注解</strong>
|
||||
* <ul>
|
||||
* <li>@Target(ElementType.TYPE) // 接口、类、枚举、注解</li>
|
||||
* <li>@Target(ElementType.FIELD) // 属性、枚举的常量</li>
|
||||
* <li>@Target(ElementType.METHOD) // 方法</li>
|
||||
* <li>@Target(ElementType.PARAMETER) // 方法参数</li>
|
||||
* <li>@Target(ElementType.CONSTRUCTOR) // 构造函数</li>
|
||||
* <li>@Target(ElementType.LOCAL_VARIABLE)// 局部变量</li>
|
||||
* <li>@Target(ElementType.ANNOTATION_TYPE)// 该注解使用在另一个注解上</li>
|
||||
* <li>@Target(ElementType.PACKAGE) // 包</li>
|
||||
* <li>@Retention(RetentionPolicy.RUNTIME) <br>注解会在class字节码文件中存在,jvm加载时可以通过反射获取到该注解的内容</li>
|
||||
* </ul>
|
||||
*
|
||||
* 生命周期:SOURCE < CLASS < RUNTIME
|
||||
* 1、一般如果需要在运行时去动态获取注解信息,用RUNTIME注解
|
||||
* 2、要在编译时进行一些预处理操作,如ButterKnife,用CLASS注解。注解会在class文件中存在,但是在运行时会被丢弃
|
||||
* 3、做一些检查性的操作,如@Override,用SOURCE源码注解。注解仅存在源码级别,在编译的时候丢弃该注解
|
||||
*
|
||||
* @〆 奋斗吧
|
||||
* APT中,的确有时候使用 SOURCE,有时候使用CLASS,但是他们从角色上考虑,还是有细微区别的哦
|
||||
* CLASS:要在编译时进行一些预处理操作,如ButterKnife,用CLASS注解。注解会在class文件中存在
|
||||
* SOURCE:做一些检查性的操作,既然是做一些检查工作,是不是和编译期非常类似,但是他的角色是,如@Override,用SOURCE源码注解。注解仅存在源码级别
|
||||
*/
|
||||
@Target(ElementType.TYPE) // 该注解作用在类之上
|
||||
@Retention(RetentionPolicy.CLASS) // 要在编译时进行一些预处理操作,注解会在class文件中存在
|
||||
public @interface ARouter {
|
||||
|
||||
// 详细路由路径(必填),如:"/app/MainActivity"
|
||||
String path();
|
||||
|
||||
// 路由组名(选填,如果开发者不填写,可以从path中截取出来)
|
||||
String group() default "";
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.xiangxue.arouter_annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* <strong>Activity使用的布局文件注解</strong>
|
||||
* <ul>
|
||||
* <li>@Target(ElementType.TYPE) // 接口、类、枚举、注解</li>
|
||||
* <li>@Target(ElementType.FIELD) // 属性、枚举的常量</li>
|
||||
* <li>@Target(ElementType.METHOD) // 方法</li>
|
||||
* <li>@Target(ElementType.PARAMETER) // 方法参数</li>
|
||||
* <li>@Target(ElementType.CONSTRUCTOR) // 构造函数</li>
|
||||
* <li>@Target(ElementType.LOCAL_VARIABLE)// 局部变量</li>
|
||||
* <li>@Target(ElementType.ANNOTATION_TYPE)// 该注解使用在另一个注解上</li>
|
||||
* <li>@Target(ElementType.PACKAGE) // 包</li>
|
||||
* <li>@Retention(RetentionPolicy.RUNTIME) <br>注解会在class字节码文件中存在,jvm加载时可以通过反射获取到该注解的内容</li>
|
||||
* </ul>
|
||||
*
|
||||
* 生命周期:SOURCE < CLASS < RUNTIME
|
||||
* 1、一般如果需要在运行时去动态获取注解信息,用RUNTIME注解
|
||||
* 2、要在编译时进行一些预处理操作,如ButterKnife,用CLASS注解。注解会在class文件中存在,但是在运行时会被丢弃
|
||||
* 3、做一些检查性的操作,如@Override,用SOURCE源码注解。注解仅存在源码级别,在编译的时候丢弃该注解
|
||||
*/
|
||||
@Target(ElementType.FIELD) // 该注解作用在类之上 字段上有作用
|
||||
@Retention(RetentionPolicy.CLASS) // 要在编译时进行一些预处理操作,注解会在class文件中存在
|
||||
public @interface Parameter {
|
||||
|
||||
// 不填写name的注解值表示该属性名就是key,填写了就用注解值作为key
|
||||
// 从getIntent()方法中获取传递参数值
|
||||
String name() default "";
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
package com.xiangxue.arouter_annotation.bean;
|
||||
|
||||
import javax.lang.model.element.Element;
|
||||
|
||||
/**
|
||||
* 最终路由 要 传递 对象
|
||||
*
|
||||
* 路由路径Path的最终实体封装类
|
||||
* 例如:app分组中的MainActivity对象,这个对象有更多的属性
|
||||
*/
|
||||
public class RouterBean {
|
||||
|
||||
// 为了以后的发展
|
||||
public enum TypeEnum {
|
||||
ACTIVITY,
|
||||
CALL
|
||||
}
|
||||
|
||||
private TypeEnum typeEnum; // 枚举类型:Activity
|
||||
private Element element; // 类节点 JavaPoet学习的时候,可以拿到很多的信息
|
||||
private Class<?> myClass; // 被注解的 Class对象 例如: MainActivity.class Main2Activity.class
|
||||
private String path; // 路由地址 例如:/app/MainActivity
|
||||
private String group; // 路由组 例如:app order personal
|
||||
|
||||
// TODO 以下是一组 Get 方法
|
||||
public TypeEnum getTypeEnum() {
|
||||
return typeEnum;
|
||||
}
|
||||
|
||||
public Element getElement() {
|
||||
return element;
|
||||
}
|
||||
|
||||
public Class<?> getMyClass() {
|
||||
return myClass;
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
public String getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
// TODO 以下是一组 Set 方法
|
||||
public void setTypeEnum(TypeEnum typeEnum) {
|
||||
this.typeEnum = typeEnum;
|
||||
}
|
||||
|
||||
public void setElement(Element element) {
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
public void setMyClass(Class<?> myClass) {
|
||||
this.myClass = myClass;
|
||||
}
|
||||
|
||||
public void setPath(String path) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
public void setGroup(String group) {
|
||||
this.group = group;
|
||||
}
|
||||
|
||||
private RouterBean(TypeEnum typeEnum, /*Element element,*/ Class<?> myClass, String path, String group) {
|
||||
this.typeEnum = typeEnum;
|
||||
// this.element = element;
|
||||
this.myClass = myClass;
|
||||
this.path = path;
|
||||
this.group = group;
|
||||
}
|
||||
|
||||
// 对外暴露
|
||||
// 对外提供简易版构造方法,主要是为了方便APT生成代码
|
||||
public static RouterBean create(TypeEnum type, Class<?> clazz, String path, String group) {
|
||||
return new RouterBean(type, clazz, path, group);
|
||||
}
|
||||
|
||||
// 构建者模式相关
|
||||
private RouterBean(Builder builder) {
|
||||
this.typeEnum = builder.type;
|
||||
this.element = builder.element;
|
||||
this.myClass = builder.clazz;
|
||||
this.path = builder.path;
|
||||
this.group = builder.group;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建者模式
|
||||
*/
|
||||
public static class Builder {
|
||||
|
||||
// 枚举类型:Activity
|
||||
private TypeEnum type;
|
||||
// 类节点
|
||||
private Element element;
|
||||
// 注解使用的类对象
|
||||
private Class<?> clazz;
|
||||
// 路由地址
|
||||
private String path;
|
||||
// 路由组
|
||||
private String group;
|
||||
|
||||
public Builder addType(TypeEnum type) {
|
||||
this.type = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder addElement(Element element) {
|
||||
this.element = element;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder addClazz(Class<?> clazz) {
|
||||
this.clazz = clazz;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder addPath(String path) {
|
||||
this.path = path;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder addGroup(String group) {
|
||||
this.group = group;
|
||||
return this;
|
||||
}
|
||||
|
||||
// 最后的build或者create,往往是做参数的校验或者初始化赋值工作
|
||||
public RouterBean build() {
|
||||
if (path == null || path.length() == 0) {
|
||||
throw new IllegalArgumentException("path必填项为空,如:/app/MainActivity");
|
||||
}
|
||||
return new RouterBean(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "RouterBean{" +
|
||||
"path='" + path + '\'' +
|
||||
", group='" + group + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user