一、directly present, indirectly present, present, and associated
question
在API文档中,获取Java注解的方法的返回值注释directly present, indirectly present, present, and associated是什么含义?
引子
API documentation:AnnotatedElement (Java SE 15 & JDK 15) (oracle.com)
The terms directly present, indirectly present, present, and associated are used throughout this interface to describe precisely which annotations are returned by methods:
- An annotation A is directly present on an element E if E has a
RuntimeVisibleAnnotations
orRuntimeVisibleParameterAnnotations
orRuntimeVisibleTypeAnnotations
attribute, and the attribute contains A. - An annotation A is indirectly present on an element E if E has a
RuntimeVisibleAnnotations
orRuntimeVisibleParameterAnnotations
orRuntimeVisibleTypeAnnotations
attribute, and A 's type is repeatable, and the attribute contains exactly one annotation whose value element contains A and whose type is the containing annotation type of A 's type. - An annotation A is present on an element E if either:
- A is directly present on E; or
- No annotation of A 's type is directly present on E, and E is a class, and A 's type is inheritable, and A is present on the superclass of E.
- An annotation A is associated with an element E if either:
- A is directly or indirectly present on E; or
- No annotation of A 's type is directly or indirectly present on E, and E is a class, and A's type is inheritable, and A is associated with the superclass of E.
The table below summarizes which kind of annotation presence different methods in this interface examine.
Method | Kind of Presence | ||||
---|---|---|---|---|---|
Return Type | Signature | Directly Present | Indirectly Present | Present | Associated |
T |
getAnnotation(Class<T>) |
X | |||
Annotation[] |
getAnnotations() |
X | |||
T[] |