A
- the annotation typepublic interface MergedAnnotation<A extends Annotation>
MergedAnnotations
collection. Presents a view onto an annotation where attribute values may
have been "merged" from different source values.
Attribute values may be accessed using the various get
methods.
For example, to access an int
attribute the getInt(String)
method would be used.
Note that attribute values are not converted when accessed.
For example, it is not possible to call getString(String)
if the
underlying attribute is an int
. The only exception to this rule is
Class
and Class[]
values which may be accessed as
String
and String[]
respectively to prevent potential early
class initialization.
If necessary, a MergedAnnotation
can be synthesized back into an actual Annotation
.
MergedAnnotations
,
MergedAnnotationPredicates
Modifier and Type | Interface and Description |
---|---|
static class |
MergedAnnotation.Adapt
Adaptations that can be applied to attribute values when creating
Maps or
AnnotationAttributes . |
Modifier and Type | Field and Description |
---|---|
static String |
VALUE
The attribute name for annotations with a single element.
|
Modifier and Type | Method and Description |
---|---|
AnnotationAttributes |
asAnnotationAttributes(MergedAnnotation.Adapt... adaptations)
Create a new mutable
AnnotationAttributes instance from this
merged annotation. |
<T extends Map<String,Object>> |
asMap(Function<MergedAnnotation<?>,T> factory,
MergedAnnotation.Adapt... adaptations)
Create a new
Map instance of the given type that contains all the annotation
attributes. |
Map<String,Object> |
asMap(MergedAnnotation.Adapt... adaptations)
Get an immutable
Map that contains all the annotation attributes. |
MergedAnnotation<A> |
filterAttributes(Predicate<String> predicate)
Create a new view of the annotation with only attributes that match the
given predicate.
|
MergedAnnotation<A> |
filterDefaultValues()
Create a new view of the annotation with all attributes that have default
values removed.
|
static <A extends Annotation> |
from(A annotation)
Create a new
MergedAnnotation instance from the specified
annotation. |
static <A extends Annotation> |
from(Object source,
A annotation)
Create a new
MergedAnnotation instance from the specified
annotation. |
int |
getAggregateIndex()
Get the index of the aggregate collection containing this annotation.
|
<T extends Annotation> |
getAnnotation(String attributeName,
Class<T> type)
Get a required annotation attribute value from the annotation.
|
<T extends Annotation> |
getAnnotationArray(String attributeName,
Class<T> type)
Get a required annotation array attribute value from the annotation.
|
boolean |
getBoolean(String attributeName)
Get a required boolean attribute value from the annotation.
|
boolean[] |
getBooleanArray(String attributeName)
Get a required boolean array attribute value from the annotation.
|
byte |
getByte(String attributeName)
Get a required byte attribute value from the annotation.
|
byte[] |
getByteArray(String attributeName)
Get a required byte array attribute value from the annotation.
|
char |
getChar(String attributeName)
Get a required char attribute value from the annotation.
|
char[] |
getCharArray(String attributeName)
Get a required char array attribute value from the annotation.
|
Class<?> |
getClass(String attributeName)
Get a required class attribute value from the annotation.
|
Class<?>[] |
getClassArray(String attributeName)
Get a required class array attribute value from the annotation.
|
Optional<Object> |
getDefaultValue(String attributeName)
Get the default attribute value from the annotation as specified in
the annotation declaration.
|
<T> Optional<T> |
getDefaultValue(String attributeName,
Class<T> type)
Get the default attribute value from the annotation as specified in
the annotation declaration.
|
int |
getDistance()
Get the distance of this annotation related to its use as a
meta-annotation.
|
double |
getDouble(String attributeName)
Get a required double attribute value from the annotation.
|
double[] |
getDoubleArray(String attributeName)
Get a required double array attribute value from the annotation.
|
<E extends Enum<E>> |
getEnum(String attributeName,
Class<E> type)
Get a required enum attribute value from the annotation.
|
<E extends Enum<E>> |
getEnumArray(String attributeName,
Class<E> type)
Get a required enum array attribute value from the annotation.
|
float |
getFloat(String attributeName)
Get a required float attribute value from the annotation.
|
float[] |
getFloatArray(String attributeName)
Get a required float array attribute value from the annotation.
|
int |
getInt(String attributeName)
Get a required int attribute value from the annotation.
|
int[] |
getIntArray(String attributeName)
Get a required int array attribute value from the annotation.
|
long |
getLong(String attributeName)
Get a required long attribute value from the annotation.
|
long[] |
getLongArray(String attributeName)
Get a required long array attribute value from the annotation.
|
MergedAnnotation<?> |
getMetaSource()
Get the source of the meta-annotation, or
null if the
annotation is not meta-present. |
List<Class<? extends Annotation>> |
getMetaTypes()
Get the complete list of annotation types within the annotation hierarchy
from this annotation to the
root . |
MergedAnnotation<?> |
getRoot()
Get the root annotation, i.e.
|
short |
getShort(String attributeName)
Get a required short attribute value from the annotation.
|
short[] |
getShortArray(String attributeName)
Get a required short array attribute value from the annotation.
|
Object |
getSource()
Get the source that ultimately declared the root annotation, or
null if the source is not known. |
String |
getString(String attributeName)
Get a required string attribute value from the annotation.
|
String[] |
getStringArray(String attributeName)
Get a required string array attribute value from the annotation.
|
Class<A> |
getType()
Get the
Class reference for the actual annotation type. |
Optional<Object> |
getValue(String attributeName)
Get an optional attribute value from the annotation.
|
<T> Optional<T> |
getValue(String attributeName,
Class<T> type)
Get an optional attribute value from the annotation.
|
boolean |
hasDefaultValue(String attributeName)
Determine if the specified attribute name has a default value when compared
to the annotation declaration.
|
boolean |
hasNonDefaultValue(String attributeName)
Determine if the specified attribute name has a non-default value when
compared to the annotation declaration.
|
boolean |
isDirectlyPresent()
Determine if the annotation is directly present on the source.
|
boolean |
isMetaPresent()
Determine if the annotation is meta-present on the source.
|
boolean |
isPresent()
Determine if the annotation is present on the source.
|
static <A extends Annotation> |
missing()
Create a
MergedAnnotation that represents a missing annotation
(i.e. |
static <A extends Annotation> |
of(AnnotatedElement source,
Class<A> annotationType,
Map<String,?> attributes)
Create a new
MergedAnnotation instance of the specified
annotation type with attribute values supplied by a map. |
static <A extends Annotation> |
of(Class<A> annotationType)
Create a new
MergedAnnotation instance of the specified
annotation type. |
static <A extends Annotation> |
of(Class<A> annotationType,
Map<String,?> attributes)
Create a new
MergedAnnotation instance of the specified
annotation type with attribute values supplied by a map. |
static <A extends Annotation> |
of(ClassLoader classLoader,
Object source,
Class<A> annotationType,
Map<String,?> attributes)
Create a new
MergedAnnotation instance of the specified
annotation type with attribute values supplied by a map. |
A |
synthesize()
Create a type-safe synthesized version of this merged annotation that can
be used directly in code.
|
Optional<A> |
synthesize(Predicate<? super MergedAnnotation<A>> condition)
Optionally create a type-safe synthesized version of this annotation based
on a condition predicate.
|
MergedAnnotation<A> |
withNonMergedAttributes()
Create a new view of the annotation that exposes non-merged attribute values.
|
static final String VALUE
Class<A> getType()
Class
reference for the actual annotation type.boolean isPresent()
MergedAnnotations.SearchStrategy
used.true
if the annotation is presentboolean isDirectlyPresent()
A directly present annotation is one that the user has explicitly
declared and not one that is meta-present
or @Inherited
.
true
if the annotation is directly presentboolean isMetaPresent()
A meta-present annotation is an annotation that the user hasn't explicitly declared, but has been used as a meta-annotation somewhere in the annotation hierarchy.
true
if the annotation is meta-presentint getDistance()
A directly declared annotation has a distance of 0
, a
meta-annotation has a distance of 1
, a meta-annotation on a
meta-annotation has a distance of 2
, etc. A missing annotation will always return a distance of -1
.
-1
if the annotation is missingint getAggregateIndex()
Can be used to reorder a stream of annotations, for example, to give a
higher priority to annotations declared on a superclass or interface. A
missing annotation will always return an aggregate
index of -1
.
0
) or -1
if the
annotation is missing@Nullable Object getSource()
null
if the source is not known.
If this merged annotation was created
from
an
AnnotatedElement
then this source will be an element of the same
type. If the annotation was loaded without using reflection, the source
can be of any type, but should have a sensible toString()
.
Meta-annotations will always return the same source as the
root
.
null
@Nullable MergedAnnotation<?> getMetaSource()
null
if the
annotation is not meta-present.
The meta-source is the annotation that was meta-annotated with this annotation.
null
getRoot()
MergedAnnotation<?> getRoot()
distance
0
annotation as directly declared on the source.getMetaSource()
List<Class<? extends Annotation>> getMetaTypes()
root
.
Provides a useful way to uniquely identify a merged annotation instance.
MergedAnnotationPredicates.unique(Function)
,
getRoot()
,
getMetaSource()
boolean hasNonDefaultValue(String attributeName)
attributeName
- the attribute nametrue
if the attribute value is different from the default
valueboolean hasDefaultValue(String attributeName) throws NoSuchElementException
attributeName
- the attribute nametrue
if the attribute value is the same as the default
valueNoSuchElementException
byte getByte(String attributeName) throws NoSuchElementException
attributeName
- the attribute nameNoSuchElementException
- if there is no matching attributebyte[] getByteArray(String attributeName) throws NoSuchElementException
attributeName
- the attribute nameNoSuchElementException
- if there is no matching attributeboolean getBoolean(String attributeName) throws NoSuchElementException
attributeName
- the attribute nameNoSuchElementException
- if there is no matching attributeboolean[] getBooleanArray(String attributeName) throws NoSuchElementException
attributeName
- the attribute nameNoSuchElementException
- if there is no matching attributechar getChar(String attributeName) throws NoSuchElementException
attributeName
- the attribute nameNoSuchElementException
- if there is no matching attributechar[] getCharArray(String attributeName) throws NoSuchElementException
attributeName
- the attribute nameNoSuchElementException
- if there is no matching attributeshort getShort(String attributeName) throws NoSuchElementException
attributeName
- the attribute nameNoSuchElementException
- if there is no matching attributeshort[] getShortArray(String attributeName) throws NoSuchElementException
attributeName
- the attribute nameNoSuchElementException
- if there is no matching attributeint getInt(String attributeName) throws NoSuchElementException
attributeName
- the attribute nameNoSuchElementException
- if there is no matching attributeint[] getIntArray(String attributeName) throws NoSuchElementException
attributeName
- the attribute nameNoSuchElementException
- if there is no matching attributelong getLong(String attributeName) throws NoSuchElementException
attributeName
- the attribute nameNoSuchElementException
- if there is no matching attributelong[] getLongArray(String attributeName) throws NoSuchElementException
attributeName
- the attribute nameNoSuchElementException
- if there is no matching attributedouble getDouble(String attributeName) throws NoSuchElementException
attributeName
- the attribute nameNoSuchElementException
- if there is no matching attributedouble[] getDoubleArray(String attributeName) throws NoSuchElementException
attributeName
- the attribute nameNoSuchElementException
- if there is no matching attributefloat getFloat(String attributeName) throws NoSuchElementException
attributeName
- the attribute nameNoSuchElementException
- if there is no matching attributefloat[] getFloatArray(String attributeName) throws NoSuchElementException
attributeName
- the attribute nameNoSuchElementException
- if there is no matching attributeString getString(String attributeName) throws NoSuchElementException
attributeName
- the attribute nameNoSuchElementException
- if there is no matching attributeString[] getStringArray(String attributeName) throws NoSuchElementException
attributeName
- the attribute nameNoSuchElementException
- if there is no matching attributeClass<?> getClass(String attributeName) throws NoSuchElementException
attributeName
- the attribute nameNoSuchElementException
- if there is no matching attributeClass<?>[] getClassArray(String attributeName) throws NoSuchElementException
attributeName
- the attribute nameNoSuchElementException
- if there is no matching attribute<E extends Enum<E>> E getEnum(String attributeName, Class<E> type) throws NoSuchElementException
attributeName
- the attribute nametype
- the enum typeNoSuchElementException
- if there is no matching attribute<E extends Enum<E>> E[] getEnumArray(String attributeName, Class<E> type) throws NoSuchElementException
attributeName
- the attribute nametype
- the enum typeNoSuchElementException
- if there is no matching attribute<T extends Annotation> MergedAnnotation<T> getAnnotation(String attributeName, Class<T> type) throws NoSuchElementException
attributeName
- the attribute nametype
- the annotation typeMergedAnnotation
NoSuchElementException
- if there is no matching attribute<T extends Annotation> MergedAnnotation<T>[] getAnnotationArray(String attributeName, Class<T> type) throws NoSuchElementException
attributeName
- the attribute nametype
- the annotation typeMergedAnnotation
arrayNoSuchElementException
- if there is no matching attributeOptional<Object> getValue(String attributeName)
attributeName
- the attribute nameOptional.empty()
if there is no
matching attribute<T> Optional<T> getValue(String attributeName, Class<T> type)
attributeName
- the attribute nametype
- the attribute type. Must be compatible with the underlying
attribute type or Object.class
.Optional.empty()
if there is no
matching attributeOptional<Object> getDefaultValue(String attributeName)
attributeName
- the attribute nameOptional.empty()
if
there is no matching attribute or no defined default<T> Optional<T> getDefaultValue(String attributeName, Class<T> type)
attributeName
- the attribute nametype
- the attribute type. Must be compatible with the underlying
attribute type or Object.class
.Optional.empty()
if
there is no matching attribute or no defined defaultMergedAnnotation<A> filterDefaultValues()
filterAttributes(Predicate)
MergedAnnotation<A> filterAttributes(Predicate<String> predicate)
predicate
- a predicate used to filter attribute namesfilterDefaultValues()
,
MergedAnnotationPredicates
MergedAnnotation<A> withNonMergedAttributes()
Methods from this view will return attribute values with only alias mirroring
rules applied. Aliases to meta-source
attributes will
not be applied.
AnnotationAttributes asAnnotationAttributes(MergedAnnotation.Adapt... adaptations)
AnnotationAttributes
instance from this
merged annotation.
The adaptations
may be used to change the way that values
are added.
adaptations
- the adaptations that should be applied to the annotation valuesMap<String,Object> asMap(MergedAnnotation.Adapt... adaptations)
Map
that contains all the annotation attributes.
The adaptations
may be used to change the way that values are added.
adaptations
- the adaptations that should be applied to the annotation values<T extends Map<String,Object>> T asMap(Function<MergedAnnotation<?>,T> factory, MergedAnnotation.Adapt... adaptations)
Map
instance of the given type that contains all the annotation
attributes.
The adaptations
may be used to change the way that values are added.
factory
- a map factoryadaptations
- the adaptations that should be applied to the annotation valuesA synthesize() throws NoSuchElementException
The result is synthesized using a JDK Proxy
and as a result may
incur a computational cost when first invoked.
If this merged annotation was created from an annotation instance, that annotation will be returned unmodified if it is not synthesizable. An annotation is considered synthesizable if one of the following is true.
@AliasFor
.NoSuchElementException
- on a missing annotationOptional<A> synthesize(Predicate<? super MergedAnnotation<A>> condition) throws NoSuchElementException
The result is synthesized using a JDK Proxy
and as a result may
incur a computational cost when first invoked.
Consult the documentation for synthesize()
for an explanation
of what is considered synthesizable.
condition
- the test to determine if the annotation can be synthesizedNoSuchElementException
- on a missing annotationMergedAnnotationPredicates
static <A extends Annotation> MergedAnnotation<A> missing()
MergedAnnotation
that represents a missing annotation
(i.e. one that is not present).static <A extends Annotation> MergedAnnotation<A> from(A annotation)
MergedAnnotation
instance from the specified
annotation.annotation
- the annotation to includeMergedAnnotation
instance containing the annotationstatic <A extends Annotation> MergedAnnotation<A> from(@Nullable Object source, A annotation)
MergedAnnotation
instance from the specified
annotation.source
- the source for the annotation. This source is used only for
information and logging. It does not need to actually contain
the specified annotations, and it will not be searched.annotation
- the annotation to includeMergedAnnotation
instance for the annotationstatic <A extends Annotation> MergedAnnotation<A> of(Class<A> annotationType)
MergedAnnotation
instance of the specified
annotation type. The resulting annotation will not have any attribute
values but may still be used to query default values.annotationType
- the annotation typeMergedAnnotation
instance for the annotationstatic <A extends Annotation> MergedAnnotation<A> of(Class<A> annotationType, @Nullable Map<String,?> attributes)
MergedAnnotation
instance of the specified
annotation type with attribute values supplied by a map.annotationType
- the annotation typeattributes
- the annotation attributes or null
if just default
values should be usedMergedAnnotation
instance for the annotation and attributesof(AnnotatedElement, Class, Map)
static <A extends Annotation> MergedAnnotation<A> of(@Nullable AnnotatedElement source, Class<A> annotationType, @Nullable Map<String,?> attributes)
MergedAnnotation
instance of the specified
annotation type with attribute values supplied by a map.source
- the source for the annotation. This source is used only for
information and logging. It does not need to actually contain
the specified annotations and it will not be searched.annotationType
- the annotation typeattributes
- the annotation attributes or null
if just default
values should be usedMergedAnnotation
instance for the annotation and attributesstatic <A extends Annotation> MergedAnnotation<A> of(@Nullable ClassLoader classLoader, @Nullable Object source, Class<A> annotationType, @Nullable Map<String,?> attributes)
MergedAnnotation
instance of the specified
annotation type with attribute values supplied by a map.classLoader
- the class loader used to resolve class attributessource
- the source for the annotation. This source is used only for
information and logging. It does not need to actually contain
the specified annotations and it will not be searched.annotationType
- the annotation typeattributes
- the annotation attributes or null
if just default
values should be usedMergedAnnotation
instance for the annotation and attributes