Class ClassInfo
- java.lang.Object
-
- org.jboss.jandex.ClassInfo
-
- All Implemented Interfaces:
AnnotationTarget
public final class ClassInfo extends Object implements AnnotationTarget
Represents a class entry in an index. A ClassInfo is only a partial view of a Java class, it is not intended as a complete replacement for Java reflection. Only the methods and fields which are references by an annotation are stored.Global information including the parent class, implemented methodParameters, and access flags are also provided since this information is often necessary.
Note that a parent class and interface may exist outside of the scope of the index (e.g. classes in a different jar) so the references are stored as names instead of direct references. It is expected that multiple indexes may need to be queried to assemble a full hierarchy in a complex multi-jar environment (e.g. an application server).
Thread-Safety
This class is immutable and can be shared between threads without safe publication.- Author:
- Jason T. Greene
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classClassInfo.EnclosingMethodInfoProvides information on the enclosing method or constructor for a local or anonymous class, if available.static classClassInfo.NestingTypeDescribes the form of nesting used by a class-
Nested classes/interfaces inherited from interface org.jboss.jandex.AnnotationTarget
AnnotationTarget.Kind
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Map<DotName,List<AnnotationInstance>>annotations()Returns a map indexed by annotation name, with a value list of annotation instances.ClassInfoasClass()Casts and returns this target as aClassInfoif it is of kindCLASSFieldInfoasField()Casts and returns this target as aFieldInfoif it is of kindFIELDMethodInfoasMethod()Casts and returns this target as aMethodInfoif it is of kindMETHODMethodParameterInfoasMethodParameter()Casts and returns this target as aMethodParameterInfoif it is of kindMETHOD_PARAMETERRecordComponentInfoasRecordComponent()Casts and returns this target as aRecordComponentInfoif it is of kindRECORD_COMPONENTTypeTargetasType()Casts and returns this target as aTypeTargetif it is of kindTYPEAnnotationInstanceclassAnnotation(DotName name)Returns the annotation with the specified name directly declared on this class.Collection<AnnotationInstance>classAnnotations()Returns a list of all annotations directly declared on this class.List<AnnotationInstance>classAnnotationsWithRepeatable(DotName name, IndexView index)Retrieves annotation instances declared on this class, by the name of the annotation.List<MethodInfo>constructors()Returns a list of all constructors declared in this class (which the JVM names "<init>").static ClassInfocreate(DotName name, DotName superName, short flags, DotName[] interfaces, Map<DotName,List<AnnotationInstance>> annotations, boolean hasNoArgsConstructor)Deprecated.DotNameenclosingClass()Returns the enclosing class if this is an inner class, or null if this is an anonymous, a local, or a top level class.ClassInfo.EnclosingMethodInfoenclosingMethod()Returns the enclosing method of this class if it is a local, or anonymous class, and it is declared within the body of a method or constructor.FieldInfofield(String name)Retrieves a field by the given name.List<FieldInfo>fields()Returns a list of all available fields.MethodInfofirstMethod(String name)Retrieves the "first" occurrence of a method by the given name.shortflags()Returns the access flags for this class.booleanhasNoArgsConstructor()Returns a boolean indicating the presence of a no-arg constructor, if supported by the underlying index store.List<DotName>interfaceNames()Returns a list of names for all interfaces this class implements.DotName[]interfaces()Deprecated.List<Type>interfaceTypes()Returns the list of types in the implements clause of this class.booleanisAnnotation()booleanisEnum()booleanisModule()booleanisRecord()booleanisSynthetic()AnnotationTarget.Kindkind()Returns the kind of object this target represents.MethodInfomethod(String name, Type... parameters)Retrieves a method based on its signature, which includes a method name and an argument list.List<MethodInfo>methods()Returns a list of all methods declared in this class.ModuleInfomodule()Returns the module information from this class if it is a module descriptor, i.e.DotNamename()Returns the name of the classClassInfo.NestingTypenestingType()Returns the nesting type of this class, which could either be a standard top level class, an inner class, an anonymous class, or a local class.RecordComponentInforecordComponent(String name)Retrieves a record component by the given name.List<RecordComponentInfo>recordComponents()Returns a list of all record components declared by this class.StringsimpleName()Returns the source declared name of this class if it is an inner class, or a local class.TypesuperClassType()Returns a super type represented by the extends clause of this class.DotNamesuperName()Returns the name of the super class declared by the extends clause of this class.StringtoString()List<TypeVariable>typeParameters()Returns the generic type parameters of this class, if any.List<FieldInfo>unsortedFields()List<MethodInfo>unsortedMethods()List<RecordComponentInfo>unsortedRecordComponents()
-
-
-
Method Detail
-
create
@Deprecated public static ClassInfo create(DotName name, DotName superName, short flags, DotName[] interfaces, Map<DotName,List<AnnotationInstance>> annotations, boolean hasNoArgsConstructor)
Deprecated.Constructs a "mock" ClassInfo using the passed values. All passed values MUST NOT BE MODIFIED AFTER THIS CALL. Otherwise the resulting object would not conform to the contract outlined above.- Parameters:
name- the name of this classsuperName- the name of the parent classflags- the class attributesinterfaces- the methodParameters this class implementsannotations- the annotations on this classhasNoArgsConstructor- whether this class has a no arg constructor- Returns:
- a new mock class representation
-
kind
public final AnnotationTarget.Kind kind()
Description copied from interface:AnnotationTargetReturns the kind of object this target represents.- Specified by:
kindin interfaceAnnotationTarget- Returns:
- the target kind.
-
name
public final DotName name()
Returns the name of the class- Returns:
- the name of the class
-
flags
public final short flags()
Returns the access flags for this class. The standardModifiercan be used to decode the value.- Returns:
- the access flags
-
isSynthetic
public final boolean isSynthetic()
- Returns:
trueif this class is a synthetic class
-
isEnum
public final boolean isEnum()
- Returns:
trueif this class was declared as an enum
-
isAnnotation
public final boolean isAnnotation()
- Returns:
trueif this class object represents an annotation type
-
isRecord
public final boolean isRecord()
- Returns:
trueif this class was declared as a record
-
isModule
public final boolean isModule()
- Returns:
trueif this class object represents a Java module descriptor
-
superName
public final DotName superName()
Returns the name of the super class declared by the extends clause of this class. This information is also available from thesuperClassTypemethod. For all classes, with the one exception ofjava.lang.Object, which is the one class in the Java language without a super-type, this method will always return a non-null value.- Returns:
- the name of the super class of this class, or null if this class is
java.lang.Object
-
interfaces
@Deprecated public final DotName[] interfaces()
Deprecated.Returns an array of interface names implemented by this class. Every call to this method performs a defensive copy, sointerfaceNames()should be used instead.- Returns:
- an array of interface names implemented by this class
-
annotations
public final Map<DotName,List<AnnotationInstance>> annotations()
Returns a map indexed by annotation name, with a value list of annotation instances. The annotation instances in this map correspond to both annotations on the class, and every nested element of the class (fields, types, methods, etc).The target of the annotation instance can be used to determine the location of the annotation usage.
- Returns:
- the annotations specified on this class and its elements
-
classAnnotations
public final Collection<AnnotationInstance> classAnnotations()
Returns a list of all annotations directly declared on this class.- Returns:
- the list of annotations declared on this class
-
classAnnotation
public final AnnotationInstance classAnnotation(DotName name)
Returns the annotation with the specified name directly declared on this class.- Parameters:
name- the annotation name to look for- Returns:
- the declared annotation or null if not found
-
classAnnotationsWithRepeatable
public final List<AnnotationInstance> classAnnotationsWithRepeatable(DotName name, IndexView index)
Retrieves annotation instances declared on this class, by the name of the annotation. If the specified annotation is repeatable (JLS 9.6), then attempt to result contains the values from the containing annotation.- Parameters:
name- the name of the annotationindex- the index used to obtain the annotation class- Returns:
- the annotation instances declared on this field, or an empty list if none
-
methods
public final List<MethodInfo> methods()
Returns a list of all methods declared in this class. This includes constructors and static initializer blocks which have the special JVM assigned names of "<init>" and "<clinit>", respectively. It does not, however, include inherited methods. These must be discovered by traversing the class hierarchy.This list may be empty, but never null.
- Returns:
- the list of methods declared in this class
-
unsortedMethods
public final List<MethodInfo> unsortedMethods()
-
constructors
public final List<MethodInfo> constructors()
Returns a list of all constructors declared in this class (which the JVM names "<init>"). It does not include inherited methods. These must be discovered by traversing the class hierarchy.This list may never be null.
- Returns:
- the list of constructors declared in this class
-
method
public final MethodInfo method(String name, Type... parameters)
Retrieves a method based on its signature, which includes a method name and an argument list. The argument list is compared based on the underlying raw type of the type arguments. As an example, a generic type parameter "T" is equivalent tojava.lang.Object, since the raw form of a type parameter is its upper bound.Eligible methods include constructors and static initializer blocks which have the special JVM assigned names of "<init>" and "<clinit>", respectively. This does not, however, include inherited methods. These must be discovered by traversing the class hierarchy.
- Parameters:
name- the name of the method to findparameters- the type parameters of the method- Returns:
- the located method or null if not found
-
firstMethod
public final MethodInfo firstMethod(String name)
Retrieves the "first" occurrence of a method by the given name. Note that the order of methods is not defined, and may change in the future. Therefore, this method should not be used when overloading is possible. It's merely intended to provide a handy shortcut for throw away or test code.- Parameters:
name- the name of the method- Returns:
- the first discovered method matching this name, or null if no match is found
-
field
public final FieldInfo field(String name)
Retrieves a field by the given name. Only fields declared in this class are available. Locating inherited fields requires traversing the class hierarchy.- Parameters:
name- the name of the field- Returns:
- the field
-
fields
public final List<FieldInfo> fields()
Returns a list of all available fields. Only fields declared in this class are available. Locating inherited fields requires traversing the class hierarchy. This list may be empty, but never null.- Returns:
- a list of fields
-
recordComponent
public final RecordComponentInfo recordComponent(String name)
Retrieves a record component by the given name.- Parameters:
name- the name of the record component- Returns:
- the record component
-
recordComponents
public final List<RecordComponentInfo> recordComponents()
Returns a list of all record components declared by this class. This list may be empty, but never null.- Returns:
- a list of record components
-
unsortedRecordComponents
public final List<RecordComponentInfo> unsortedRecordComponents()
-
interfaceNames
public final List<DotName> interfaceNames()
Returns a list of names for all interfaces this class implements. This list may be empty, but never null.Note that this information is also available on the
Typeinstances returned byinterfaceTypes- Returns:
- the list of names implemented by this class
-
interfaceTypes
public final List<Type> interfaceTypes()
Returns the list of types in the implements clause of this class. These types may be generic types. This list may be empty, but never null- Returns:
- the list of types declared in the implements clause of this class
-
superClassType
public final Type superClassType()
Returns a super type represented by the extends clause of this class. This type might be a generic type.- Returns:
- the super class type definition in the extends clause
-
typeParameters
public final List<TypeVariable> typeParameters()
Returns the generic type parameters of this class, if any. These will be returned as resolved type variables, so if a parameter has a bound on another parameter, that information will be available.- Returns:
- the generic type parameters of this class
-
hasNoArgsConstructor
public final boolean hasNoArgsConstructor()
Returns a boolean indicating the presence of a no-arg constructor, if supported by the underlying index store. This information is available in indexes produced by Jandex 1.2.0 and later.- Returns:
truein case of the Java class has a no-copyParameters constructor,falseif it does not, or it is not known- Since:
- 1.2.0
-
nestingType
public ClassInfo.NestingType nestingType()
Returns the nesting type of this class, which could either be a standard top level class, an inner class, an anonymous class, or a local class.For historical reasons, static nested classes are returned as
INNER. You can differentiate between a non-static nested class (inner class) and a static nested class by callingModifier.isStatic(int)on the return offlags()- Returns:
- the nesting type of this class
-
simpleName
public String simpleName()
Returns the source declared name of this class if it is an inner class, or a local class. Otherwise this method will return null.- Returns:
- the simple name of a top-level, local, or inner class, or null if this is an anonymous class
-
enclosingClass
public DotName enclosingClass()
Returns the enclosing class if this is an inner class, or null if this is an anonymous, a local, or a top level class.- Returns:
- the enclosing class if this class is an inner class
-
enclosingMethod
public ClassInfo.EnclosingMethodInfo enclosingMethod()
Returns the enclosing method of this class if it is a local, or anonymous class, and it is declared within the body of a method or constructor. It will return null if this class is a top level, or an inner class. It will also return null if the local or anonymous class is on an initializer.- Returns:
- the enclosing method/constructor, if this class is local or anonymous, and it is within a method/constructor
-
module
public ModuleInfo module()
Returns the module information from this class if it is a module descriptor, i.e. module-info.- Returns:
- the module descriptor for module classes, otherwise null
-
asClass
public ClassInfo asClass()
Description copied from interface:AnnotationTargetCasts and returns this target as aClassInfoif it is of kindCLASS- Specified by:
asClassin interfaceAnnotationTarget- Returns:
- this instance cast to a class
-
asField
public FieldInfo asField()
Description copied from interface:AnnotationTargetCasts and returns this target as aFieldInfoif it is of kindFIELD- Specified by:
asFieldin interfaceAnnotationTarget- Returns:
- this instance cast to a field
-
asMethod
public MethodInfo asMethod()
Description copied from interface:AnnotationTargetCasts and returns this target as aMethodInfoif it is of kindMETHOD- Specified by:
asMethodin interfaceAnnotationTarget- Returns:
- this instance cast to a method
-
asMethodParameter
public MethodParameterInfo asMethodParameter()
Description copied from interface:AnnotationTargetCasts and returns this target as aMethodParameterInfoif it is of kindMETHOD_PARAMETER- Specified by:
asMethodParameterin interfaceAnnotationTarget- Returns:
- this instance cast to a method parameter
-
asType
public TypeTarget asType()
Description copied from interface:AnnotationTargetCasts and returns this target as aTypeTargetif it is of kindTYPE- Specified by:
asTypein interfaceAnnotationTarget- Returns:
- this instance cast to a type target
-
asRecordComponent
public RecordComponentInfo asRecordComponent()
Description copied from interface:AnnotationTargetCasts and returns this target as aRecordComponentInfoif it is of kindRECORD_COMPONENT- Specified by:
asRecordComponentin interfaceAnnotationTarget- Returns:
- this instance cast to a record component
-
-