Class ClassInfo
- java.lang.Object
-
- org.jboss.jandex.ClassInfo
-
- All Implemented Interfaces:
AnnotationTarget
public final class ClassInfo extends java.lang.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ClassInfo.EnclosingMethodInfo
Provides information on the enclosing method or constructor for a local or anonymous class, if available.private static class
ClassInfo.NestingInfo
static class
ClassInfo.NestingType
Describes the form of nesting used by a class-
Nested classes/interfaces inherited from interface org.jboss.jandex.AnnotationTarget
AnnotationTarget.Kind
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<DotName,java.util.List<AnnotationInstance>>
annotations
private static byte[]
EMPTY_POSITIONS
private byte[]
fieldPositions
private FieldInternal[]
fields
private short
flags
private boolean
hasNoArgsConstructor
private Type[]
interfaceTypes
private static int
MAX_POSITIONS
private byte[]
methodPositions
private MethodInternal[]
methods
private static int
MODULE
private DotName
name
private ClassInfo.NestingInfo
nestingInfo
private byte[]
recordComponentPositions
private RecordComponentInternal[]
recordComponents
private Type
superClassType
private Type[]
typeParameters
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.util.Map<DotName,java.util.List<AnnotationInstance>>
annotations()
Returns a map indexed by annotation name, with a value list of annotation instances.ClassInfo
asClass()
Casts and returns this target as aClassInfo
if it is of kindCLASS
FieldInfo
asField()
Casts and returns this target as aFieldInfo
if it is of kindFIELD
MethodInfo
asMethod()
Casts and returns this target as aMethodInfo
if it is of kindMETHOD
MethodParameterInfo
asMethodParameter()
Casts and returns this target as aMethodParameterInfo
if it is of kindMETHOD_PARAMETER
RecordComponentInfo
asRecordComponent()
Casts and returns this target as aRecordComponentInfo
if it is of kindRECORD_COMPONENT
TypeTarget
asType()
Casts and returns this target as aTypeTarget
if it is of kindTYPE
AnnotationInstance
classAnnotation(DotName name)
Returns the annotation with the specified name directly declared on this class.java.util.Collection<AnnotationInstance>
classAnnotations()
Returns a list of all annotations directly declared on this class.java.util.List<AnnotationInstance>
classAnnotationsWithRepeatable(DotName name, IndexView index)
Retrieves annotation instances declared on this class, by the name of the annotation.java.util.List<MethodInfo>
constructors()
Returns a list of all constructors declared in this class (which the JVM names "<init>").(package private) Type[]
copyInterfaceTypes()
static ClassInfo
create(DotName name, DotName superName, short flags, DotName[] interfaces, java.util.Map<DotName,java.util.List<AnnotationInstance>> annotations, boolean hasNoArgsConstructor)
Deprecated.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.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.FieldInfo
field(java.lang.String name)
Retrieves a field by the given name.(package private) FieldInternal[]
fieldArray()
(package private) byte[]
fieldPositionArray()
java.util.List<FieldInfo>
fields()
Returns a list of all available fields.MethodInfo
firstMethod(java.lang.String name)
Retrieves the "first" occurrence of a method by the given name.short
flags()
Returns the access flags for this class.boolean
hasNoArgsConstructor()
Returns a boolean indicating the presence of a no-arg constructor, if supported by the underlying index store.java.util.List<DotName>
interfaceNames()
Returns a list of names for all interfaces this class implements.DotName[]
interfaces()
Deprecated.(package private) Type[]
interfaceTypeArray()
java.util.List<Type>
interfaceTypes()
Returns the list of types in the implements clause of this class.boolean
isAnnotation()
boolean
isEnum()
boolean
isModule()
boolean
isRecord()
boolean
isSynthetic()
AnnotationTarget.Kind
kind()
Returns the kind of object this target represents.MethodInfo
method(java.lang.String name, Type... parameters)
Retrieves a method based on its signature, which includes a method name and an argument list.(package private) MethodInternal[]
methodArray()
(package private) byte[]
methodPositionArray()
java.util.List<MethodInfo>
methods()
Returns a list of all methods declared in this class.ModuleInfo
module()
Returns the module information from this class if it is a module descriptor, i.e.DotName
name()
Returns the name of the class(package private) java.lang.String
nestingSimpleName()
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.RecordComponentInfo
recordComponent(java.lang.String name)
Retrieves a record component by the given name.(package private) RecordComponentInternal[]
recordComponentArray()
(package private) byte[]
recordComponentPositionArray()
java.util.List<RecordComponentInfo>
recordComponents()
Returns a list of all record components declared by this class.(package private) void
setAnnotations(java.util.Map<DotName,java.util.List<AnnotationInstance>> annotations)
(package private) void
setEnclosingMethod(ClassInfo.EnclosingMethodInfo enclosingMethod)
(package private) void
setFieldArray(FieldInternal[] fields)
(package private) void
setFieldPositionArray(byte[] fieldPositions)
(package private) void
setFields(java.util.List<FieldInfo> fields, NameTable names)
(package private) void
setFlags(short flags)
(package private) void
setHasNoArgsConstructor(boolean hasNoArgsConstructor)
(package private) void
setInnerClassInfo(DotName enclosingClass, java.lang.String simpleName, boolean knownInnerClass)
(package private) void
setInterfaceTypes(Type[] interfaceTypes)
(package private) void
setMethodArray(MethodInternal[] methods)
(package private) void
setMethodPositionArray(byte[] methodPositions)
(package private) void
setMethods(java.util.List<MethodInfo> methods, NameTable names)
(package private) void
setModule(ModuleInfo module)
(package private) void
setRecordComponentArray(RecordComponentInternal[] recordComponents)
(package private) void
setRecordComponentPositionArray(byte[] recordComponentPositions)
(package private) void
setRecordComponents(java.util.List<RecordComponentInfo> recordComponents, NameTable names)
(package private) void
setSuperClassType(Type superClassType)
(package private) void
setTypeParameters(Type[] typeParameters)
java.lang.String
simpleName()
Returns the source declared name of this class if it is an inner class, or a local class.(package private) static <T> byte[]
sortAndGetPositions(T[] internals, java.util.Comparator<T> comparator, NameTable names)
Sorts the array of internals using the provided comparator and returns an array of offsets in the original order of internals.Type
superClassType()
Returns a super type represented by the extends clause of this class.DotName
superName()
Returns the name of the super class declared by the extends clause of this class.java.lang.String
toString()
(package private) Type[]
typeParameterArray()
java.util.List<TypeVariable>
typeParameters()
Returns the generic type parameters of this class, if any.java.util.List<FieldInfo>
unsortedFields()
java.util.List<MethodInfo>
unsortedMethods()
java.util.List<RecordComponentInfo>
unsortedRecordComponents()
-
-
-
Field Detail
-
MODULE
private static final int MODULE
- See Also:
- Constant Field Values
-
MAX_POSITIONS
private static final int MAX_POSITIONS
- See Also:
- Constant Field Values
-
EMPTY_POSITIONS
private static final byte[] EMPTY_POSITIONS
-
name
private final DotName name
-
annotations
private java.util.Map<DotName,java.util.List<AnnotationInstance>> annotations
-
flags
private short flags
-
interfaceTypes
private Type[] interfaceTypes
-
superClassType
private Type superClassType
-
typeParameters
private Type[] typeParameters
-
methods
private MethodInternal[] methods
-
fields
private FieldInternal[] fields
-
recordComponents
private RecordComponentInternal[] recordComponents
-
methodPositions
private byte[] methodPositions
-
fieldPositions
private byte[] fieldPositions
-
recordComponentPositions
private byte[] recordComponentPositions
-
hasNoArgsConstructor
private boolean hasNoArgsConstructor
-
nestingInfo
private ClassInfo.NestingInfo nestingInfo
-
-
Method Detail
-
create
@Deprecated public static ClassInfo create(DotName name, DotName superName, short flags, DotName[] interfaces, java.util.Map<DotName,java.util.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:AnnotationTarget
Returns the kind of object this target represents.- Specified by:
kind
in interfaceAnnotationTarget
- Returns:
- the target kind.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
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 standardModifier
can be used to decode the value.- Returns:
- the access flags
-
isSynthetic
public final boolean isSynthetic()
- Returns:
true
if this class is a synthetic class
-
isEnum
public final boolean isEnum()
- Returns:
true
if this class was declared as an enum
-
isAnnotation
public final boolean isAnnotation()
- Returns:
true
if this class object represents an annotation type
-
isRecord
public final boolean isRecord()
- Returns:
true
if this class was declared as a record
-
isModule
public final boolean isModule()
- Returns:
true
if 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 thesuperClassType
method. 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 java.util.Map<DotName,java.util.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
-
setAnnotations
final void setAnnotations(java.util.Map<DotName,java.util.List<AnnotationInstance>> annotations)
-
classAnnotations
public final java.util.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 java.util.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 java.util.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 java.util.List<MethodInfo> unsortedMethods()
-
constructors
public final java.util.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
-
methodArray
final MethodInternal[] methodArray()
-
methodPositionArray
final byte[] methodPositionArray()
-
method
public final MethodInfo method(java.lang.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(java.lang.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(java.lang.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 java.util.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
-
unsortedFields
public final java.util.List<FieldInfo> unsortedFields()
-
fieldArray
final FieldInternal[] fieldArray()
-
fieldPositionArray
final byte[] fieldPositionArray()
-
recordComponent
public final RecordComponentInfo recordComponent(java.lang.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 java.util.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 java.util.List<RecordComponentInfo> unsortedRecordComponents()
-
recordComponentArray
final RecordComponentInternal[] recordComponentArray()
-
recordComponentPositionArray
final byte[] recordComponentPositionArray()
-
interfaceNames
public final java.util.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
Type
instances returned byinterfaceTypes
- Returns:
- the list of names implemented by this class
-
interfaceTypes
public final java.util.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
-
interfaceTypeArray
final Type[] interfaceTypeArray()
-
copyInterfaceTypes
final Type[] copyInterfaceTypes()
-
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 java.util.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
-
typeParameterArray
final Type[] typeParameterArray()
-
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:
true
in case of the Java class has a no-copyParameters constructor,false
if 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 java.lang.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
-
nestingSimpleName
java.lang.String nestingSimpleName()
-
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:AnnotationTarget
Casts and returns this target as aClassInfo
if it is of kindCLASS
- Specified by:
asClass
in interfaceAnnotationTarget
- Returns:
- this instance cast to a class
-
asField
public FieldInfo asField()
Description copied from interface:AnnotationTarget
Casts and returns this target as aFieldInfo
if it is of kindFIELD
- Specified by:
asField
in interfaceAnnotationTarget
- Returns:
- this instance cast to a field
-
asMethod
public MethodInfo asMethod()
Description copied from interface:AnnotationTarget
Casts and returns this target as aMethodInfo
if it is of kindMETHOD
- Specified by:
asMethod
in interfaceAnnotationTarget
- Returns:
- this instance cast to a method
-
asMethodParameter
public MethodParameterInfo asMethodParameter()
Description copied from interface:AnnotationTarget
Casts and returns this target as aMethodParameterInfo
if it is of kindMETHOD_PARAMETER
- Specified by:
asMethodParameter
in interfaceAnnotationTarget
- Returns:
- this instance cast to a method parameter
-
asType
public TypeTarget asType()
Description copied from interface:AnnotationTarget
Casts and returns this target as aTypeTarget
if it is of kindTYPE
- Specified by:
asType
in interfaceAnnotationTarget
- Returns:
- this instance cast to a type target
-
asRecordComponent
public RecordComponentInfo asRecordComponent()
Description copied from interface:AnnotationTarget
Casts and returns this target as aRecordComponentInfo
if it is of kindRECORD_COMPONENT
- Specified by:
asRecordComponent
in interfaceAnnotationTarget
- Returns:
- this instance cast to a record component
-
setHasNoArgsConstructor
void setHasNoArgsConstructor(boolean hasNoArgsConstructor)
-
setFieldArray
void setFieldArray(FieldInternal[] fields)
-
setFieldPositionArray
void setFieldPositionArray(byte[] fieldPositions)
-
setMethodArray
void setMethodArray(MethodInternal[] methods)
-
setMethodPositionArray
void setMethodPositionArray(byte[] methodPositions)
-
setMethods
void setMethods(java.util.List<MethodInfo> methods, NameTable names)
-
setRecordComponentArray
void setRecordComponentArray(RecordComponentInternal[] recordComponents)
-
setRecordComponentPositionArray
void setRecordComponentPositionArray(byte[] recordComponentPositions)
-
setRecordComponents
void setRecordComponents(java.util.List<RecordComponentInfo> recordComponents, NameTable names)
-
sortAndGetPositions
static <T> byte[] sortAndGetPositions(T[] internals, java.util.Comparator<T> comparator, NameTable names)
Sorts the array of internals using the provided comparator and returns an array of offsets in the original order of internals.- Type Parameters:
T
- An internal member type, FieldInternal or MethodInternal- Parameters:
internals
- Array of internal types set on the ClassInfo instancecomparator
- Comparator used to sort internals and locate original positionsnames
- NameTable used to intern byte arrays of member positions- Returns:
- an array offsets in the array of internals in the order prior to sorting
-
setSuperClassType
void setSuperClassType(Type superClassType)
-
setInterfaceTypes
void setInterfaceTypes(Type[] interfaceTypes)
-
setTypeParameters
void setTypeParameters(Type[] typeParameters)
-
setInnerClassInfo
void setInnerClassInfo(DotName enclosingClass, java.lang.String simpleName, boolean knownInnerClass)
-
setEnclosingMethod
void setEnclosingMethod(ClassInfo.EnclosingMethodInfo enclosingMethod)
-
setModule
void setModule(ModuleInfo module)
-
setFlags
void setFlags(short flags)
-
-