注解
- Annotation, JDK5.0 引入的一种注释机制
- 注解是元数据的一种形式,提供有关于程序但不属于程序本身的数据。
- 注解对它们注解的代码的操作没有直接影响。
- 标签,单独没有任何意义;架构设计会很有用
注解声明
- Java中所有的注解,默认实现 Annotation 接口:
  package java.lang.annotation;
  public interface Annotation{
    boolean equals(Object val);
    int hashCode();
    String toString();
    Class<? extends Annotaion> annotationType();
}
- 使用@interface
@Target(ElementType.TYPE,ElementType.METHOD);//元注解,注解上的注解叫元注解
@Rentation(RentationPolicy.SOURCE);// 保留级别
  public @interface Lee{
    String value() default "xxx" ;//元素,默认值 
}
// value 比较特殊 @Lee("22")
// 改成id 需要 @Lee(id = "22")
- 
ElementType 作用的范围 
  
- 
RentationPolicy 
  

- ASM Bytecode Viewer 【https://plugins.jetbrains.com/plugin/10302-asm-bytecode-viewer/versions/】
 使用插件很好的能看出来,注解编译在哪
 RetentionPolicy.SOURCE 注解只出现在源码中
 RetentionPolicy.CLASS 注解出现在字节码中

APT (Annotation Processor Tools)注解处理器
- 运行在编译时期
- 运行流程:
 .java -> javac -> .class
 采集所有注解信息,包装成节点-> Element ->注解处理程序
使用
extends AbstractProcessor

新建文件
main/resources/META-INF.services/javax.annotation.processing.Processer
com.example.LeeProcessor //全类名
//打印
Messager m= processingEnv.getMessager();
m.printMessage(Diagnostic.Kind.NOTE,"");
@supportedAnnotationTypes("com.example.Lee")//只关心的注解

IDE 语法检查
- IDE实现,IDE插件实现
 @IntDef @DrawableRes
一个对象占用?


字节码增强【插桩】
- 在字节码中写代码
 AspectJ、热修复
- .class -> 格式 【数据按照特定的方式排列、记录】
 .class->IO ->byte[] ->乱改

- 
Javac -classpath 【注解处理程序】 
  
反射
- 反射就是Reflection,Java的反射是指程序在运行期可以拿到一个对象的所有信息。
- 都是基于class
注解和反射实现findViewByID
Class<? extends Activity> cls= activity.getClass();
cls.getFiled(): 获得自己+父类成员(不包括private,只能是public);
cls.getDeclaredFields():只能获得自己的成员变量(不包括父辈,所有作用域)
cls.getSuperClass(); //获得父类
Field[] fields = cls.getDeclaredFields();//获取子类所有的成员
for(Field f : fields){
if(f.isAnnotationPresent(InjectView.class)){//判断属性是否被打上标记
//
InjectView injectView=  f.getAnnotation(InjectView.class));
int id = injectView.value();//获取注解头信息
View view = activity.findViewById(id);
//反射设置 属性的值
f.setAccessible(true);//设置访问权限 private 属性修改时需要,public不需要
f.set(activity,view);//反射给属性赋值
}
}
- 打标签
 @Retention(RententionPolicy.Runtime)
 @target(ElementType.Fileld)
 public @interface InjectView{
 @IdRes int value();//语法检查
}
反射获取泛型真实类型
Type体系


//获得泛型类型

//有花括号是创建匿名内部类,子类具象化了
//没有花括号是创建对象









