Interface Types
public interface Types
Compatibility Note: Methods may be added to this interface in future releases of the platform.
- Since:
- 1.6
- See Also:
Method Summary
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<? extends TypeMirror> |
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. |
PrimitiveType |
unboxedType |
Returns the type (a primitive type) of unboxed values of a given type. |
Method Details
asElement
Element asElement(TypeMirror t)
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(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.
- Parameters:
-
t1
- the first type -
t2
- the second type - Returns:
-
true
if and only if the two types are the same
isSubtype
boolean isSubtype(TypeMirror t1, TypeMirror t2)
- 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:
-
IllegalArgumentException
- if given a type for an executable, package, or module - See Java Language Specification:
- 4.10 Subtyping
isAssignable
boolean isAssignable(TypeMirror t1, TypeMirror t2)
- Parameters:
-
t1
- the first type -
t2
- the second type - Returns:
-
true
if and only if the first type is assignable to the second - Throws:
-
IllegalArgumentException
- if given a type for an executable, package, or module - See Java Language Specification:
- 5.2 Assignment Contexts
contains
boolean contains(TypeMirror t1, TypeMirror t2)
- Parameters:
-
t1
- the first type -
t2
- the second type - Returns:
-
true
if and only if the first type contains the second - Throws:
-
IllegalArgumentException
- if given a type for an executable, package, or module - See Java Language Specification:
- 4.5.1 Type Arguments of Parameterized Types
isSubsignature
boolean isSubsignature(ExecutableType m1, ExecutableType m2)
- Parameters:
-
m1
- the first method -
m2
- the second method - Returns:
-
true
if and only if the first signature is a subsignature of the second - See Java Language Specification:
- 8.4.2 Method Signature
directSupertypes
List<? extends TypeMirror> directSupertypes(TypeMirror t)
java.lang.Object
is returned.- Parameters:
-
t
- the type being examined - Returns:
- the direct supertypes, or an empty list if none
- Throws:
-
IllegalArgumentException
- if given a type for an executable, package, or module - See Java Language Specification:
- 4.10 Subtyping
erasure
TypeMirror erasure(TypeMirror t)
- Parameters:
-
t
- the type to be erased - Returns:
- the erasure of a type
- Throws:
-
IllegalArgumentException
- if given a type for a package or module - See Java Language Specification:
- 4.6 Type Erasure
boxedClass
TypeElement boxedClass(PrimitiveType p)
- Parameters:
-
p
- the primitive type to be converted - Returns:
- the class of a boxed value of the primitive type argument
- See Java Language Specification:
- 5.1.7 Boxing Conversion
unboxedType
PrimitiveType unboxedType(TypeMirror t)
- 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 Java Language Specification:
- 5.1.8 Unboxing Conversion
capture
TypeMirror capture(TypeMirror t)
- Parameters:
-
t
- the type to be converted - Returns:
- the result of applying capture conversion
- Throws:
-
IllegalArgumentException
- if given a type for an executable, package, or module - See Java Language Specification:
- 5.1.10 Capture Conversion
getPrimitiveType
PrimitiveType getPrimitiveType(TypeKind kind)
- Parameters:
-
kind
- the kind of primitive type to return - Returns:
- a primitive type
- Throws:
-
IllegalArgumentException
- ifkind
is not a primitive kind
getNullType
NullType getNullType()
null
.- Returns:
- the null type
getNoType
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.
- 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)
- 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)
- Parameters:
-
extendsBound
- the extends (upper) bound, ornull
if none -
superBound
- the super (lower) bound, ornull
if none - Returns:
- a new wildcard type
- Throws:
-
IllegalArgumentException
- if bounds are not valid
getDeclaredType
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>
. 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 a 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)
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, ornull
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:
-
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)
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:
-
IllegalArgumentException
- if the element is not a valid one for the given type
© 1993, 2021, 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/17/docs/api/java.compiler/javax/lang/model/util/Types.html