Interface Types


  • public interface Types
    Utility methods for operating on types.

    Compatibility Note: Methods may be added to this interface in future releases of the platform.

    Since:
    1.6
    See Also:
    ProcessingEnvironment.getTypeUtils()
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      Element asElement​(javax.lang.model.type.TypeMirror t)
      Returns the element corresponding to a type.
      javax.lang.model.type.TypeMirror asMemberOf​(javax.lang.model.type.DeclaredType containing, Element element)
      Returns the type of an element when that element is viewed as a member of, or otherwise directly contained by, a given type.
      TypeElement boxedClass​(javax.lang.model.type.PrimitiveType p)
      Returns the class of a boxed value of a given primitive type.
      javax.lang.model.type.TypeMirror capture​(javax.lang.model.type.TypeMirror t)
      Applies capture conversion to a type.
      boolean contains​(javax.lang.model.type.TypeMirror t1, javax.lang.model.type.TypeMirror t2)
      Tests whether one type argument contains another.
      java.util.List<? extends javax.lang.model.type.TypeMirror> directSupertypes​(javax.lang.model.type.TypeMirror t)
      Returns the direct supertypes of a type.
      javax.lang.model.type.TypeMirror erasure​(javax.lang.model.type.TypeMirror t)
      Returns the erasure of a type.
      javax.lang.model.type.ArrayType getArrayType​(javax.lang.model.type.TypeMirror componentType)
      Returns an array type with the specified component type.
      javax.lang.model.type.DeclaredType getDeclaredType​(TypeElement typeElem, javax.lang.model.type.TypeMirror... typeArgs)
      Returns the type corresponding to a type element and actual type arguments.
      javax.lang.model.type.DeclaredType getDeclaredType​(javax.lang.model.type.DeclaredType containing, TypeElement typeElem, javax.lang.model.type.TypeMirror... typeArgs)
      Returns the type corresponding to a type element and actual type arguments, given a containing type of which it is a member.
      javax.lang.model.type.NoType getNoType​(javax.lang.model.type.TypeKind kind)
      Returns a pseudo-type used where no actual type is appropriate.
      javax.lang.model.type.NullType getNullType()
      Returns the null type.
      javax.lang.model.type.PrimitiveType getPrimitiveType​(javax.lang.model.type.TypeKind kind)
      Returns a primitive type.
      javax.lang.model.type.WildcardType getWildcardType​(javax.lang.model.type.TypeMirror extendsBound, javax.lang.model.type.TypeMirror superBound)
      Returns a new wildcard type argument.
      boolean isAssignable​(javax.lang.model.type.TypeMirror t1, javax.lang.model.type.TypeMirror t2)
      Tests whether one type is assignable to another.
      boolean isSameType​(javax.lang.model.type.TypeMirror t1, javax.lang.model.type.TypeMirror t2)
      Tests whether two TypeMirror objects represent the same type.
      boolean isSubsignature​(javax.lang.model.type.ExecutableType m1, javax.lang.model.type.ExecutableType m2)
      Tests whether the signature of one method is a subsignature of another.
      boolean isSubtype​(javax.lang.model.type.TypeMirror t1, javax.lang.model.type.TypeMirror t2)
      Tests whether one type is a subtype of another.
      javax.lang.model.type.PrimitiveType unboxedType​(javax.lang.model.type.TypeMirror t)
      Returns the type (a primitive type) of unboxed values of a given type.
    • Method Detail

      • asElement

        Element asElement​(javax.lang.model.type.TypeMirror t)
        Returns the element corresponding to a type. The type may be a DeclaredType or TypeVariable. Returns null if the type is not one with a corresponding element.
        Parameters:
        t - the type to map to an element
        Returns:
        the element corresponding to the given type
      • isSameType

        boolean isSameType​(javax.lang.model.type.TypeMirror t1,
                           javax.lang.model.type.TypeMirror t2)
        Tests whether two TypeMirror objects represent the same type.

        Caveat: if either of the arguments to this method represents a wildcard, this method will return false. As a consequence, a wildcard is not the same type as itself. This might be surprising at first, but makes sense once you consider that an example like this must be rejected by the compiler:

           List<?> list = new ArrayList<Object>();
           list.add(list.get(0));
         

        Since annotations are only meta-data associated with a type, the set of annotations on either argument is not taken into account when computing whether or not two TypeMirror objects are the same type. In particular, two TypeMirror objects can have different annotations and still be considered the same.

        Parameters:
        t1 - the first type
        t2 - the second type
        Returns:
        true if and only if the two types are the same
      • isSubtype

        boolean isSubtype​(javax.lang.model.type.TypeMirror t1,
                          javax.lang.model.type.TypeMirror t2)
        Tests whether one type is a subtype of another. Any type is considered to be a subtype of itself.
        Parameters:
        t1 - the first type
        t2 - the second type
        Returns:
        true if and only if the first type is a subtype of the second
        Throws:
        java.lang.IllegalArgumentException - if given a type for an executable, package, or module
      • isAssignable

        boolean isAssignable​(javax.lang.model.type.TypeMirror t1,
                             javax.lang.model.type.TypeMirror t2)
        Tests whether one type is assignable to another.
        Parameters:
        t1 - the first type
        t2 - the second type
        Returns:
        true if and only if the first type is assignable to the second
        Throws:
        java.lang.IllegalArgumentException - if given a type for an executable, package, or module
      • contains

        boolean contains​(javax.lang.model.type.TypeMirror t1,
                         javax.lang.model.type.TypeMirror t2)
        Tests whether one type argument contains another.
        Parameters:
        t1 - the first type
        t2 - the second type
        Returns:
        true if and only if the first type contains the second
        Throws:
        java.lang.IllegalArgumentException - if given a type for an executable, package, or module
      • isSubsignature

        boolean isSubsignature​(javax.lang.model.type.ExecutableType m1,
                               javax.lang.model.type.ExecutableType m2)
        Tests whether the signature of one method is a subsignature of another.
        Parameters:
        m1 - the first method
        m2 - the second method
        Returns:
        true if and only if the first signature is a subsignature of the second
      • directSupertypes

        java.util.List<? extends javax.lang.model.type.TypeMirror> directSupertypes​(javax.lang.model.type.TypeMirror t)
        Returns the direct supertypes of a type. The interface types, if any, will appear last in the list. For an interface type with no direct super-interfaces, a type mirror representing java.lang.Object is returned.
        Parameters:
        t - the type being examined
        Returns:
        the direct supertypes, or an empty list if none
        Throws:
        java.lang.IllegalArgumentException - if given a type for an executable, package, or module
      • erasure

        javax.lang.model.type.TypeMirror erasure​(javax.lang.model.type.TypeMirror t)
        Returns the erasure of a type.
        Parameters:
        t - the type to be erased
        Returns:
        the erasure of the given type
        Throws:
        java.lang.IllegalArgumentException - if given a type for a package or module
      • boxedClass

        TypeElement boxedClass​(javax.lang.model.type.PrimitiveType p)
        Returns the class of a boxed value of a given primitive type. That is, boxing conversion is applied.
        Parameters:
        p - the primitive type to be converted
        Returns:
        the class of a boxed value of type p
      • unboxedType

        javax.lang.model.type.PrimitiveType unboxedType​(javax.lang.model.type.TypeMirror t)
        Returns the type (a primitive type) of unboxed values of a given type. That is, unboxing conversion is applied.
        Parameters:
        t - the type to be unboxed
        Returns:
        the type of an unboxed value of type t
        Throws:
        java.lang.IllegalArgumentException - if the given type has no unboxing conversion
      • capture

        javax.lang.model.type.TypeMirror capture​(javax.lang.model.type.TypeMirror t)
        Applies capture conversion to a type.
        Parameters:
        t - the type to be converted
        Returns:
        the result of applying capture conversion
        Throws:
        java.lang.IllegalArgumentException - if given a type for an executable, package, or module
      • getPrimitiveType

        javax.lang.model.type.PrimitiveType getPrimitiveType​(javax.lang.model.type.TypeKind kind)
        Returns a primitive type.
        Parameters:
        kind - the kind of primitive type to return
        Returns:
        a primitive type
        Throws:
        java.lang.IllegalArgumentException - if kind is not a primitive kind
      • getNullType

        javax.lang.model.type.NullType getNullType()
        Returns the null type. This is the type of null.
        Returns:
        the null type
      • getNoType

        javax.lang.model.type.NoType getNoType​(javax.lang.model.type.TypeKind kind)
        Returns a pseudo-type used where no actual type is appropriate. The kind of type to return may be either VOID or NONE.

        To get the pseudo-type corresponding to a package or module, call asType() on the element modeling the package or module. Names can be converted to elements for packages or modules using Elements.getPackageElement(CharSequence) or Elements.getModuleElement(CharSequence), respectively.

        Parameters:
        kind - the kind of type to return
        Returns:
        a pseudo-type of kind VOID or NONE
        Throws:
        java.lang.IllegalArgumentException - if kind is not valid
      • getArrayType

        javax.lang.model.type.ArrayType getArrayType​(javax.lang.model.type.TypeMirror componentType)
        Returns an array type with the specified component type.
        Parameters:
        componentType - the component type
        Returns:
        an array type with the specified component type.
        Throws:
        java.lang.IllegalArgumentException - if the component type is not valid for an array
      • getWildcardType

        javax.lang.model.type.WildcardType getWildcardType​(javax.lang.model.type.TypeMirror extendsBound,
                                                           javax.lang.model.type.TypeMirror superBound)
        Returns a new wildcard type argument. Either of the wildcard's bounds may be specified, or neither, but not both.
        Parameters:
        extendsBound - the extends (upper) bound, or null if none
        superBound - the super (lower) bound, or null if none
        Returns:
        a new wildcard
        Throws:
        java.lang.IllegalArgumentException - if bounds are not valid
      • getDeclaredType

        javax.lang.model.type.DeclaredType getDeclaredType​(TypeElement typeElem,
                                                           javax.lang.model.type.TypeMirror... typeArgs)
        Returns the type corresponding to a type element and actual type arguments. Given the type element for Set and the type mirror for String, for example, this method may be used to get the parameterized type Set<String>.

        The number of type arguments must either equal the number of the type element's formal type parameters, or must be zero. If zero, and if the type element is generic, then the type element's raw type is returned.

        If a parameterized type is being returned, its type element must not be contained within a generic outer class. The parameterized type Outer<String>.Inner<Number>, for example, may be constructed by first using this method to get the type Outer<String>, and then invoking getDeclaredType(DeclaredType, TypeElement, TypeMirror...).

        Parameters:
        typeElem - the type element
        typeArgs - the actual type arguments
        Returns:
        the type corresponding to the type element and actual type arguments
        Throws:
        java.lang.IllegalArgumentException - if too many or too few type arguments are given, or if an inappropriate type argument or type element is provided
      • getDeclaredType

        javax.lang.model.type.DeclaredType getDeclaredType​(javax.lang.model.type.DeclaredType containing,
                                                           TypeElement typeElem,
                                                           javax.lang.model.type.TypeMirror... typeArgs)
        Returns the type corresponding to a type element and actual type arguments, given a containing type of which it is a member. The parameterized type Outer<String>.Inner<Number>, for example, may be constructed by first using getDeclaredType(TypeElement, TypeMirror...) to get the type Outer<String>, and then invoking this method.

        If the containing type is a parameterized type, the number of type arguments must equal the number of typeElem's formal type parameters. If it is not parameterized or if it is null, this method is equivalent to getDeclaredType(typeElem, typeArgs).

        Parameters:
        containing - the containing type, or null if none
        typeElem - the type element
        typeArgs - the actual type arguments
        Returns:
        the type corresponding to the type element and actual type arguments, contained within the given type
        Throws:
        java.lang.IllegalArgumentException - if too many or too few type arguments are given, or if an inappropriate type argument, type element, or containing type is provided
      • asMemberOf

        javax.lang.model.type.TypeMirror asMemberOf​(javax.lang.model.type.DeclaredType containing,
                                                    Element element)
        Returns the type of an element when that element is viewed as a member of, or otherwise directly contained by, a given type. For example, when viewed as a member of the parameterized type Set<String>, the Set.add method is an ExecutableType whose parameter is of type String.
        Parameters:
        containing - the containing type
        element - the element
        Returns:
        the type of the element as viewed from the containing type
        Throws:
        java.lang.IllegalArgumentException - if the element is not a valid one for the given type