Interface AnnotationTarget
-
- All Known Subinterfaces:
ArrayType,ClassInfo,ClassType,DeclarationInfo,FieldInfo,MethodInfo,PackageInfo,ParameterInfo,ParameterizedType,PrimitiveType,RecordComponentInfo,Type,TypeVariable,VoidType,WildcardType
public interface AnnotationTargetAn annotation target is anything that can be annotated. That is:- a declaration, such as a class, a method, a field, etc.
- a type, such as the type of a method parameter, the type of a field, a type argument, a type parameter, present in a declaration of a generic class or method, etc.
hasAnnotation,annotation,repeatableAnnotationandannotationsmethods may be used to obtain information about annotations present on this annotation target. The phrase "present on this annotation target" means: either the annotation is declared or implicitly declared directly on this annotation target, or this annotation target is a class declaration and the annotation is inherited from a superclass.Note that if more than one annotation of a repeatable annotation type is declared on an annotation target, only an implicitly declared container annotation is present on the annotation target; the originally declared annotations are not. If exactly one annotation of a repeatable annotation type is declared on an annotation target, that annotation is present.
Annotations are represented as
AnnotationInfo, so that implementations of this interface are not required to instantiate the annotation type.Implementations of this interface are required to define the
equalsandhashCodemethods. Implementations of this interface are encouraged to define thetoStringmethod such that it returns a text resembling the corresponding Java™ syntax.There is no guarantee that any particular construct, represented by an implementation of this interface, will always be represented by the same object. That includes natural singletons such as the
java.lang.Objectclass declaration, or thevoidpseudo-type. Instances should always be compared usingequals.- Since:
- 4.0
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description <T extends Annotation>
AnnotationInfoannotation(Class<T> annotationType)Returns an annotation of given type, if it is present on this annotation target.Collection<AnnotationInfo>annotations()Returns a collection of all annotations present on this annotation target.Collection<AnnotationInfo>annotations(Predicate<AnnotationInfo> predicate)Returns a collection of all annotations present on this annotation target that match given predicate.DeclarationInfoasDeclaration()Returns this annotation target as a declaration.TypeasType()Returns this annotation target as a type.booleanhasAnnotation(Class<? extends Annotation> annotationType)Returns whether an annotation of given type is present on this annotation target.booleanhasAnnotation(Predicate<AnnotationInfo> predicate)Returns whether given predicate matches any annotation present on this annotation target.booleanisDeclaration()Returns whether this annotation target is a declaration.booleanisType()Returns whether this annotation target is a type.<T extends Annotation>
Collection<AnnotationInfo>repeatableAnnotation(Class<T> annotationType)Returns a collection of annotations of given repeatableannotationTypethat are present on this annotation target.
-
-
-
Method Detail
-
isDeclaration
boolean isDeclaration()
Returns whether this annotation target is a declaration.- Returns:
trueif this is a declaration,falseotherwise
-
isType
boolean isType()
Returns whether this annotation target is a type.- Returns:
trueif this is a type,falseotherwise
-
asDeclaration
DeclarationInfo asDeclaration()
Returns this annotation target as a declaration.- Returns:
- this declaration, never
null - Throws:
IllegalStateException- ifisDeclaration()returnsfalse
-
asType
Type asType()
Returns this annotation target as a type.- Returns:
- this type, never
null - Throws:
IllegalStateException- ifisType()returnsfalse
-
hasAnnotation
boolean hasAnnotation(Class<? extends Annotation> annotationType)
Returns whether an annotation of given type is present on this annotation target.- Parameters:
annotationType- the annotation type, must not benull- Returns:
trueif given annotation type is present on this annotation target,falseotherwise
-
hasAnnotation
boolean hasAnnotation(Predicate<AnnotationInfo> predicate)
Returns whether given predicate matches any annotation present on this annotation target.- Parameters:
predicate- annotation predicate, must not benull- Returns:
trueif given predicate matches any annotation present on this annotation target,falseotherwise.
-
annotation
<T extends Annotation> AnnotationInfo annotation(Class<T> annotationType)
Returns an annotation of given type, if it is present on this annotation target. Returnsnullif no such annotation is present.- Type Parameters:
T- the annotation generic type- Parameters:
annotationType- the annotation type, must not benull- Returns:
- the
AnnotationInfoornullif no such annotation is present
-
repeatableAnnotation
<T extends Annotation> Collection<AnnotationInfo> repeatableAnnotation(Class<T> annotationType)
Returns a collection of annotations of given repeatableannotationTypethat are present on this annotation target. Returns an empty collection if no such annotation is present.For the purpose of this method, annotations in the
valuemember of a container annotation, as defined using@Repeatable, are considered to be present on the annotation target on which the container annotation is present.- Type Parameters:
T- the annotation generic type- Parameters:
annotationType- the@Repeatableannotation type, must not benull- Returns:
- immutable collection of
AnnotationInfo, nevernull
-
annotations
Collection<AnnotationInfo> annotations(Predicate<AnnotationInfo> predicate)
Returns a collection of all annotations present on this annotation target that match given predicate. Returns an empty collection if no such annotation is present.- Parameters:
predicate- annotation predicate, must not benull- Returns:
- immutable collection of
AnnotationInfo, nevernull
-
annotations
Collection<AnnotationInfo> annotations()
Returns a collection of all annotations present on this annotation target. Returns an empty collection if no annotation is present.- Returns:
- immutable collection of
AnnotationInfo, nevernull
-
-