Class TypeVariable

  • All Implemented Interfaces:
    Descriptor

    public final class TypeVariable
    extends Type
    Represents a resolved type parameter or type argument. The name() of this type variable corresponds to the raw type name. For type variables, the raw type name is the first declared bound. The identifier() is the name of the type variable as present in the source code.

    For example:

     T extends Number
     
    In this case, the identifier is T, while the name is java.lang.Number.

    Type variables may have bounds. There are three possibilities:

    1. no bounds
    2. one bound, which is a type variable
    3. multiple bounds, where the first may be a class type, and the rest must be interface types
    In the last case, the hasImplicitObjectBound() method may be used to discover whether the first class bound was declared or not.
    Since:
    2.0
    • Field Detail

      • identifier

        private final java.lang.String identifier
      • bounds

        private final Type[] bounds
      • hash

        private int hash
    • Constructor Detail

      • TypeVariable

        TypeVariable​(java.lang.String name)
      • TypeVariable

        TypeVariable​(java.lang.String name,
                     Type[] bounds)
      • TypeVariable

        TypeVariable​(java.lang.String name,
                     Type[] bounds,
                     AnnotationInstance[] annotations,
                     boolean implicitObjectBound)
    • Method Detail

      • create

        public static TypeVariable create​(java.lang.String identifier)
        Create an instance of a type variable with the given identifier. The type variable has a single bound of java.lang.Object.
        Parameters:
        identifier - identifier of the type variable
        Returns:
        the type variable
        Since:
        3.1.0
        See Also:
        identifier()
      • builder

        public static TypeVariable.Builder builder​(java.lang.String identifier)
        Create a builder of a type variable.
        Returns:
        the builder
        Since:
        3.1.0
      • name

        public DotName name()
        Description copied from class: Type
        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 Lbinary.name.of.TheClass; 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).
        Overrides:
        name in class Type
        Returns:
        the name of this type (or its erasure in case of generic types)
      • identifier

        public java.lang.String identifier()
        The identifier of this type variable as it appears in Java source code.

        The following class has a type parameter with an identifier of T:

         class Foo<T extends Number> {
         }
         
        Returns:
        the identifier of this type variable
      • bounds

        public java.util.List<Type> bounds()
      • boundArray

        Type[] boundArray()
      • hasImplicitObjectBound

        public boolean hasImplicitObjectBound()
        Returns whether this type variable has an implicit (undeclared) bound of type Object. If so, all bounds() are interface types.
        Returns:
        whether this type variable has an implicit (undeclared) bound of type Object
      • kind

        public Type.Kind kind()
        Description copied from class: Type
        Returns the kind of Type this is.
        Specified by:
        kind in class Type
        Returns:
        the kind
      • withoutAnnotations

        Type withoutAnnotations()
        Description copied from class: Type
        Returns this type with all type annotations removed. The annotations are removed deeply, that is also on the constituent type in case of arrays, on type arguments in case of parameterized types, on the bound in case of wildcard types, etc.
        Overrides:
        withoutAnnotations in class Type
        Returns:
        this type without type annotations
      • toString

        java.lang.String toString​(boolean simple)
        Overrides:
        toString in class Type
      • equals

        public boolean equals​(java.lang.Object o)
        Description copied from class: Type
        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 Type
        Parameters:
        o - the type to compare to
        Returns:
        true if equal
        See Also:
        Object.equals(Object)
      • hashCode

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

        boolean internEquals​(java.lang.Object o)
        Overrides:
        internEquals in class Type