Class 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.
    • Field Detail

      • EMPTY_POSITIONS

        private static final byte[] EMPTY_POSITIONS
      • flags

        private short flags
      • interfaceTypes

        private Type[] interfaceTypes
      • superClassType

        private Type superClassType
      • typeParameters

        private Type[] typeParameters
      • methodPositions

        private byte[] methodPositions
      • fieldPositions

        private byte[] fieldPositions
      • recordComponentPositions

        private byte[] recordComponentPositions
      • hasNoArgsConstructor

        private boolean hasNoArgsConstructor
    • Constructor Detail

      • ClassInfo

        ClassInfo​(DotName name,
                  Type superClassType,
                  short flags,
                  Type[] interfaceTypes)
      • ClassInfo

        ClassInfo​(DotName name,
                  Type superClassType,
                  short flags,
                  Type[] interfaceTypes,
                  boolean hasNoArgsConstructor)
    • 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 class
        superName - the name of the parent class
        flags - the class attributes
        interfaces - the methodParameters this class implements
        annotations - the annotations on this class
        hasNoArgsConstructor - whether this class has a no arg constructor
        Returns:
        a new mock class representation
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.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 standard Modifier 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 the superClassType method. For all classes, with the one exception of java.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, so interfaceNames() 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
      • 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 annotation
        index - 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
      • 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 to java.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 find
        parameters - 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()
      • 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()
      • 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 by interfaceTypes

        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 calling Modifier.isStatic(int) on the return of flags()

        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 a ClassInfo if it is of kind CLASS
        Specified by:
        asClass in interface AnnotationTarget
        Returns:
        this instance cast to a class
      • asField

        public FieldInfo asField()
        Description copied from interface: AnnotationTarget
        Casts and returns this target as a FieldInfo if it is of kind FIELD
        Specified by:
        asField in interface AnnotationTarget
        Returns:
        this instance cast to a field
      • asMethod

        public MethodInfo asMethod()
        Description copied from interface: AnnotationTarget
        Casts and returns this target as a MethodInfo if it is of kind METHOD
        Specified by:
        asMethod in interface AnnotationTarget
        Returns:
        this instance cast to a method
      • asType

        public TypeTarget asType()
        Description copied from interface: AnnotationTarget
        Casts and returns this target as a TypeTarget if it is of kind TYPE
        Specified by:
        asType in interface AnnotationTarget
        Returns:
        this instance cast to a type target
      • setHasNoArgsConstructor

        void setHasNoArgsConstructor​(boolean hasNoArgsConstructor)
      • setFieldPositionArray

        void setFieldPositionArray​(byte[] fieldPositions)
      • setMethodPositionArray

        void setMethodPositionArray​(byte[] methodPositions)
      • setRecordComponentPositionArray

        void setRecordComponentPositionArray​(byte[] recordComponentPositions)
      • 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 instance
        comparator - Comparator used to sort internals and locate original positions
        names - 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)
      • setModule

        void setModule​(ModuleInfo module)
      • setFlags

        void setFlags​(short flags)