-
-
Notifications
You must be signed in to change notification settings - Fork 309
AnnotationInfo API
See also the ClassGraph API overview.
Holds information about one concrete annotation instance, for an annotation on a class, field, method, method parameter, package or module. Obtained by calling .getAllAnnotationInfo() or .getDirectAnnotationInfo() on a ClassInfo, FieldInfo, MethodInfo, MethodParameterInfo, PackageInfo or ModuleInfo object.
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.ignoreClassVisibility()if you want to scan non-public annotation classes.)-
.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. -
.toStringWithSimpleNames()returns a simpler rendering of the annotation thanAnnotationInfo#toString(), by using only the simple name of any referenced classes.
-
-
Parameters:
-
.getParameterValues()returns the parameter values of this annotation, including any parameter values that were not given explicitly at the annotation use site but that have a default value declared by the annotation type, as anAnnotationParameterValueListofAnnotationParameterValueobjects. This is usually the method you want. -
.getDeclaredParameterValues()returns only the parameter values that were given explicitly at the annotation use site, without filling in any defaults. -
.getDefaultParameterValues()returns only the default parameter values declared by the annotation class returned by.getClassInfo(). Default parameter values are declared by the annotation class, so if the annotation class was not scanned, the empty list is returned.
-
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, orString[]for arrays of strings - A boxed type --
Integer,Long,Short,Boolean,Character,Float,DoubleorByte-- for primitive-typed constants - A one-dimensional primitive array (
int[],long[],short[],char[],byte[],boolean[],float[]ordouble[]) for arrays of primitives - A one-dimensional
Object[]array for other array types, where the array element type may be any of the types in this list -
AnnotationEnumValuefor enum constants -
AnnotationClassRefforClass<?>references within annotations -
AnnotationInfofor nested annotations
-
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 the 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. ClassGraph reads the enum class name and the constant name straight from the classfile, so the enum class is never loaded or initialized.
-
.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 constant value (i.e.getClassName() + "." + getValueName()) as aString.
💡 To get the
FieldInfoobjects for the constants of an enum class, without loading it, callClassInfo#getEnumConstants().
Represents a Class<?> reference in an annotation parameter value. Only the name of the referenced class is read from the classfile -- the class itself is never loaded.
-
.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). Returnsnullif the referenced class was not encountered during scanning, e.g. if it was rejected.
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#getAllAnnotationInfo(), then the returned list also excludes annotations inherited from a superclass or implemented interface that was annotated with@java.lang.annotation.Inherited. (Calling.getDirectAnnotationInfo()on the annotated element is shorthand for exactly this.) -
.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.-
.getAsStringsWithSimpleNames()works like.getAsStrings(), but uses only the simple name of any referenced classes.
-
-
.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. -
.getRepeatable(String annotationName | Class<? extends Annotation> annotationClass)returns allAnnotationInfoobjects 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(Predicate<AnnotationInfo> filter)returns anAnnotationInfoListthat is a subset of the original list, obtained by applying the given predicate to eachAnnotationInfoobject in the list, e.g..filter(AnnotationInfo::isInherited).