CARVIEW |
Navigation Menu
-
-
Notifications
You must be signed in to change notification settings - Fork 294
AnnotationInfo API
See also the ClassGraph API overview.
Holds information about one concrete annotation instance, for an annotation on a class, field, method, or method parameter. Obtained by calling ClassInfo#getAnnotationInfo()
, FieldInfo#getAnnotationInfo()
, MethodInfo#getAnnotationInfo()
, and MethodParameterInfo#getAnnotationInfo()
.
This is the information about a concrete annotation in the sense that it contains information not just about the annotating class, but also the specific annotation parameters, which may or may not override default annotation parameter values associated with the annotation class.
-
Properties: (N.B. call
.enableAnnotationInfo()
before.scan()
to enable annotation scanning, and call.ignoreAnnotationVisibility()
if you want to scan non-public annotations.)-
.getName()
returns the name of the annotation as aString
. -
.getClassInfo()
returns theClassInfo
object for the annotation class. -
.isInherited()
returnstrue
if the annotation is meta-annotated with@Inherited
, which means the annotation is inherited by the subclasses of annotated classes.
-
-
Parameters:
-
.getParameterValues()
returns the parameter values of this annotation (including possibly-overridden default parameter values) as anAnnotationParameterValueList
ofAnnotationParameterValue
objects. -
.getParameterValues(boolean includeDefaults)
returns the parameter values of this annotation (not including any default parameter values, if includeDefaults is false) as anAnnotationParameterValueList
ofAnnotationParameterValue
objects. -
.getDefaultParameterValues()
returns the default parameter values of associated with the annotation class returned by.getClassInfo()
, as anAnnotationParameterValueList
ofAnnotationParameterValue
objects.
-
-
Classloading:
-
.loadClassAndInstantiate()
loads the class returned by.getClassInfo()
and returns anAnnotation
instance that can be cast to the specific annotation type, and that is equivalent to to the specific annotation instance represented by theAnnotationInfo
object.- This is implemented by dynamically instantiating an
InvocationHandler
that emulates the annotation instance, pulling annotation parameters (including annotation default parameter values) from theAnnotationInfo
object. AnInvocationHandler
is needed because annotations can't be directly instantiated without also loading the annotated class.
- This is implemented by dynamically instantiating an
-
Holds an annotation parameter name and value.
-
.getName()
returns the name of the annotation parameter as aString
. -
.getValue()
returns the annotation parameter value as anObject
. May be one of the following types:-
String
for string constants -
Integer
,Long
,Short
,Boolean
,Character
,Float
,Double
orByte
for primitive-typed constants -
AnnotationEnumValue
for enum constants -
AnnotationClassRef
forClass<?>
references within annotations -
AnnotationInfo
for nested annotations - An array, with element type matching one of the types in this list, for array annotation parameter types.
-
Extends ArrayList<AnnotationParameterValue>
with the following convenience methods:
-
.asMap()
returns theAnnotationParameterValueList
as aMap<String, AnnotationParameterValue>
mapping each annotation parameter name to the correspondingAnnotationParameterValue
object. -
.getNames()
returns a list of the names of the annotation parameters in this list, as aList<String>
. -
.getAsStrings()
returns a list of the result of calling.toString()
on eachAnnotationParameterValue
object in this list, producing aList<string>
ofString
representations of each annotation parameter name and value.-
.getAsStringsWithSimpleNames()
works like.getAsStrings()
, but uses only the simple name of any referenced classes, by calling.toStringWithSimpleNames()
on each list element rather than.toString()
.
-
-
.containsName(String parameterName)
returnstrue
if the list contains an annotation parameter of the requested name. -
.get(String parameterName)
returns theAnnotationParameterValue
object in this list with the requested parameter name, ornull
if not found. -
.getValue(String parameterName)
returns value of theAnnotationParameterValue
object in this list with the requested parameter name, by callingAnnotationParameterValue#getValue()
on that object, ornull
if not found.
Represents an enum constant in an annotation parameter value.
-
.getClassName()
returns the name of the enum class as aString
. -
.getValueName()
returns the name of the enum constant value as aString
. -
.getName()
returns the fully-qualified name of the enum const value (i.e.getClassName() + "." + getConstValueName()
) as aString
. -
.loadClassAndReturnConstValue()
loads the enum class, if it has not yet been loaded, instantiates the enum constants, and returns the enum constant represented by this annotation parameter value.🛑 Make sure that you are not trying to load classes after the
ScanResult
goes out of scope orScanResult#close()
is called. TheScanResult
must still exist for classloading to succeed.-
.loadClassAndReturnConstValue(boolean ignoreExceptions)
loads the enum class and returns the enum constant value, as above, but also allows you to ignore classloading exceptions (if there is an exception, it returnsnull
instead).
-
Represents a Class<?>
reference in an annotation parameter value.
-
.getName()
returns the name of the referenced class as aString
. -
.getClassInfo()
returns theClassInfo
object for the referenced class, if the referenced class was encountered during scanning (causing aClassInfo
object to be created for it). May returnnull
if the referenced class was not encountered during scanning (e.g. if the class was rejected). Even if.getClassInfo()
returnsnull
,.loadClass()
may still be able to load the class by name. -
.loadClass()
loads the referenced class, if it has not yet been loaded, and returns aClass<?>
reference for the loaded class.🛑 Make sure that you are not trying to load classes after the
ScanResult
goes out of scope orScanResult#close()
is called. TheScanResult
must still exist for classloading to succeed.-
.loadClass(boolean ignoreExceptions)
loads the referenced class, as above, but also allows you to ignore classloading exceptions (if there is an exception, it returnsnull
instead).
-
Extends ArrayList<AnnotationInfo>
with the following convenience methods:
-
.asMap()
returns theAnnotationInfoList
as aMap<String, AnnotationInfo>
mapping each annotation name to the correspondingAnnotationInfo
object. -
.directOnly()
returns the list of direct annotations, excluding meta-annotations. If thisAnnotationInfoList
consists of class annotations, i.e. if it was produced by callingClassInfo#getAnnotationInfo()
, then the returned list also excludes annotations inherited from a superclass or implemented interface that was meta-annotated with@java.lang.annotation.Inherited
. -
.getNames()
returns a list of the names of the annotations in this list, as aList<String>
. -
.getAsStrings()
returns a list of the result of calling.toString()
on eachAnnotationInfo
object in this list, producing aList<string>
ofString
representations of each annotation, including annotation parameters, etc. -
.containsName(String annotationName)
returnstrue
if this list contains an annotation of the requested name. -
.get(String annotationName)
returns the firstAnnotationInfo
object in this list with the requested name, if present, otherwise returnsnull
. There will only be one annotation in the list with a given name, unless the annotation is a@Repeatable
annotation (i.e. unless another class is meta-annotated with@Repeatable
, with the repeated annotation's class ref as a parameter). -
.getRepeatable(String annotationName | Class<?> annotationClassRef)
returns allAnnotationInfo
objects in this list with the requested annotation name or class, as anAnnotationInfoList
otherwise returns the empty list. The returned list will only contain zero or one elements unless the named annotation is@Repeatable
. -
.filter(AnnotationInfoFilter filter)
returns anAnnotationInfoList
that is a subset of the original list, obtained by applying the given filter predicate to eachAnnotationInfo
object in the list.-
AnnotationInfoFilter
is aFunctionalInterface
with the single abstract methodboolean accept(AnnotationInfo annotationInfo)
.
-