-
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
Methods Modifier and Type Method and Description Element
asElement(TypeMirror t)
Returns the element corresponding to a type.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.TypeElement
boxedClass(PrimitiveType p)
Returns the class of a boxed value of a given primitive type.TypeMirror
capture(TypeMirror t)
Applies capture conversion to a type.boolean
contains(TypeMirror t1, TypeMirror t2)
Tests whether one type argument contains another.List<? extends TypeMirror>
directSupertypes(TypeMirror t)
Returns the direct supertypes of a type.TypeMirror
erasure(TypeMirror t)
Returns the erasure of a type.ArrayType
getArrayType(TypeMirror componentType)
Returns an array type with the specified component type.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.DeclaredType
getDeclaredType(TypeElement typeElem, TypeMirror... typeArgs)
Returns the type corresponding to a type element and actual type arguments.NoType
getNoType(TypeKind kind)
Returns a pseudo-type used where no actual type is appropriate.NullType
getNullType()
Returns the null type.PrimitiveType
getPrimitiveType(TypeKind kind)
Returns a primitive type.WildcardType
getWildcardType(TypeMirror extendsBound, TypeMirror superBound)
Returns a new wildcard type argument.boolean
isAssignable(TypeMirror t1, TypeMirror t2)
Tests whether one type is assignable to another.boolean
isSameType(TypeMirror t1, TypeMirror t2)
Tests whether twoTypeMirror
objects represent the same type.boolean
isSubsignature(ExecutableType m1, ExecutableType m2)
Tests whether the signature of one method is a subsignature of another.boolean
isSubtype(TypeMirror t1, TypeMirror t2)
Tests whether one type is a subtype of another.PrimitiveType
unboxedType(TypeMirror t)
Returns the type (a primitive type) of unboxed values of a given type.
-
-
-
Method Detail
-
asElement
Element asElement(TypeMirror t)
Returns the element corresponding to a type. The type may be aDeclaredType
orTypeVariable
. Returnsnull
if the type is not one with a corresponding element.- Returns:
- the element corresponding to the given type
-
isSameType
boolean isSameType(TypeMirror t1, TypeMirror t2)
Tests whether twoTypeMirror
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));
- Parameters:
t1
- the first typet2
- the second type- Returns:
true
if and only if the two types are the same
-
isSubtype
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
- the first typet2
- the second type- Returns:
true
if and only if the first type is a subtype of the second- Throws:
IllegalArgumentException
- if given an executable or package type- See The Java™ Language Specification:
- 4.10 Subtyping
-
isAssignable
boolean isAssignable(TypeMirror t1, TypeMirror t2)
Tests whether one type is assignable to another.- Parameters:
t1
- the first typet2
- the second type- Returns:
true
if and only if the first type is assignable to the second- Throws:
IllegalArgumentException
- if given an executable or package type- See The Java™ Language Specification:
- 5.2 Assignment Conversion
-
contains
boolean contains(TypeMirror t1, TypeMirror t2)
Tests whether one type argument contains another.- Parameters:
t1
- the first typet2
- the second type- Returns:
true
if and only if the first type contains the second- Throws:
IllegalArgumentException
- if given an executable or package type- See The Java™ Language Specification:
- 4.5.1.1 Type Argument Containment and Equivalence
-
isSubsignature
boolean isSubsignature(ExecutableType m1, ExecutableType m2)
Tests whether the signature of one method is a subsignature of another.- Parameters:
m1
- the first methodm2
- the second method- Returns:
true
if and only if the first signature is a subsignature of the second- See The Java™ Language Specification:
- 8.4.2 Method Signature
-
directSupertypes
List<? extends TypeMirror> directSupertypes(TypeMirror t)
Returns the direct supertypes of a type. The interface types, if any, will appear last in the list.- Parameters:
t
- the type being examined- Returns:
- the direct supertypes, or an empty list if none
- Throws:
IllegalArgumentException
- if given an executable or package type
-
erasure
TypeMirror erasure(TypeMirror t)
Returns the erasure of a type.- Parameters:
t
- the type to be erased- Returns:
- the erasure of the given type
- Throws:
IllegalArgumentException
- if given a package type- See The Java™ Language Specification:
- 4.6 Type Erasure
-
boxedClass
TypeElement boxedClass(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
- See The Java™ Language Specification:
- 5.1.7 Boxing Conversion
-
unboxedType
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
- the type to be unboxed- Returns:
- the type of an unboxed value of type
t
- Throws:
IllegalArgumentException
- if the given type has no unboxing conversion- See The Java™ Language Specification:
- 5.1.8 Unboxing Conversion
-
capture
TypeMirror capture(TypeMirror t)
Applies capture conversion to a type.- Parameters:
t
- the type to be converted- Returns:
- the result of applying capture conversion
- Throws:
IllegalArgumentException
- if given an executable or package type- See The Java™ Language Specification:
- 5.1.10 Capture Conversion
-
getPrimitiveType
PrimitiveType getPrimitiveType(TypeKind kind)
Returns a primitive type.- Parameters:
kind
- the kind of primitive type to return- Returns:
- a primitive type
- Throws:
IllegalArgumentException
- ifkind
is not a primitive kind
-
getNullType
NullType getNullType()
Returns the null type. This is the type ofnull
.- Returns:
- the null type
-
getNoType
NoType getNoType(TypeKind kind)
Returns a pseudo-type used where no actual type is appropriate. The kind of type to return may be eitherVOID
orNONE
. For packages, useElements.getPackageElement(CharSequence)
.asType()
instead.- Parameters:
kind
- the kind of type to return- Returns:
- a pseudo-type of kind
VOID
orNONE
- Throws:
IllegalArgumentException
- ifkind
is not valid
-
getArrayType
ArrayType getArrayType(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:
IllegalArgumentException
- if the component type is not valid for an array
-
getWildcardType
WildcardType 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.- Parameters:
extendsBound
- the extends (upper) bound, ornull
if nonesuperBound
- the super (lower) bound, ornull
if none- Returns:
- a new wildcard
- Throws:
IllegalArgumentException
- if bounds are not valid
-
getDeclaredType
DeclaredType getDeclaredType(TypeElement typeElem, TypeMirror... typeArgs)
Returns the type corresponding to a type element and actual type arguments. Given the type element forSet
and the type mirror forString
, for example, this method may be used to get the parameterized typeSet<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 typeOuter<String>
, and then invokinggetDeclaredType(DeclaredType, TypeElement, TypeMirror...)
.- Parameters:
typeElem
- the type elementtypeArgs
- the actual type arguments- Returns:
- the type corresponding to the type element and actual type arguments
- Throws:
IllegalArgumentException
- if too many or too few type arguments are given, or if an inappropriate type argument or type element is provided
-
getDeclaredType
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 typeOuter<String>.Inner<Number>
, for example, may be constructed by first usinggetDeclaredType(TypeElement, TypeMirror...)
to get the typeOuter<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 isnull
, this method is equivalent togetDeclaredType(typeElem, typeArgs)
.- Parameters:
containing
- the containing type, ornull
if nonetypeElem
- the type elementtypeArgs
- the actual type arguments- Returns:
- the type corresponding to the type element and actual type arguments, contained within the given type
- Throws:
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
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 typeSet<String>
, theSet.add
method is anExecutableType
whose parameter is of typeString
.- Parameters:
containing
- the containing typeelement
- the element- Returns:
- the type of the element as viewed from the containing type
- Throws:
IllegalArgumentException
- if the element is not a valid one for the given type
-
-
Document created the 11/06/2005, last modified the 04/03/2020
Source of the printed document:https://www.gaudry.be/en/java-api-rf-javax/lang/model/util/Types.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.