public interface AnnotationSource
A source of annotations. This API provides an alternative to Java's AnnotatedElement
that uses the compile time produced data
from Micronaut. This is the parent interface of the AnnotationMetadata
which provides event more methods to read annotations values and compute Repeatable
annotations.
Note that this interface also includes methods such as synthesize(Class)
that allows materializing an instance of an annotation by producing a runtime proxy. These methods are a last resort if no other option is possible and should generally be avoided as they require the use of runtime reflection and proxying which hurts performance and memory consumption.
AnnotationMetadata
Modifier and Type | Field and Description |
---|---|
static AnnotationSource |
EMPTY
An empty annotation source.
|
Modifier and Type | Method and Description |
---|---|
default <T extends Annotation> |
findAnnotation(Class<T> annotationClass)
Find an
AnnotationValue for the given annotation type. |
default <T extends Annotation> |
findAnnotation(String annotation)
Find an
AnnotationValue for the given annotation name. |
default <T extends Annotation> |
findDeclaredAnnotation(Class<T> annotationClass)
Get all of the values for the given annotation that are directly declared on the annotated element.
|
default <T extends Annotation> |
findDeclaredAnnotation(String annotation)
Get all of the values for the given annotation that are directly declared on the annotated element.
|
default <T extends Annotation> |
getAnnotation(Class<T> annotationClass)
Find an
AnnotationValue for the given annotation name. |
default <T extends Annotation> |
getAnnotation(String annotation)
Find an
AnnotationValue for the given annotation name. |
default <T extends Annotation> |
getDeclaredAnnotation(Class<T> annotationClass)
Find an
AnnotationValue for the given annotation name. |
default <T extends Annotation> |
getDeclaredAnnotation(String annotation)
Get all of the values for the given annotation that are directly declared on the annotated element.
|
default boolean |
isAnnotationPresent(Class<? extends Annotation> annotationClass)
Return whether an annotation is present.
|
default boolean |
isAnnotationPresent(String annotationName)
Return whether an annotation is present.
|
default boolean |
isDeclaredAnnotationPresent(Class<? extends Annotation> annotationClass)
Variation of
isAnnotationPresent(Class) for declared annotations. |
default boolean |
isDeclaredAnnotationPresent(String annotationName)
Variation of
isAnnotationPresent(String) for declared annotations. |
default <T extends Annotation> |
synthesize(Class<T> annotationClass)
Synthesizes a new annotation from the metadata for the given annotation type.
|
default <T extends Annotation> |
synthesize(Class<T> annotationClass,
String sourceAnnotation)
Synthesizes a new annotation for the given annotation type using the member values of the given source annotation.
|
default Annotation[] |
synthesizeAll()
Synthesizes a new annotations from the metadata.
|
default <T extends Annotation> |
synthesizeAnnotationsByType(Class<T> annotationClass)
Synthesizes a new annotations from the metadata for the given type.
|
default Annotation[] |
synthesizeDeclared()
Synthesizes a new annotations from the metadata.
|
default <T extends Annotation> |
synthesizeDeclared(Class<T> annotationClass)
Synthesizes a new annotation from the metadata for the given annotation type.
|
default <T extends Annotation> |
synthesizeDeclared(Class<T> annotationClass,
String sourceAnnotation)
Synthesizes a new annotation declared for the given annotation type using the member values of the given source annotation.
|
default <T extends Annotation> |
synthesizeDeclaredAnnotationsByType(Class<T> annotationClass)
Synthesizes a new annotations from the metadata for the given type.
|
static final AnnotationSource EMPTY
@Nullable default <T extends Annotation> T synthesize(@NonNull Class<T> annotationClass)
T
- The annotation generic typeannotationClass
- The annotation class@Nullable default <T extends Annotation> T synthesize(@NonNull Class<T> annotationClass, @NonNull String sourceAnnotation)
This method allows supporting synthesizing annotations that have been renamed, for example a jakarta.inject.Named
annotation an be synthesized from the metadata of the a javax.inject.Named
annotation.
T
- The annotation generic typeannotationClass
- The annotation classsourceAnnotation
- The source annotation that provides the member values@Nullable default <T extends Annotation> T synthesizeDeclared(@NonNull Class<T> annotationClass, @NonNull String sourceAnnotation)
This method allows supporting synthesizing annotations that have been renamed, for example a jakarta.inject.Named
annotation an be synthesized from the metadata of the a javax.inject.Named
annotation.
T
- The annotation generic typeannotationClass
- The annotation classsourceAnnotation
- The source annotation that provides the member values@Nullable default <T extends Annotation> T synthesizeDeclared(@NonNull Class<T> annotationClass)
This method ignores inherited annotations. (Returns null if no annotations are directly present on this element.)
T
- The annotation generic typeannotationClass
- The annotation class@NonNull default Annotation[] synthesizeAll()
@NonNull default Annotation[] synthesizeDeclared()
@NonNull default <T extends Annotation> T[] synthesizeAnnotationsByType(@NonNull Class<T> annotationClass)
T
- The annotation generic typeannotationClass
- The annotation type@NonNull default <T extends Annotation> T[] synthesizeDeclaredAnnotationsByType(@NonNull Class<T> annotationClass)
T
- The annotation generic typeannotationClass
- The annotation type@NonNull default <T extends Annotation> Optional<AnnotationValue<T>> findAnnotation(@NonNull String annotation)
AnnotationValue
for the given annotation name.T
- The annotation typeannotation
- The annotation nameAnnotationValue
instance@NonNull default <T extends Annotation> Optional<AnnotationValue<T>> findAnnotation(@NonNull Class<T> annotationClass)
AnnotationValue
for the given annotation type.T
- The annotation typeannotationClass
- The annotationAnnotationValue
instance@NonNull default <T extends Annotation> Optional<AnnotationValue<T>> findDeclaredAnnotation(@NonNull String annotation)
T
- The annotation typeannotation
- The annotation nameAnnotationValue
instance@NonNull default <T extends Annotation> Optional<AnnotationValue<T>> findDeclaredAnnotation(@NonNull Class<T> annotationClass)
T
- The annotation typeannotationClass
- The annotation nameAnnotationValue
instance@Nullable default <T extends Annotation> AnnotationValue<T> getAnnotation(@NonNull String annotation)
AnnotationValue
for the given annotation name.T
- The annotation typeannotation
- The annotation nameAnnotationValue
instance or null@Nullable default <T extends Annotation> AnnotationValue<T> getAnnotation(@NonNull Class<T> annotationClass)
AnnotationValue
for the given annotation name.T
- The annotation typeannotationClass
- The annotation nameAnnotationValue
instance or null@Nullable default <T extends Annotation> AnnotationValue<T> getDeclaredAnnotation(@NonNull String annotation)
T
- The annotation typeannotation
- The annotation nameAnnotationValue
instance@Nullable default <T extends Annotation> AnnotationValue<T> getDeclaredAnnotation(@NonNull Class<T> annotationClass)
AnnotationValue
for the given annotation name.T
- The annotation typeannotationClass
- The annotation nameAnnotationValue
instance or nulldefault boolean isAnnotationPresent(@NonNull Class<? extends Annotation> annotationClass)
annotationClass
- The annotation classdefault boolean isDeclaredAnnotationPresent(@NonNull Class<? extends Annotation> annotationClass)
isAnnotationPresent(Class)
for declared annotations.annotationClass
- The annotation classdefault boolean isAnnotationPresent(@NonNull String annotationName)
annotationName
- The annotation namedefault boolean isDeclaredAnnotationPresent(@NonNull String annotationName)
isAnnotationPresent(String)
for declared annotations.annotationName
- The annotation name