java.lang.reflect 包的结构
目录
整体类结构图:
AnnotatedElement接口的含义,被注解的元素,实现了此接口的类即是可被注解的,jdk中实现此接口的类如下,
AccessibleObject, Class, Constructor, Executable, Field, Method, Package, Parameter(即主要是 类、字段、方法、参数、构造函数)
接口定义了如下方法,用于判断注解是否存在或获取注解:
isAnnotationPresent
getAnnotation
getAnnotations
getAnnotationsByType
getDeclaredAnnotation
getDeclaredAnnotationsByType
getDeclaredAnnotations
jdk1.8中增加了repeatable 元注解,被此注解标注的注解可用多次注解同一AnnotatedElement,如spring中
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Repeatable {
// Repeatable 注解定义时,值需要指定注解的容器类型,
//例如spring @ComponetScan的定义
Class<? extends Annotation> value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Repeatable(ComponentScans.class) // 注意为ComponentScans.class
public @interface ComponentScan {
@AliasFor("basePackages")
String[] value() default {};
.....
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
public @interface ComponentScans {
ComponentScan[] value(); //类型为ComponentScan 数组
}