类 AnnotationUtils
Note that most of the features of this class are not provided by the JDK's introspection facilities themselves.
As a general rule for runtime-retained application annotations (e.g. for
transaction control, authorization, or service exposure), always use the
lookup methods on this class (e.g. findAnnotation(Method, Class) or
getAnnotation(Method, Class)) instead of the plain annotation lookup
methods in the JDK. You can still explicitly choose between a get
lookup on the given class level only (getAnnotation(Method, Class))
and a find lookup in the entire inheritance hierarchy of the given
method (findAnnotation(Method, Class)).
Terminology
The terms directly present, indirectly present, and present have the same meanings as defined in the class-level javadoc forAnnotatedElement (in Java 8).
An annotation is meta-present on an element if the annotation
is declared as a meta-annotation on some other annotation which is
present on the element. Annotation A is meta-present
on another annotation if A is either directly present or
meta-present on the other annotation.
Meta-annotation Support
Most find*() methods and some get*() methods in this class
provide support for finding annotations used as meta-annotations. Consult the
javadoc for each method in this class for details. For fine-grained support for
meta-annotations with attribute overrides in composed annotations,
consider using AnnotatedElementUtils's more specific methods instead.
Attribute Aliases
All public methods in this class that return annotations, arrays of
annotations, or AnnotationAttributes transparently support attribute
aliases configured via @AliasFor. Consult the various
synthesizeAnnotation*(..) methods for details.
Search Scope
The search algorithms used by methods in this class stop searching for an annotation once the first annotation of the specified type has been found. As a consequence, additional annotations of the specified type will be silently ignored.
- 从以下版本开始:
- 4.0 2021/7/28 21:15
- 作者:
- Rob Harrop, Juergen Hoeller, Sam Brannen, Mark Fisher, Chris Beams, Phillip Webb, Oleg Zhurakousky, Harry Yang
- 另请参阅:
-
嵌套类概要
嵌套类修饰符和类型类说明private static final recordInternal holder used to wrap default values. -
字段概要
字段修饰符和类型字段说明private static final Map<Class<? extends Annotation>,Map<String, AnnotationUtils.DefaultValueHolder>> private static final AnnotationFilterstatic final StringThe attribute name for annotations with a single element. -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明private static ObjectadaptValue(Object annotatedElement, Object value, boolean classValuesAsString) static voidClear the internal annotation metadata cache.private static Map<String,AnnotationUtils.DefaultValueHolder> computeDefaultValues(Class<? extends Annotation> annotationType) static <A extends Annotation>
AfindAnnotation(Class<?> clazz, Class<A> annotationType) Find a singleAnnotationofannotationTypeon the suppliedClass, traversing its interfaces, annotations, and superclasses if the annotation is not directly present on the given class itself.static <A extends Annotation>
AfindAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType) static <A extends Annotation>
AfindAnnotation(Method method, Class<A> annotationType) Find a singleAnnotationofannotationTypeon the suppliedMethod, traversing its super methods (i.e. from superclasses and interfaces) if the annotation is not directly present on the given method itself.static Class<?>findAnnotationDeclaringClass(Class<? extends Annotation> annotationType, Class<?> clazz) Find the firstClassin the inheritance hierarchy of the specifiedclazz(including the specifiedclazzitself) on which an annotation of the specifiedannotationTypeis directly present.static Class<?>findAnnotationDeclaringClassForTypes(List<Class<? extends Annotation>> annotationTypes, Class<?> clazz) Find the firstClassin the inheritance hierarchy of the specifiedclazz(including the specifiedclazzitself) on which at least one of the specifiedannotationTypesis directly present.static <A extends Annotation>
AgetAnnotation(Annotation annotation, Class<A> annotationType) Get a singleAnnotationofannotationTypefrom the supplied annotation: either the given annotation itself or a direct meta-annotation thereof.static <A extends Annotation>
AgetAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType) Get a singleAnnotationofannotationTypefrom the suppliedAnnotatedElement, where the annotation is either present or meta-present on theAnnotatedElement.static <A extends Annotation>
AgetAnnotation(Method method, Class<A> annotationType) Get a singleAnnotationofannotationTypefrom the suppliedMethod, where the annotation is either present or meta-present on the method.getAnnotationAttributes(Annotation annotation) Retrieve the given annotation's attributes as aMap, preserving all attribute types.getAnnotationAttributes(Annotation annotation, boolean classValuesAsString) Retrieve the given annotation's attributes as aMap.static AnnotationAttributesgetAnnotationAttributes(Annotation annotation, boolean classValuesAsString, boolean nestedAnnotationsAsMap) Retrieve the given annotation's attributes as anAnnotationAttributesmap.static AnnotationAttributesgetAnnotationAttributes(AnnotatedElement annotatedElement, Annotation annotation) Retrieve the given annotation's attributes as anAnnotationAttributesmap.static AnnotationAttributesgetAnnotationAttributes(AnnotatedElement annotatedElement, Annotation annotation, boolean classValuesAsString, boolean nestedAnnotationsAsMap) Retrieve the given annotation's attributes as anAnnotationAttributesmap.static Annotation[]getAnnotations(AnnotatedElement annotatedElement) Get allAnnotationsthat are present on the suppliedAnnotatedElement.static Annotation[]getAnnotations(Method method) Get allAnnotationsthat are present on the suppliedMethod.private static ObjectgetAttributeValueForMirrorResolution(Method attribute, Object attributes) static <A extends Annotation>
Set<A>getDeclaredRepeatableAnnotations(AnnotatedElement annotatedElement, Class<A> annotationType) Get the declared repeatable annotations ofannotationTypefrom the suppliedAnnotatedElement, where such annotations are either directly present, indirectly present, or meta-present on the element.static <A extends Annotation>
Set<A>getDeclaredRepeatableAnnotations(AnnotatedElement annotatedElement, Class<A> annotationType, Class<? extends Annotation> containerAnnotationType) Get the declared repeatable annotations ofannotationTypefrom the suppliedAnnotatedElement, where such annotations are either directly present, indirectly present, or meta-present on the element.static ObjectgetDefaultValue(Annotation annotation) Retrieve the default value of thevalueattribute of a single-element Annotation, given an annotation instance.static ObjectgetDefaultValue(Annotation annotation, String attributeName) Retrieve the default value of a named attribute, given an annotation instance.static ObjectgetDefaultValue(Class<? extends Annotation> annotationType) Retrieve the default value of thevalueattribute of a single-element Annotation, given theannotation type.static ObjectgetDefaultValue(Class<? extends Annotation> annotationType, String attributeName) Retrieve the default value of a named attribute, given theannotation type.private static Map<String,AnnotationUtils.DefaultValueHolder> getDefaultValues(Class<? extends Annotation> annotationType) static <A extends Annotation>
Set<A>getRepeatableAnnotations(AnnotatedElement annotatedElement, Class<A> annotationType) Get the repeatable annotations ofannotationTypefrom the suppliedAnnotatedElement, where such annotations are either present, indirectly present, or meta-present on the element.static <A extends Annotation>
Set<A>getRepeatableAnnotations(AnnotatedElement annotatedElement, Class<A> annotationType, Class<? extends Annotation> containerAnnotationType) Get the repeatable annotations ofannotationTypefrom the suppliedAnnotatedElement, where such annotations are either present, indirectly present, or meta-present on the element.static ObjectgetValue(Annotation annotation) Retrieve the value of thevalueattribute of a single-element Annotation, given an annotation instance.static ObjectgetValue(Annotation annotation, String attributeName) Retrieve the value of a named attribute, given an annotation instance.(专用程序包) static voidhandleIntrospectionFailure(AnnotatedElement element, Throwable ex) Handle the supplied annotation introspection exception.static booleanisAnnotationDeclaredLocally(Class<? extends Annotation> annotationType, Class<?> clazz) Determine whether an annotation of the specifiedannotationTypeis declared locally (i.e.static booleanisAnnotationInherited(Class<? extends Annotation> annotationType, Class<?> clazz) Determine whether an annotation of the specifiedannotationTypeis present on the suppliedclazzand is inherited (i.e. not directly present).static booleanisAnnotationMetaPresent(Class<? extends Annotation> annotationType, Class<? extends Annotation> metaAnnotationType) Determine if an annotation of typemetaAnnotationTypeis meta-present on the suppliedannotationType.static booleanisCandidateClass(Class<?> clazz, Class<? extends Annotation> annotationType) Determine whether the given class is a candidate for carrying the specified annotation (at type, method or field level).static booleanisCandidateClass(Class<?> clazz, Class<? extends Annotation> annotationType1, Class<? extends Annotation> annotationType2) Determine whether the given class is a candidate for carrying the specified annotation (at type, method or field level).static booleanisCandidateClass(Class<?> clazz, String annotationName) Determine whether the given class is a candidate for carrying the specified annotation (at type, method or field level).static booleanisCandidateClass(Class<?> clazz, Collection<Class<? extends Annotation>> annotationTypes) Determine whether the given class is a candidate for carrying one of the specified annotations (at type, method or field level).static booleanisInJavaLangAnnotationPackage(Annotation annotation) Determine if the suppliedAnnotationis defined in the core JDKjava.lang.annotationpackage.static booleanisInJavaLangAnnotationPackage(String annotationType) Determine if theAnnotationwith the supplied name is defined in the core JDKjava.lang.annotationpackage.static <A extends Annotation>
booleanisPresent(AnnotatedElement element, Class<A> annType) Whether aAnnotationpresent onAnnotatedElementprivate static <A extends Annotation>
booleanisSingleLevelPresent(MergedAnnotation<A> mergedAnnotation) static booleanisSynthesizedAnnotation(Annotation annotation) Determine if the suppliedAnnotationhas been synthesized by Infra (i.e. wrapped in a dynamic proxy) with additional functionality such as attribute alias handling.static voidpostProcessAnnotationAttributes(Object annotatedElement, AnnotationAttributes attributes, boolean classValuesAsString) Post-process the suppliedAnnotationAttributes, preserving nested annotations asAnnotationinstances.static voidregisterDefaultValues(AnnotationAttributes attributes) Register the annotation-declared default values for the given attributes, if available.(专用程序包) static voidIf the supplied throwable is anAnnotationConfigurationException, it will be cast to anAnnotationConfigurationExceptionand thrown, allowing it to propagate to the caller.static <A extends Annotation>
AsynthesizeAnnotation(A annotation, AnnotatedElement annotatedElement) Synthesize an annotation from the suppliedannotationby wrapping it in a dynamic proxy that transparently enforces attribute alias semantics for annotation attributes that are annotated with@AliasFor.static <A extends Annotation>
AsynthesizeAnnotation(Class<A> annotationType) Synthesize an annotation from its default attributes values.static <A extends Annotation>
AsynthesizeAnnotation(Map<String, Object> attributes, Class<A> annotationType, AnnotatedElement annotatedElement) Synthesize an annotation from the supplied map of annotation attributes by wrapping the map in a dynamic proxy that implements an annotation of the specifiedannotationTypeand transparently enforces attribute alias semantics for annotation attributes that are annotated with@AliasFor.(专用程序包) static Annotation[]synthesizeAnnotationArray(Annotation[] annotations, AnnotatedElement annotatedElement) Synthesize an array of annotations from the supplied array ofannotationsby creating a new array of the same size and type and populating it with synthesized versions of the annotations from the input array.static voidvalidateAnnotation(Annotation annotation) Check the declared attributes of the given annotation, in particular covering Google App Engine's late arrival ofTypeNotPresentExceptionProxyforClassvalues (instead of earlyClass.getAnnotations() failure.
-
字段详细资料
-
VALUE
The attribute name for annotations with a single element.- 另请参阅:
-
JAVA_LANG_ANNOTATION_FILTER
-
defaultValuesCache
private static final Map<Class<? extends Annotation>,Map<String, defaultValuesCacheAnnotationUtils.DefaultValueHolder>>
-
-
构造器详细资料
-
AnnotationUtils
public AnnotationUtils()
-
-
方法详细资料
-
isPresent
public static <A extends Annotation> boolean isPresent(@Nullable AnnotatedElement element, @Nullable Class<A> annType) Whether aAnnotationpresent onAnnotatedElement- 类型参数:
A-Annotationtype- 参数:
element- TargetAnnotatedElementannType- Target annotation type- 返回:
- Whether it's present
-
rethrowAnnotationConfigurationException
If the supplied throwable is anAnnotationConfigurationException, it will be cast to anAnnotationConfigurationExceptionand thrown, allowing it to propagate to the caller.Otherwise, this method does nothing.
- 参数:
ex- the throwable to inspect
-
handleIntrospectionFailure
Handle the supplied annotation introspection exception.If the supplied exception is an
AnnotationConfigurationException, it will simply be thrown, allowing it to propagate to the caller, and nothing will be logged.Otherwise, this method logs an introspection failure (in particular for a
TypeNotPresentException) before moving on, assuming nestedClassvalues were not resolvable within annotation attributes and thereby effectively pretending there were no annotations on the specified element.- 参数:
element- the element that we tried to introspect annotations onex- the exception that we encountered- 另请参阅:
-
isCandidateClass
public static boolean isCandidateClass(Class<?> clazz, Collection<Class<? extends Annotation>> annotationTypes) Determine whether the given class is a candidate for carrying one of the specified annotations (at type, method or field level).- 参数:
clazz- the class to introspectannotationTypes- the searchable annotation types- 返回:
falseif the class is known to have no such annotations at any level;trueotherwise. Callers will usually perform full method/field introspection iftrueis being returned here.- 另请参阅:
-
isCandidateClass
public static boolean isCandidateClass(Class<?> clazz, @Nullable Class<? extends Annotation> annotationType) Determine whether the given class is a candidate for carrying the specified annotation (at type, method or field level).- 参数:
clazz- the class to introspectannotationType- the searchable annotation type- 返回:
falseif the class is known to have no such annotations at any level;trueotherwise. Callers will usually perform full method/field introspection iftrueis being returned here.- 另请参阅:
-
isCandidateClass
public static boolean isCandidateClass(Class<?> clazz, @Nullable Class<? extends Annotation> annotationType1, @Nullable Class<? extends Annotation> annotationType2) Determine whether the given class is a candidate for carrying the specified annotation (at type, method or field level).- 参数:
clazz- the class to introspectannotationType1- the searchable annotation typeannotationType2- the searchable annotation type- 返回:
falseif the class is known to have no such annotations at any level;trueotherwise. Callers will usually perform full method/field introspection iftrueis being returned here.- 另请参阅:
-
isCandidateClass
Determine whether the given class is a candidate for carrying the specified annotation (at type, method or field level).- 参数:
clazz- the class to introspectannotationName- the fully-qualified name of the searchable annotation type- 返回:
falseif the class is known to have no such annotations at any level;trueotherwise. Callers will usually perform full method/field introspection iftrueis being returned here.- 另请参阅:
-
getAnnotation
@Nullable public static <A extends Annotation> A getAnnotation(Annotation annotation, Class<A> annotationType) Get a singleAnnotationofannotationTypefrom the supplied annotation: either the given annotation itself or a direct meta-annotation thereof.Note that this method supports only a single level of meta-annotations. For support for arbitrary levels of meta-annotations, use one of the
find*()methods instead.- 参数:
annotation- the Annotation to checkannotationType- the annotation type to look for, both locally and as a meta-annotation- 返回:
- the first matching annotation, or
nullif not found
-
getAnnotation
@Nullable public static <A extends Annotation> A getAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType) Get a singleAnnotationofannotationTypefrom the suppliedAnnotatedElement, where the annotation is either present or meta-present on theAnnotatedElement.Note that this method supports only a single level of meta-annotations. For support for arbitrary levels of meta-annotations, use
findAnnotation(AnnotatedElement, Class)instead.- 参数:
annotatedElement- theAnnotatedElementfrom which to get the annotationannotationType- the annotation type to look for, both locally and as a meta-annotation- 返回:
- the first matching annotation, or
nullif not found
-
isSingleLevelPresent
private static <A extends Annotation> boolean isSingleLevelPresent(MergedAnnotation<A> mergedAnnotation) -
getAnnotation
@Nullable public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationType) Get a singleAnnotationofannotationTypefrom the suppliedMethod, where the annotation is either present or meta-present on the method.Correctly handles bridge
Methodsgenerated by the compiler.Note that this method supports only a single level of meta-annotations. For support for arbitrary levels of meta-annotations, use
findAnnotation(Method, Class)instead.- 参数:
method- the method to look for annotations onannotationType- the annotation type to look for- 返回:
- the first matching annotation, or
nullif not found - 另请参阅:
-
getAnnotations
Get allAnnotationsthat are present on the suppliedAnnotatedElement.Meta-annotations will not be searched.
- 参数:
annotatedElement- the Method, Constructor or Field to retrieve annotations from- 返回:
- the annotations found, an empty array, or
nullif not resolvable (e.g. because nested Class values in annotation attributes failed to resolve at runtime) - 另请参阅:
-
getAnnotations
Get allAnnotationsthat are present on the suppliedMethod.Correctly handles bridge
Methodsgenerated by the compiler.Meta-annotations will not be searched.
- 参数:
method- the Method to retrieve annotations from- 返回:
- the annotations found, an empty array, or
nullif not resolvable (e.g. because nested Class values in annotation attributes failed to resolve at runtime) - 另请参阅:
-
getRepeatableAnnotations
public static <A extends Annotation> Set<A> getRepeatableAnnotations(AnnotatedElement annotatedElement, Class<A> annotationType) Get the repeatable annotations ofannotationTypefrom the suppliedAnnotatedElement, where such annotations are either present, indirectly present, or meta-present on the element.This method mimics the functionality of Java 8's
AnnotatedElement.getAnnotationsByType(Class)with support for automatic detection of a container annotation declared via @Repeatable(when running on Java 8 or higher) and with additional support for meta-annotations.Handles both single annotations and annotations nested within a container annotation.
Correctly handles bridge methods generated by the compiler if the supplied element is a
Method.Meta-annotations will be searched if the annotation is not present on the supplied element.
- 参数:
annotatedElement- the element to look for annotations onannotationType- the annotation type to look for- 返回:
- the annotations found or an empty set (never
null) - 另请参阅:
-
getRepeatableAnnotations(AnnotatedElement, Class, Class)getDeclaredRepeatableAnnotations(AnnotatedElement, Class, Class)AnnotatedElementUtils.getMergedRepeatableAnnotations(AnnotatedElement, Class)BridgeMethodResolver.findBridgedMethod(java.lang.reflect.Method)RepeatableAnnotatedElement.getAnnotationsByType(java.lang.Class<T>)
-
getRepeatableAnnotations
public static <A extends Annotation> Set<A> getRepeatableAnnotations(AnnotatedElement annotatedElement, Class<A> annotationType, @Nullable Class<? extends Annotation> containerAnnotationType) Get the repeatable annotations ofannotationTypefrom the suppliedAnnotatedElement, where such annotations are either present, indirectly present, or meta-present on the element.This method mimics the functionality of Java 8's
AnnotatedElement.getAnnotationsByType(Class)with additional support for meta-annotations.Handles both single annotations and annotations nested within a container annotation.
Correctly handles bridge methods generated by the compiler if the supplied element is a
Method.Meta-annotations will be searched if the annotation is not present on the supplied element.
- 参数:
annotatedElement- the element to look for annotations onannotationType- the annotation type to look forcontainerAnnotationType- the type of the container that holds the annotations; may benullif a container is not supported or if it should be looked up via @Repeatablewhen running on Java 8 or higher- 返回:
- the annotations found or an empty set (never
null) - 另请参阅:
-
getRepeatableAnnotations(AnnotatedElement, Class)getDeclaredRepeatableAnnotations(AnnotatedElement, Class)getDeclaredRepeatableAnnotations(AnnotatedElement, Class, Class)AnnotatedElementUtils.getMergedRepeatableAnnotations(AnnotatedElement, Class, Class)BridgeMethodResolver.findBridgedMethod(java.lang.reflect.Method)RepeatableAnnotatedElement.getAnnotationsByType(java.lang.Class<T>)
-
getDeclaredRepeatableAnnotations
public static <A extends Annotation> Set<A> getDeclaredRepeatableAnnotations(AnnotatedElement annotatedElement, Class<A> annotationType) Get the declared repeatable annotations ofannotationTypefrom the suppliedAnnotatedElement, where such annotations are either directly present, indirectly present, or meta-present on the element.This method mimics the functionality of Java 8's
AnnotatedElement.getDeclaredAnnotationsByType(Class)with support for automatic detection of a container annotation declared via @Repeatable(when running on Java 8 or higher) and with additional support for meta-annotations.Handles both single annotations and annotations nested within a container annotation.
Correctly handles bridge methods generated by the compiler if the supplied element is a
Method.Meta-annotations will be searched if the annotation is not present on the supplied element.
- 参数:
annotatedElement- the element to look for annotations onannotationType- the annotation type to look for- 返回:
- the annotations found or an empty set (never
null) - 另请参阅:
-
getRepeatableAnnotations(AnnotatedElement, Class)getRepeatableAnnotations(AnnotatedElement, Class, Class)getDeclaredRepeatableAnnotations(AnnotatedElement, Class, Class)AnnotatedElementUtils.getMergedRepeatableAnnotations(AnnotatedElement, Class)BridgeMethodResolver.findBridgedMethod(java.lang.reflect.Method)RepeatableAnnotatedElement.getDeclaredAnnotationsByType(java.lang.Class<T>)
-
getDeclaredRepeatableAnnotations
public static <A extends Annotation> Set<A> getDeclaredRepeatableAnnotations(AnnotatedElement annotatedElement, Class<A> annotationType, @Nullable Class<? extends Annotation> containerAnnotationType) Get the declared repeatable annotations ofannotationTypefrom the suppliedAnnotatedElement, where such annotations are either directly present, indirectly present, or meta-present on the element.This method mimics the functionality of Java 8's
AnnotatedElement.getDeclaredAnnotationsByType(Class)with additional support for meta-annotations.Handles both single annotations and annotations nested within a container annotation.
Correctly handles bridge methods generated by the compiler if the supplied element is a
Method.Meta-annotations will be searched if the annotation is not present on the supplied element.
- 参数:
annotatedElement- the element to look for annotations onannotationType- the annotation type to look forcontainerAnnotationType- the type of the container that holds the annotations; may benullif a container is not supported or if it should be looked up via @Repeatablewhen running on Java 8 or higher- 返回:
- the annotations found or an empty set (never
null) - 另请参阅:
-
getRepeatableAnnotations(AnnotatedElement, Class)getRepeatableAnnotations(AnnotatedElement, Class, Class)getDeclaredRepeatableAnnotations(AnnotatedElement, Class)AnnotatedElementUtils.getMergedRepeatableAnnotations(AnnotatedElement, Class, Class)BridgeMethodResolver.findBridgedMethod(java.lang.reflect.Method)RepeatableAnnotatedElement.getDeclaredAnnotationsByType(java.lang.Class<T>)
-
findAnnotation
@Nullable public static <A extends Annotation> A findAnnotation(AnnotatedElement annotatedElement, @Nullable Class<A> annotationType) Find a singleAnnotationofannotationTypeon the suppliedAnnotatedElement.Meta-annotations will be searched if the annotation is not directly present on the supplied element.
Warning: this method operates generically on annotated elements. In other words, this method does not execute specialized search algorithms for classes or methods. If you require the more specific semantics of
findAnnotation(Class, Class)orfindAnnotation(Method, Class), invoke one of those methods instead.- 参数:
annotatedElement- theAnnotatedElementon which to find the annotationannotationType- the annotation type to look for, both locally and as a meta-annotation- 返回:
- the first matching annotation, or
nullif not found
-
findAnnotation
@Nullable public static <A extends Annotation> A findAnnotation(Method method, @Nullable Class<A> annotationType) Find a singleAnnotationofannotationTypeon the suppliedMethod, traversing its super methods (i.e. from superclasses and interfaces) if the annotation is not directly present on the given method itself.Correctly handles bridge
Methodsgenerated by the compiler.Meta-annotations will be searched if the annotation is not directly present on the method.
Annotations on methods are not inherited by default, so we need to handle this explicitly.
- 参数:
method- the method to look for annotations onannotationType- the annotation type to look for- 返回:
- the first matching annotation, or
nullif not found - 另请参阅:
-
findAnnotation
@Nullable public static <A extends Annotation> A findAnnotation(Class<?> clazz, @Nullable Class<A> annotationType) Find a singleAnnotationofannotationTypeon the suppliedClass, traversing its interfaces, annotations, and superclasses if the annotation is not directly present on the given class itself.This method explicitly handles class-level annotations which are not declared as
inheritedas well as meta-annotations and annotations on interfaces.The algorithm operates as follows:
- Search for the annotation on the given class and return it if found.
- Recursively search through all annotations that the given class declares.
- Recursively search through all interfaces that the given class declares.
- Recursively search through the superclass hierarchy of the given class.
Note: in this context, the term recursively means that the search process continues by returning to step #1 with the current interface, annotation, or superclass as the class to look for annotations on.
- 参数:
clazz- the class to look for annotations onannotationType- the type of annotation to look for- 返回:
- the first matching annotation, or
nullif not found
-
findAnnotationDeclaringClass
@Nullable public static Class<?> findAnnotationDeclaringClass(Class<? extends Annotation> annotationType, @Nullable Class<?> clazz) Find the firstClassin the inheritance hierarchy of the specifiedclazz(including the specifiedclazzitself) on which an annotation of the specifiedannotationTypeis directly present.If the supplied
clazzis an interface, only the interface itself will be checked; the inheritance hierarchy for interfaces will not be traversed.Meta-annotations will not be searched.
The standard
ClassAPI does not provide a mechanism for determining which class in an inheritance hierarchy actually declares anAnnotation, so we need to handle this explicitly.- 参数:
annotationType- the annotation type to look forclazz- the class to check for the annotation on (may benull)- 返回:
- the first
Classin the inheritance hierarchy that declares an annotation of the specifiedannotationType, ornullif not found - 另请参阅:
-
findAnnotationDeclaringClassForTypes
@Nullable public static Class<?> findAnnotationDeclaringClassForTypes(List<Class<? extends Annotation>> annotationTypes, @Nullable Class<?> clazz) Find the firstClassin the inheritance hierarchy of the specifiedclazz(including the specifiedclazzitself) on which at least one of the specifiedannotationTypesis directly present.If the supplied
clazzis an interface, only the interface itself will be checked; the inheritance hierarchy for interfaces will not be traversed.Meta-annotations will not be searched.
The standard
ClassAPI does not provide a mechanism for determining which class in an inheritance hierarchy actually declares one of several candidate annotations, so we need to handle this explicitly.- 参数:
annotationTypes- the annotation types to look forclazz- the class to check for the annotation on (maybenull)- 返回:
- the first
Classin the inheritance hierarchy that declares an annotation of at least one of the specifiedannotationTypes, ornullif not found - 另请参阅:
-
isAnnotationDeclaredLocally
public static boolean isAnnotationDeclaredLocally(Class<? extends Annotation> annotationType, Class<?> clazz) Determine whether an annotation of the specifiedannotationTypeis declared locally (i.e. directly present) on the suppliedclazz.The supplied
Classmay represent any type.Meta-annotations will not be searched.
Note: This method does not determine if the annotation is inherited.
- 参数:
annotationType- the annotation type to look forclazz- the class to check for the annotation on- 返回:
trueif an annotation of the specifiedannotationTypeis directly present- 另请参阅:
-
isAnnotationInherited
public static boolean isAnnotationInherited(Class<? extends Annotation> annotationType, Class<?> clazz) Determine whether an annotation of the specifiedannotationTypeis present on the suppliedclazzand is inherited (i.e. not directly present).Meta-annotations will not be searched.
If the supplied
clazzis an interface, only the interface itself will be checked. In accordance with standard meta-annotation semantics in Java, the inheritance hierarchy for interfaces will not be traversed. See the javadoc for the@Inheritedmeta-annotation for further details regarding annotation inheritance.- 参数:
annotationType- the annotation type to look forclazz- the class to check for the annotation on- 返回:
trueif an annotation of the specifiedannotationTypeis present and inherited- 另请参阅:
-
isAnnotationMetaPresent
public static boolean isAnnotationMetaPresent(Class<? extends Annotation> annotationType, @Nullable Class<? extends Annotation> metaAnnotationType) Determine if an annotation of typemetaAnnotationTypeis meta-present on the suppliedannotationType.- 参数:
annotationType- the annotation type to search onmetaAnnotationType- the type of meta-annotation to search for- 返回:
trueif such an annotation is meta-present
-
isInJavaLangAnnotationPackage
Determine if the suppliedAnnotationis defined in the core JDKjava.lang.annotationpackage.- 参数:
annotation- the annotation to check- 返回:
trueif the annotation is in thejava.lang.annotationpackage
-
isInJavaLangAnnotationPackage
Determine if theAnnotationwith the supplied name is defined in the core JDKjava.lang.annotationpackage.- 参数:
annotationType- the name of the annotation type to check- 返回:
trueif the annotation is in thejava.lang.annotationpackage
-
validateAnnotation
Check the declared attributes of the given annotation, in particular covering Google App Engine's late arrival ofTypeNotPresentExceptionProxyforClassvalues (instead of earlyClass.getAnnotations() failure.This method not failing indicates that
getAnnotationAttributes(Annotation)won't failure either (when attempted later on).- 参数:
annotation- the annotation to validate- 抛出:
IllegalStateException- if a declaredClassattribute could not be read- 另请参阅:
-
getAnnotationAttributes
Retrieve the given annotation's attributes as aMap, preserving all attribute types.Equivalent to calling
getAnnotationAttributes(Annotation, boolean, boolean)with theclassValuesAsStringandnestedAnnotationsAsMapparameters set tofalse.Note: This method actually returns an
AnnotationAttributesinstance. However, theMapsignature has been preserved for binary compatibility.- 参数:
annotation- the annotation to retrieve the attributes for- 返回:
- the Map of annotation attributes, with attribute names as keys and
corresponding attribute values as values (never
null) - 另请参阅:
-
getAnnotationAttributes
public static Map<String,Object> getAnnotationAttributes(Annotation annotation, boolean classValuesAsString) Retrieve the given annotation's attributes as aMap.Equivalent to calling
getAnnotationAttributes(Annotation, boolean, boolean)with thenestedAnnotationsAsMapparameter set tofalse.Note: This method actually returns an
AnnotationAttributesinstance. However, theMapsignature has been preserved for binary compatibility.- 参数:
annotation- the annotation to retrieve the attributes forclassValuesAsString- whether to convert Class references into Strings (for compatibility withAnnotationMetadata) or to preserve them as Class references- 返回:
- the Map of annotation attributes, with attribute names as keys and
corresponding attribute values as values (never
null) - 另请参阅:
-
getAnnotationAttributes
public static AnnotationAttributes getAnnotationAttributes(Annotation annotation, boolean classValuesAsString, boolean nestedAnnotationsAsMap) Retrieve the given annotation's attributes as anAnnotationAttributesmap.This method provides fully recursive annotation reading capabilities on par with the reflection-based
StandardAnnotationMetadata.- 参数:
annotation- the annotation to retrieve the attributes forclassValuesAsString- whether to convert Class references into Strings (for compatibility withAnnotationMetadata) or to preserve them as Class referencesnestedAnnotationsAsMap- whether to convert nested annotations intoAnnotationAttributesmaps (for compatibility withAnnotationMetadata) or to preserve them asAnnotationinstances- 返回:
- the annotation attributes (a specialized Map) with attribute names as keys
and corresponding attribute values as values (never
null)
-
getAnnotationAttributes
public static AnnotationAttributes getAnnotationAttributes(@Nullable AnnotatedElement annotatedElement, Annotation annotation) Retrieve the given annotation's attributes as anAnnotationAttributesmap.Equivalent to calling
getAnnotationAttributes(AnnotatedElement, Annotation, boolean, boolean)with theclassValuesAsStringandnestedAnnotationsAsMapparameters set tofalse.- 参数:
annotatedElement- the element that is annotated with the supplied annotation; may benullif unknownannotation- the annotation to retrieve the attributes for- 返回:
- the annotation attributes (a specialized Map) with attribute names as keys
and corresponding attribute values as values (never
null) - 另请参阅:
-
getAnnotationAttributes
public static AnnotationAttributes getAnnotationAttributes(@Nullable AnnotatedElement annotatedElement, Annotation annotation, boolean classValuesAsString, boolean nestedAnnotationsAsMap) Retrieve the given annotation's attributes as anAnnotationAttributesmap.This method provides fully recursive annotation reading capabilities on par with the reflection-based
StandardAnnotationMetadata.- 参数:
annotatedElement- the element that is annotated with the supplied annotation; may benullif unknownannotation- the annotation to retrieve the attributes forclassValuesAsString- whether to convert Class references into Strings (for compatibility withAnnotationMetadata) or to preserve them as Class referencesnestedAnnotationsAsMap- whether to convert nested annotations intoAnnotationAttributesmaps (for compatibility withAnnotationMetadata) or to preserve them asAnnotationinstances- 返回:
- the annotation attributes (a specialized Map) with attribute names as keys
and corresponding attribute values as values (never
null)
-
registerDefaultValues
Register the annotation-declared default values for the given attributes, if available.- 参数:
attributes- the annotation attributes to process
-
getDefaultValues
private static Map<String,AnnotationUtils.DefaultValueHolder> getDefaultValues(Class<? extends Annotation> annotationType) -
computeDefaultValues
private static Map<String,AnnotationUtils.DefaultValueHolder> computeDefaultValues(Class<? extends Annotation> annotationType) -
postProcessAnnotationAttributes
public static void postProcessAnnotationAttributes(@Nullable Object annotatedElement, @Nullable AnnotationAttributes attributes, boolean classValuesAsString) Post-process the suppliedAnnotationAttributes, preserving nested annotations asAnnotationinstances.Specifically, this method enforces attribute alias semantics for annotation attributes that are annotated with
@AliasForand replaces default value placeholders with their original default values.- 参数:
annotatedElement- the element that is annotated with an annotation or annotation hierarchy from which the supplied attributes were created; may benullif unknownattributes- the annotation attributes to post-processclassValuesAsString- whether to convert Class references into Strings (for compatibility withAnnotationMetadata) or to preserve them as Class references- 另请参阅:
-
getAttributeValueForMirrorResolution
-
adaptValue
-
getValue
Retrieve the value of thevalueattribute of a single-element Annotation, given an annotation instance.- 参数:
annotation- the annotation instance from which to retrieve the value- 返回:
- the attribute value, or
nullif not found unless the attribute value cannot be retrieved due to anAnnotationConfigurationException, in which case such an exception will be rethrown - 另请参阅:
-
getValue
@Nullable public static Object getValue(@Nullable Annotation annotation, @Nullable String attributeName) Retrieve the value of a named attribute, given an annotation instance.- 参数:
annotation- the annotation instance from which to retrieve the valueattributeName- the name of the attribute value to retrieve- 返回:
- the attribute value, or
nullif not found unless the attribute value cannot be retrieved due to anAnnotationConfigurationException, in which case such an exception will be rethrown - 另请参阅:
-
getDefaultValue
Retrieve the default value of thevalueattribute of a single-element Annotation, given an annotation instance.- 参数:
annotation- the annotation instance from which to retrieve the default value- 返回:
- the default value, or
nullif not found - 另请参阅:
-
getDefaultValue
@Nullable public static Object getDefaultValue(@Nullable Annotation annotation, @Nullable String attributeName) Retrieve the default value of a named attribute, given an annotation instance.- 参数:
annotation- the annotation instance from which to retrieve the default valueattributeName- the name of the attribute value to retrieve- 返回:
- the default value of the named attribute, or
nullif not found - 另请参阅:
-
getDefaultValue
Retrieve the default value of thevalueattribute of a single-element Annotation, given theannotation type.- 参数:
annotationType- the annotation type for which the default value should be retrieved- 返回:
- the default value, or
nullif not found - 另请参阅:
-
getDefaultValue
@Nullable public static Object getDefaultValue(@Nullable Class<? extends Annotation> annotationType, @Nullable String attributeName) Retrieve the default value of a named attribute, given theannotation type.- 参数:
annotationType- the annotation type for which the default value should be retrievedattributeName- the name of the attribute value to retrieve.- 返回:
- the default value of the named attribute, or
nullif not found - 另请参阅:
-
synthesizeAnnotation
public static <A extends Annotation> A synthesizeAnnotation(A annotation, @Nullable AnnotatedElement annotatedElement) Synthesize an annotation from the suppliedannotationby wrapping it in a dynamic proxy that transparently enforces attribute alias semantics for annotation attributes that are annotated with@AliasFor.- 参数:
annotation- the annotation to synthesizeannotatedElement- the element that is annotated with the supplied annotation; may benullif unknown- 返回:
- the synthesized annotation if the supplied annotation is
synthesizable;
nullif the supplied annotation isnull; otherwise the supplied annotation unmodified - 抛出:
AnnotationConfigurationException- if invalid configuration of@AliasForis detected- 另请参阅:
-
synthesizeAnnotation
Synthesize an annotation from its default attributes values.This method simply delegates to
synthesizeAnnotation(Map, Class, AnnotatedElement), supplying an empty map for the source attribute values andnullfor theAnnotatedElement.- 参数:
annotationType- the type of annotation to synthesize- 返回:
- the synthesized annotation
- 抛出:
IllegalArgumentException- if a required attribute is missingAnnotationConfigurationException- if invalid configuration of@AliasForis detected- 另请参阅:
-
synthesizeAnnotation
public static <A extends Annotation> A synthesizeAnnotation(Map<String, Object> attributes, Class<A> annotationType, @Nullable AnnotatedElement annotatedElement) Synthesize an annotation from the supplied map of annotation attributes by wrapping the map in a dynamic proxy that implements an annotation of the specifiedannotationTypeand transparently enforces attribute alias semantics for annotation attributes that are annotated with@AliasFor.The supplied map must contain a key-value pair for every attribute defined in the supplied
annotationTypethat is not aliased or does not have a default value. Nested maps and nested arrays of maps will be recursively synthesized into nested annotations or nested arrays of annotations, respectively.Note that
AnnotationAttributesis a specialized type ofMapthat is an ideal candidate for this method'sattributesargument.- 参数:
attributes- the map of annotation attributes to synthesizeannotationType- the type of annotation to synthesizeannotatedElement- the element that is annotated with the annotation corresponding to the supplied attributes; may benullif unknown- 返回:
- the synthesized annotation
- 抛出:
IllegalArgumentException- if a required attribute is missing or if an attribute is not of the correct typeAnnotationConfigurationException- if invalid configuration of@AliasForis detected- 另请参阅:
-
synthesizeAnnotationArray
static Annotation[] synthesizeAnnotationArray(Annotation[] annotations, AnnotatedElement annotatedElement) Synthesize an array of annotations from the supplied array ofannotationsby creating a new array of the same size and type and populating it with synthesized versions of the annotations from the input array.- 参数:
annotations- the array of annotations to synthesizeannotatedElement- the element that is annotated with the supplied array of annotations; may benullif unknown- 返回:
- a new array of synthesized annotations, or
nullif the supplied array isnull - 抛出:
AnnotationConfigurationException- if invalid configuration of@AliasForis detected- 另请参阅:
-
isSynthesizedAnnotation
Determine if the suppliedAnnotationhas been synthesized by Infra (i.e. wrapped in a dynamic proxy) with additional functionality such as attribute alias handling.- 参数:
annotation- the annotation to check- 返回:
trueif the supplied annotation is a synthesized annotation
-
clearCache
public static void clearCache()Clear the internal annotation metadata cache.
-