public interface Types
Utility methods for operating on types.
Compatibility Note: Methods may be added to this interface in future releases of the platform.
ProcessingEnvironment.getTypeUtils()
Element asElement(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.
t
- the type to map to an elementboolean 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.
t1
- the first typet2
- the second typetrue
if and only if the two types are the sameboolean isSubtype(TypeMirror t1, TypeMirror t2)
Tests whether one type is a subtype of another. Any type is considered to be a subtype of itself.
t1
- the first typet2
- the second typetrue
if and only if the first type is a subtype of the secondIllegalArgumentException
- if given an executable or package typeboolean isAssignable(TypeMirror t1, TypeMirror t2)
Tests whether one type is assignable to another.
t1
- the first typet2
- the second typetrue
if and only if the first type is assignable to the secondIllegalArgumentException
- if given an executable or package typeboolean contains(TypeMirror t1, TypeMirror t2)
Tests whether one type argument contains another.
t1
- the first typet2
- the second typetrue
if and only if the first type contains the secondIllegalArgumentException
- if given an executable or package typeboolean isSubsignature(ExecutableType m1, ExecutableType m2)
Tests whether the signature of one method is a subsignature of another.
m1
- the first methodm2
- the second methodtrue
if and only if the first signature is a subsignature of the secondList<? extends TypeMirror> directSupertypes(TypeMirror t)
Returns the direct supertypes of a type. The interface types, if any, will appear last in the list.
t
- the type being examinedIllegalArgumentException
- if given an executable or package typeTypeMirror erasure(TypeMirror t)
Returns the erasure of a type.
t
- the type to be erasedIllegalArgumentException
- if given a package typeTypeElement boxedClass(PrimitiveType p)
Returns the class of a boxed value of a given primitive type. That is, boxing conversion is applied.
p
- the primitive type to be convertedp
PrimitiveType unboxedType(TypeMirror t)
Returns the type (a primitive type) of unboxed values of a given type. That is, unboxing conversion is applied.
t
- the type to be unboxedt
IllegalArgumentException
- if the given type has no unboxing conversionTypeMirror capture(TypeMirror t)
Applies capture conversion to a type.
t
- the type to be convertedIllegalArgumentException
- if given an executable or package typePrimitiveType getPrimitiveType(TypeKind kind)
Returns a primitive type.
kind
- the kind of primitive type to returnIllegalArgumentException
- if kind
is not a primitive kindNullType getNullType()
Returns the null type. This is the type of null
.
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
. For packages, use Elements.getPackageElement(CharSequence)
.asType()
instead.
kind
- the kind of type to returnVOID
or NONE
IllegalArgumentException
- if kind
is not validArrayType getArrayType(TypeMirror componentType)
Returns an array type with the specified component type.
componentType
- the component typeIllegalArgumentException
- if the component type is not valid for an arrayWildcardType getWildcardType(TypeMirror extendsBound, TypeMirror superBound)
Returns a new wildcard type argument. Either of the wildcard's bounds may be specified, or neither, but not both.
extendsBound
- the extends (upper) bound, or null
if nonesuperBound
- the super (lower) bound, or null
if noneIllegalArgumentException
- if bounds are not validDeclaredType 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>
.
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...)
.
typeElem
- the type elementtypeArgs
- the actual type argumentsIllegalArgumentException
- if too many or too few type arguments are given, or if an inappropriate type argument or type element is providedDeclaredType 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.
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)
.
containing
- the containing type, or null
if nonetypeElem
- the type elementtypeArgs
- the actual type argumentsIllegalArgumentException
- if too many or too few type arguments are given, or if an inappropriate type argument, type element, or containing type is providedTypeMirror 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
.
containing
- the containing typeelement
- the elementIllegalArgumentException
- if the element is not a valid one for the given type
© 1993–2017, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.