public interface Types
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.
IllegalArgumentException from the methods in this interface.| Modifier and Type | Method | Description |
|---|---|---|
Element |
asElement |
Returns the element corresponding to a type. |
TypeMirror |
asMemberOf |
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 |
Returns the class of a boxed value of the primitive type argument. |
TypeMirror |
capture |
Applies capture conversion to a type. |
boolean |
contains |
Tests whether one type argument contains another. |
List |
directSupertypes |
Returns the direct supertypes of a type. |
TypeMirror |
erasure |
Returns the erasure of a type. |
ArrayType |
getArrayType |
Returns an array type with the specified component type. |
DeclaredType |
getDeclaredType |
Returns the type corresponding to a type element and actual type arguments. |
DeclaredType |
getDeclaredType |
Returns the type corresponding to a type element and actual type arguments, given a containing type of which it is a member. |
NoType |
getNoType |
Returns a pseudo-type used where no actual type is appropriate. |
NullType |
getNullType() |
Returns the null type. |
PrimitiveType |
getPrimitiveType |
Returns a primitive type. |
WildcardType |
getWildcardType |
Returns a new wildcard type. |
boolean |
isAssignable |
Tests whether one type is assignable to another. |
boolean |
isSameType |
Tests whether two TypeMirror objects represent the same type. |
boolean |
isSubsignature |
Tests whether the signature of one method is a subsignature of another. |
boolean |
isSubtype |
Tests whether one type is a subtype of another. |
default <T extends TypeMirror> |
stripAnnotations |
Returns a type mirror equivalent to the argument, but with no annotations. |
PrimitiveType |
unboxedType |
Returns the type (a primitive type) of unboxed values of a given type. |
Element asElement(TypeMirror t)
DeclaredType TypeVariable null if the type is not one with a corresponding element. Types without corresponding elements include:
t - the type to map to an elementboolean isSameType(TypeMirror t1, TypeMirror t2)
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)
t1 - the first typet2 - the second typetrue if and only if the first type is a subtype of the secondIllegalArgumentException - if given a type for an executable, package, or moduleboolean isAssignable(TypeMirror t1, TypeMirror t2)
t1 - the first typet2 - the second typetrue if and only if the first type is assignable to the secondIllegalArgumentException - if given a type for an executable, package, or moduleboolean contains(TypeMirror t1, TypeMirror t2)
t1 - the first typet2 - the second typetrue if and only if the first type contains the secondIllegalArgumentException - if given a type for an executable, package, or moduleboolean isSubsignature(ExecutableType m1, ExecutableType m2)
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)
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 supertypes are preserved.t - the type being examinedIllegalArgumentException - if given a type for an executable, package, or moduleTypeMirror erasure(TypeMirror t)
t - the type to be erasedIllegalArgumentException - if given a type for a package or moduleTypeElement boxedClass(PrimitiveType p)
p - the primitive type to be convertedPrimitiveType unboxedType(TypeMirror t)
t - the type to be unboxedt
IllegalArgumentException - if the given type has no unboxing conversion. Only types for the wrapper classes have an unboxing conversion.TypeMirror capture(TypeMirror t)
t - the type to be convertedIllegalArgumentException - if given a type for an executable, package, or modulePrimitiveType getPrimitiveType(TypeKind kind)
kind - the kind of primitive type to returnIllegalArgumentException - if kind is not a primitive kindNullType getNullType()
null.NoType getNoType(TypeKind kind)
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.
kind - the kind of type to returnVOID or NONE
IllegalArgumentException - if kind is not validArrayType getArrayType(TypeMirror componentType)
componentType - the component typeIllegalArgumentException - if the component type is not valid for an array. All valid types are reference types or primitive types. Invalid types include null, executable, package, module, and wildcard types.WildcardType getWildcardType(TypeMirror extendsBound, TypeMirror superBound)
extendsBound - the extends (upper) bound, or null if nonesuperBound - the super (lower) bound, or null if noneIllegalArgumentException - if bounds are not valid. Invalid bounds include all types that are not reference types.DeclaredType getDeclaredType(TypeElement typeElem, TypeMirror... typeArgs)
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...).
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)
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 be equal to 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)
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 typedefault <T extends TypeMirror> T stripAnnotations(T t)
For most kinds of type mirrors, the result of
types.isSameType(typeMirror, types.stripAnnotations(typeMirror))
true. The predicate is false on wildcard types for reasons discussed elsewhere.UnsupportedOperationException.T - the specific type of type mirrort - the type mirror
© 1993, 2025, 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.
https://docs.oracle.com/en/java/javase/25/docs/api/java.compiler/javax/lang/model/util/Types.html