Class Type

java.lang.Object
org.jboss.jandex.Type
Direct Known Subclasses:
ArrayType, ClassType, ParameterizedType, PrimitiveType, TypeVariable, TypeVariableReference, UnresolvedTypeVariable, VoidType, WildcardType

public abstract class Type extends Object
Represents a Java type usage that is specified on methods, fields, classes, annotations, or other types. A type can be any class based type (interface, class, annotation), any primitive, any array, any generic type declaration, or void.

A type usage may have annotations associated with its declaration. A type is equal to another type if, and only if, it represents the same exact definition including the annotations specific to its usage.

To reduce memory overhead, type instances are often shared between their enclosing classes.

Author:
Jason T. Greene
  • Field Details

    • EMPTY_ARRAY

      public static final Type[] EMPTY_ARRAY
  • Method Details

    • create

      public static Type create(DotName name, Type.Kind kind)
      Creates a type instance of the specified kind. If kind is CLASS, the name is used as is. If kind is ARRAY, the name must be in the Java reflection format (Java descriptor format changing / to ., e.g. [[[[Ljava.lang.String;" or [[[I). If kind is PRIMITIVE, the name must be in the Java reflection format (which is equal to the Java keyword denoting the primitive type, e.g. int). If kind is VOID, the name is ignored. All other kinds cause an exception.
      Parameters:
      name - the name of type to use or parse
      kind - the kind of type to create
      Returns:
      the type
      Throws:
      IllegalArgumentException - if the kind is no supported
    • createWithAnnotations

      public static Type createWithAnnotations(DotName name, Type.Kind kind, AnnotationInstance[] annotations)
      Creates an instance of specified type with given type annotations. To create the type instance, this method delegates to create(DotName, Kind).
      Parameters:
      name - the name of type to use or parse
      kind - the kind of type to create
      annotations - the type annotations that should be present on the type instance
      Returns:
      the annotated type
      Throws:
      IllegalArgumentException - if the kind is no supported
    • name

      public DotName name()
      Returns the name of this type (or its erasure in case of generic types) as a DotName, using the Class.getName() format. Specifically:
      • for primitive types and the void pseudo-type, the corresponding Java keyword is returned (void, boolean, byte, short, int, long, float, double, char);
      • for class types, the binary name of the class is returned;
      • for array types, a string is returned that consists of one or more [ characters corresponding to the number of dimensions of the array type, followed by the element type as a single-character code for primitive types or L<binary class name>; for class types (for example, [I for int[] or [[Ljava.lang.String; for String[][]);
      • for parameterized types, the binary name of the generic class is returned (for example, java.util.List for List<String>);
      • for type variables, the name of the first bound of the type variable is returned, or java.lang.Object for type variables that have no bound;
      • for wildcard types, the name of the upper bound is returned, or java.lang.Object if the wildcard type does not have an upper bound (for example, java.lang.Number for ? extends Number).
      Returns:
      the name of this type (or its erasure in case of generic types)
    • kind

      public abstract Type.Kind kind()
      Returns the kind of Type this is.
      Returns:
      the kind
    • asClassType

      public ClassType asClassType()
      Casts this type to a ClassType and returns it if the kind is Type.Kind.CLASS. Throws an exception otherwise.
      Returns:
      a ClassType
      Throws:
      IllegalArgumentException - if not a class
      Since:
      2.0
    • asParameterizedType

      public ParameterizedType asParameterizedType()
      Casts this type to a ParameterizedType and returns it if the kind is Type.Kind.PARAMETERIZED_TYPE. Throws an exception otherwise.
      Returns:
      a ParameterizedType
      Throws:
      IllegalArgumentException - if not a parameterized type
      Since:
      2.0
    • asTypeVariable

      public TypeVariable asTypeVariable()
      Casts this type to a TypeVariable and returns it if the kind is Type.Kind.TYPE_VARIABLE. Throws an exception otherwise.
      Returns:
      a TypeVariable
      Throws:
      IllegalArgumentException - if not a type variable
      Since:
      2.0
    • asTypeVariableReference

      public TypeVariableReference asTypeVariableReference()
      Casts this type to a TypeVariableReference and returns it if the kind is Type.Kind.TYPE_VARIABLE_REFERENCE. Throws an exception otherwise.
      Returns:
      a TypeVariableReference
      Throws:
      IllegalArgumentException - if not a type variable
      Since:
      2.0
    • asArrayType

      public ArrayType asArrayType()
      Casts this type to an ArrayType and returns it if the kind is Type.Kind.ARRAY. Throws an exception otherwise.
      Returns:
      an ArrayType
      Throws:
      IllegalArgumentException - if not an array type
      Since:
      2.0
    • asWildcardType

      public WildcardType asWildcardType()
      Casts this type to a WildcardType and returns it if the kind is Type.Kind.WILDCARD_TYPE. Throws an exception otherwise.
      Returns:
      a WildcardType
      Throws:
      IllegalArgumentException - if not a wildcard type
      Since:
      2.0
    • asUnresolvedTypeVariable

      public UnresolvedTypeVariable asUnresolvedTypeVariable()
      Casts this type to an UnresolvedTypeVariable and returns it if the kind is Type.Kind.UNRESOLVED_TYPE_VARIABLE. Throws an exception otherwise.
      Returns:
      an UnresolvedTypeVariable
      Throws:
      IllegalArgumentException - if not an unresolved type
      Since:
      2.0
    • asPrimitiveType

      public PrimitiveType asPrimitiveType()
      Casts this type to a PrimitiveType and returns it if the kind is Type.Kind.PRIMITIVE. Throws an exception otherwise.
      Returns:
      a PrimitiveType
      Throws:
      IllegalArgumentException - if not a primitive type
      Since:
      2.0
    • asVoidType

      public VoidType asVoidType()
      Casts this type to a VoidType and returns it if the kind is Type.Kind.VOID. Throws an exception otherwise.
      Returns:
      a VoidType
      Throws:
      IllegalArgumentException - if not a void type
      Since:
      2.0
    • hasAnnotation

      public final boolean hasAnnotation(DotName name)
      Returns whether an annotation instance with given name is declared on this type usage.
      Parameters:
      name - name of the annotation type to look for, must not be null
      Returns:
      true if the annotation is present, false otherwise
      See Also:
    • annotation

      public final AnnotationInstance annotation(DotName name)
      Returns the annotation instance with given name declared on this type usage.

      To allow for Type object reuse, the annotation instances returned by this method have a null annotation target.

      Parameters:
      name - name of the annotation type to look for, must not be null
      Returns:
      the annotation instance, or null if not found
    • annotationsWithRepeatable

      public final List<AnnotationInstance> annotationsWithRepeatable(DotName name, IndexView index)
      Returns the annotation instances with given name declared on this type usage.

      If the specified annotation is repeatable, the result also contains all values from the container annotation instance. In this case, the AnnotationInstance.target() returns the target of the container annotation instance.

      To allow for Type object reuse, the annotation instances returned by this method have a null annotation target.

      Parameters:
      name - name of the annotation type, must not be null
      index - index used to obtain the annotation type, must not be null
      Returns:
      immutable list of annotation instances, never null
      Throws:
      IllegalArgumentException - if the index is null, if the index does not contain the annotation type or if name does not identify an annotation type
      Since:
      3.0
      See Also:
    • annotations

      public List<AnnotationInstance> annotations()
      Returns the annotation instances declared on this type usage.

      To allow for Type object reuse, the annotation instances returned by this method have a null annotation target.

      Returns:
      immutable list of annotation instances, never null
      Since:
      2.0
    • toString

      public String toString()
      Returns a string representation for this type. It is similar, yet not identical to a Java source code representation.
      Overrides:
      toString in class Object
      Returns:
      the string representation.
    • equals

      public boolean equals(Object o)
      Compares this Type with another type. A type is equal to another type if it is of the same kind, and all of their fields are equal. This includes annotations, which must be equal as well.
      Overrides:
      equals in class Object
      Parameters:
      o - the type to compare to
      Returns:
      true if equal
      See Also:
    • hashCode

      public int hashCode()
      Computes a hash code representing this type.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code
    • internEquals

      public boolean internEquals(Object o)
    • internHashCode

      public int internHashCode()