Top Description Methods
javax.lang.model.util

public Interface Types

Imports
java.lang.annotation.Annotation, .AnnotationTypeMismatchException, .IncompleteAnnotationException, java.util.List, javax.lang.model.element.*, javax.lang.model.type.*

Utility methods for operating on types. Most methods operate on primitive types, reference types (including array types and the null type), intersection types, and the pseudo-type 'void'. Executable types and the pseudo-types for packages and modules are generally out of scope for these methods. One or more out of scope arguments will typically result in a method throwing an IllegalArgumentException.

Where a method returns a type mirror or a collection of type mirrors, any type mirrors represent types with no type annotations, unless otherwise indicated.

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

Since
1.6
See Also
javax.annotation.processing.ProcessingEnvironment#getTypeUtils

Method Summary

Modifier and TypeMethod and Description
public Element

Returns:

the element corresponding to the given type
asElement
(TypeMirror
the type to map to an element
t
)

Returns the element corresponding to a type.

public TypeMirror

Returns:

the type of the element as viewed from the containing type
asMemberOf
(DeclaredType
the containing type
containing
,
Element
the 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.

public TypeElement

Returns:

the class of a boxed value of the primitive type argument
boxedClass
(PrimitiveType
the primitive type to be converted
p
)

Returns the class of a boxed value of the primitive type argument.

public TypeMirror

Returns:

the result of applying capture conversion
capture
(TypeMirror
the type to be converted
t
)

Applies capture conversion to a type.

public boolean

Returns:

true if and only if the first type contains the second
contains
(TypeMirror
the first type
t1
,
TypeMirror
the second type
t2
)

Tests whether one type argument contains another.

public List<? extends TypeMirror>

Returns:

the direct supertypes, or an empty list if none
directSupertypes
(TypeMirror
the type being examined
t
)

Returns the direct supertypes of a type.

public TypeMirror

Returns:

the erasure of a type
erasure
(TypeMirror
the type to be erased
t
)

Returns the erasure of a type.

public ArrayType

Returns:

an array type with the specified component type
getArrayType
(TypeMirror
the component type
componentType
)

Returns an array type with the specified component type.

public DeclaredType

Returns:

the type corresponding to a type element and actual type arguments
getDeclaredType
(TypeElement
the type element
typeElem
,
TypeMirror...
the actual type arguments
typeArgs
)

Returns the type corresponding to a type element and actual type arguments.

public DeclaredType

Returns:

the type corresponding to the type element and actual type arguments, contained within the given type
getDeclaredType
(DeclaredType
the containing type, or null if none
containing
,
TypeElement
the type element
typeElem
,
TypeMirror...
the actual type arguments
typeArgs
)

Returns the type corresponding to a type element and actual type arguments, given a containing type of which it is a member.

public NoType

Returns:

a pseudo-type of kind VOID or NONE
getNoType
(TypeKind
the kind of type to return
kind
)

Returns a pseudo-type used where no actual type is appropriate.

public NullType

Returns:

the null type
getNullType
()

Returns the null type.

public PrimitiveType

Returns:

a primitive type
getPrimitiveType
(TypeKind
the kind of primitive type to return
kind
)

Returns a primitive type.

public WildcardType

Returns:

a new wildcard type
getWildcardType
(TypeMirror
the extends (upper) bound, or null if none
extendsBound
,
TypeMirror
the super (lower) bound, or null if none
superBound
)

Returns a new wildcard type.

public boolean

Returns:

true if and only if the first type is assignable to the second
isAssignable
(TypeMirror
the first type
t1
,
TypeMirror
the second type
t2
)

Tests whether one type is assignable to another.

public boolean

Returns:

true if and only if the two types are the same
isSameType
(TypeMirror
the first type
t1
,
TypeMirror
the second type
t2
)

Tests whether two TypeMirror objects represent the same type.

public boolean

Returns:

true if and only if the first signature is a subsignature of the second
isSubsignature
(ExecutableType
the first method
m1
,
ExecutableType
the second method
m2
)

Tests whether the signature of one method is a subsignature of another.

public boolean

Returns:

true if and only if the first type is a subtype of the second
isSubtype
(TypeMirror
the first type
t1
,
TypeMirror
the second type
t2
)

Tests whether one type is a subtype of another.

public default <
the specific type of type mirror
T extends TypeMirror
>
T

Returns:

a type mirror equivalent to the argument, but with no annotations
stripAnnotations
(T
the type mirror
t
)

Returns a type mirror equivalent to the argument, but with no annotations.

public PrimitiveType

Returns:

the type of an unboxed value of type t
unboxedType
(TypeMirror
the type to be unboxed
t
)

Returns the type (a primitive type) of unboxed values of a given type.

Method Detail

asElementback to summary
public Element asElement(TypeMirror t)

Returns the element corresponding to a type. The type may be one of:

The method returns null if the type is not one with a corresponding element. Types without corresponding elements include:
Parameters
t:TypeMirror

the type to map to an element

Returns:Element

the element corresponding to the given type

asMemberOfback to summary
public TypeMirror asMemberOf(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:DeclaredType

the containing type

element:Element

the element

Returns:TypeMirror

the type of the element as viewed from the containing type

Exceptions
IllegalArgumentException:
if the element is not a valid one for the given type
boxedClassback to summary
public TypeElement boxedClass(PrimitiveType p)

Returns the class of a boxed value of the primitive type argument. That is, boxing conversion is applied.

Parameters
p:PrimitiveType

the primitive type to be converted

Returns:TypeElement

the class of a boxed value of the primitive type argument

Java Language Specification
5.1.7 Boxing Conversion
captureback to summary
public TypeMirror capture(TypeMirror t)

Applies capture conversion to a type.

Parameters
t:TypeMirror

the type to be converted

Returns:TypeMirror

the result of applying capture conversion

Exceptions
IllegalArgumentException:
if given a type for an executable, package, or module
Java Language Specification
5.1.10 Capture Conversion
containsback to summary
public boolean contains(TypeMirror t1, TypeMirror t2)

Tests whether one type argument contains another.

Parameters
t1:TypeMirror

the first type

t2:TypeMirror

the second type

Returns:boolean

true if and only if the first type contains the second

Exceptions
IllegalArgumentException:
if given a type for an executable, package, or module
Java Language Specification
4.5.1 Type Arguments of Parameterized Types
directSupertypesback to summary
public List<? extends TypeMirror> directSupertypes(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. The type java.lang.Object has no direct supertype (JLS 8.1.4, 8.1.5) so an empty list is returned for the direct supertypes of a type mirror representing java.lang.Object. Annotations on the direct super types are preserved.

Parameters
t:TypeMirror

the type being examined

Returns:List<? extends TypeMirror>

the direct supertypes, or an empty list if none

Exceptions
IllegalArgumentException:
if given a type for an executable, package, or module
Java Language Specification
4.10 Subtyping
erasureback to summary
public TypeMirror erasure(TypeMirror t)

Returns the erasure of a type.

Parameters
t:TypeMirror

the type to be erased

Returns:TypeMirror

the erasure of a type

Exceptions
IllegalArgumentException:
if given a type for a package or module
Java Language Specification
4.6 Type Erasure
getArrayTypeback to summary
public ArrayType getArrayType(TypeMirror componentType)

Returns an array type with the specified component type. Annotations on the component type are preserved.

Parameters
componentType:TypeMirror

the component type

Returns:ArrayType

an array type with the specified component type

Exceptions
IllegalArgumentException:
if the component type is not valid for an array, including executable, package, module, and wildcard types
Java Language Specification
10.1 Array Types
getDeclaredTypeback to summary
public DeclaredType getDeclaredType(TypeElement typeElem, 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>. Annotations on the type arguments are preserved.

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:TypeElement

the type element

typeArgs:TypeMirror[]

the actual type arguments

Returns:DeclaredType

the type corresponding to a type element and actual type arguments

Exceptions
IllegalArgumentException:
if too many or too few type arguments are given, or if an inappropriate type argument or type element is provided
getDeclaredTypeback to summary
public DeclaredType getDeclaredType(DeclaredType containing, TypeElement typeElem, 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. Annotations on the type arguments are preserved.

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:DeclaredType

the containing type, or null if none

typeElem:TypeElement

the type element

typeArgs:TypeMirror[]

the actual type arguments

Returns:DeclaredType

the type corresponding to the type element and actual type arguments, contained within the given type

Exceptions
IllegalArgumentException:
if too many or too few type arguments are given, or if an inappropriate type argument, type element, or containing type is provided
getNoTypeback to summary
public NoType getNoType(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:TypeKind

the kind of type to return

Returns:NoType

a pseudo-type of kind VOID or NONE

Exceptions
IllegalArgumentException:
if kind is not valid
getNullTypeback to summary
public NullType getNullType()

Returns the null type. This is the type of null.

Returns:NullType

the null type

Java Language Specification
4.1 The Kinds of Types and Values
getPrimitiveTypeback to summary
public PrimitiveType getPrimitiveType(TypeKind kind)

Returns a primitive type.

Parameters
kind:TypeKind

the kind of primitive type to return

Returns:PrimitiveType

a primitive type

Exceptions
IllegalArgumentException:
if kind is not a primitive kind
Java Language Specification
4.2 Primitive Types and Values
getWildcardTypeback to summary
public WildcardType getWildcardType(TypeMirror extendsBound, TypeMirror superBound)

Returns a new wildcard type. Either of the wildcard's bounds may be specified, or neither, but not both. Annotations on the bounds are preserved.

Parameters
extendsBound:TypeMirror

the extends (upper) bound, or null if none

superBound:TypeMirror

the super (lower) bound, or null if none

Returns:WildcardType

a new wildcard type

Exceptions
IllegalArgumentException:
if bounds are not valid
Java Language Specification
4.5.1 Type Arguments of Parameterized Types
isAssignableback to summary
public boolean isAssignable(TypeMirror t1, TypeMirror t2)

Tests whether one type is assignable to another.

Parameters
t1:TypeMirror

the first type

t2:TypeMirror

the second type

Returns:boolean

true if and only if the first type is assignable to the second

Exceptions
IllegalArgumentException:
if given a type for an executable, package, or module
Java Language Specification
5.2 Assignment Contexts
isSameTypeback to summary
public boolean isSameType(TypeMirror t1, 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:TypeMirror

the first type

t2:TypeMirror

the second type

Returns:boolean

true if and only if the two types are the same

isSubsignatureback to summary
public boolean isSubsignature(ExecutableType m1, ExecutableType m2)

Tests whether the signature of one method is a subsignature of another.

Parameters
m1:ExecutableType

the first method

m2:ExecutableType

the second method

Returns:boolean

true if and only if the first signature is a subsignature of the second

Java Language Specification
8.4.2 Method Signature
isSubtypeback to summary
public boolean isSubtype(TypeMirror t1, TypeMirror t2)

Tests whether one type is a subtype of another. Any type is considered to be a subtype of itself.

Parameters
t1:TypeMirror

the first type

t2:TypeMirror

the second type

Returns:boolean

true if and only if the first type is a subtype of the second

Exceptions
IllegalArgumentException:
if given a type for an executable, package, or module
Java Language Specification
4.10 Subtyping
stripAnnotationsback to summary
public default <T extends TypeMirror> T stripAnnotations(T t)

Returns a type mirror equivalent to the argument, but with no annotations. If the type mirror is a composite type, such as an array type or a wildcard type, any constitute types, such as the component type of an array and the type of the bounds of a wildcard type, also have no annotations, recursively.

For most kinds of type mirrors, the result of

types.isSameType(typeMirror, types.stripAnnotations(typeMirror))
types.isSameType(typeMirror, types.stripAnnotations(typeMirror))
is true. The predicate is false on wildcard types for reasons discussed elsewhere.

Implementation Specification

The default implementation throws UnsupportedOperationException.

Parameters
<T>
the specific type of type mirror
t:T

the type mirror

Returns:T

a type mirror equivalent to the argument, but with no annotations

Since
23
unboxedTypeback to summary
public PrimitiveType unboxedType(TypeMirror t)

Returns the type (a primitive type) of unboxed values of a given type. That is, unboxing conversion is applied.

Parameters
t:TypeMirror

the type to be unboxed

Returns:PrimitiveType

the type of an unboxed value of type t

Exceptions
IllegalArgumentException:
if the given type has no unboxing conversion
Java Language Specification
5.1.8 Unboxing Conversion