-
-
Notifications
You must be signed in to change notification settings - Fork 298
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 theClassInfoobject for the annotation class. -
.isInherited()returnstrueif 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 anAnnotationParameterValueListofAnnotationParameterValueobjects. -
.getParameterValues(boolean includeDefaults)returns the parameter values of this annotation (not including any default parameter values, if includeDefaults is false) as anAnnotationParameterValueListofAnnotationParameterValueobjects. -
.getDefaultParameterValues()returns the default parameter values of associated with the annotation class returned by.getClassInfo(), as anAnnotationParameterValueListofAnnotationParameterValueobjects.
-
-
Classloading:
-
.loadClassAndInstantiate()loads the class returned by.getClassInfo()and returns anAnnotationinstance that can be cast to the specific annotation type, and that is equivalent to to the specific annotation instance represented by theAnnotationInfoobject.- This is implemented by dynamically instantiating an
InvocationHandlerthat emulates the annotation instance, pulling annotation parameters (including annotation default parameter values) from theAnnotationInfoobject. AnInvocationHandleris 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:-
Stringfor string constants -
Integer,Long,Short,Boolean,Character,Float,DoubleorBytefor primitive-typed constants -
AnnotationEnumValuefor enum constants -
AnnotationClassRefforClass<?>references within annotations -
AnnotationInfofor 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 theAnnotationParameterValueListas aMap<String, AnnotationParameterValue>mapping each annotation parameter name to the correspondingAnnotationParameterValueobject. -
.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 eachAnnotationParameterValueobject in this list, producing aList<string>ofStringrepresentations 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)returnstrueif the list contains an annotation parameter of the requested name. -
.get(String parameterName)returns theAnnotationParameterValueobject in this list with the requested parameter name, ornullif not found. -
.getValue(String parameterName)returns value of theAnnotationParameterValueobject in this list with the requested parameter name, by callingAnnotationParameterValue#getValue()on that object, ornullif 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
ScanResultgoes out of scope orScanResult#close()is called. TheScanResultmust 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 returnsnullinstead).
-
Represents a Class<?> reference in an annotation parameter value.
-
.getName()returns the name of the referenced class as aString. -
.getClassInfo()returns theClassInfoobject for the referenced class, if the referenced class was encountered during scanning (causing aClassInfoobject to be created for it). May returnnullif 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
ScanResultgoes out of scope orScanResult#close()is called. TheScanResultmust 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 returnsnullinstead).
-
Extends ArrayList<AnnotationInfo> with the following convenience methods:
-
.asMap()returns theAnnotationInfoListas aMap<String, AnnotationInfo>mapping each annotation name to the correspondingAnnotationInfoobject. -
.directOnly()returns the list of direct annotations, excluding meta-annotations. If thisAnnotationInfoListconsists 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 eachAnnotationInfoobject in this list, producing aList<string>ofStringrepresentations of each annotation, including annotation parameters, etc. -
.containsName(String annotationName)returnstrueif this list contains an annotation of the requested name. -
.get(String annotationName)returns the firstAnnotationInfoobject 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@Repeatableannotation (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 allAnnotationInfoobjects in this list with the requested annotation name or class, as anAnnotationInfoListotherwise returns the empty list. The returned list will only contain zero or one elements unless the named annotation is@Repeatable. -
.filter(AnnotationInfoFilter filter)returns anAnnotationInfoListthat is a subset of the original list, obtained by applying the given filter predicate to eachAnnotationInfoobject in the list.-
AnnotationInfoFilteris aFunctionalInterfacewith the single abstract methodboolean accept(AnnotationInfo annotationInfo).
-