Class Type

  • Direct Known Subclasses:
    ArrayType, ClassType, ParameterizedType, PrimitiveType, TypeVariable, UnresolvedTypeVariable, VoidType, WildcardType

    public abstract class Type
    extends Object
    Represents a Java type declaration 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 Detail

      • EMPTY_ARRAY

        public static final Type[] EMPTY_ARRAY
    • Method Detail

      • create

        public static Type create​(DotName name,
                                  Type.Kind kind)
        Creates a type instance of the specified kind. Types of kind CLASS, directly use the specified name. Types of kind ARRAY parse the name in the Java reflection format (Java descriptor format changing / to '.', e.g. "[[[[Ljava.lang.String;"). Types of kind PRIMITIVE parsed using the primitive descriptor format (e.g. "I" for int). Types of kind VOID ignore the specified name, and return a void type. All other types will throw an exception.
        Parameters:
        name - the name to use or parse
        kind - the kind of type to create
        Returns:
        the type
        Throws:
        IllegalArgumentException - if the kind is no supported
      • name

        public DotName name()
        Returns the raw name of this type. Primitives and void are returned as the Java reserved word (void, boolean, byte, short, char, int, long, float, double). Arrays are returned using the Java reflection array syntax (e.g. "[[[Ljava.lang.String;") Classes are returned as a normal DotName.

        Generic values are returned as the underlying raw value. For example, a wildcard such as ? extends Number, has a raw type of Number

        Returns:
        the name of this type
      • kind

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

        public String toString()
        Returns a string representation for this type. It is similar, yet not equivalent 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, and returns true if they are equivalent. A type is equivalent to another type if it is the same kind, and all of its 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:
        Object.equals(Object)
      • annotations

        public List<AnnotationInstance> annotations()
        Returns the list of annotations declared on this type's usage. In order to allow for type reuse, the annotation instances returned by this method will have a null annotation target value. However, this information is not useful, because if it is accessed from this method, the target is this type.
        Returns:
        a list of annotation instances declared on the usage this type represents
        Since:
        2.0
      • hasAnnotation

        public final boolean hasAnnotation​(DotName name)
      • hashCode

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