Draymonders / Code-Life

The marathon continues though.
27 stars 3 forks source link

Java 注解相关 #78

Open Draymonders opened 4 years ago

Draymonders commented 4 years ago

三种注解

  1. 标准注解 Override, Depraced, SuperWarnings
  2. 元注解(注解的注解)
    • Target(被作用目标)
    • Retention(注解的声明周期)
    • Documented(注解是否应当被放到JavaDoc中)
    • Inherited(注解是否可以被继承)
  3. 自定义注解
Draymonders commented 4 years ago
javac xxx.java => 生成 xxx.class
javap -v xxx => 反编译xxx.class
Draymonders commented 4 years ago

AnnotationElement三个常用的方法

// 获取类型为T的注解
<T extends Annotation> T getAnnotation(Class<T> var1);

// 获取所有的注解
Annotation[] getAnnotations();

// 判断当前元素是否含有 annotationClass 注解
default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
    return this.getAnnotation(annotationClass) != null;
}