public final class ReflectionTypes extends AbstractTypes
Types
backed by the Java Reflection API.
All Element
and TypeMirror
objects returned by this class are immutable and therefore thread-safe.
Likewise, only a stateless (and thus thread-safe) singleton instance of this class is available via
getInstance()
.
Currently unsupported are (resulting in an UnsupportedOperationException
):
typeMirror(Type)
for details.
directSupertypes(TypeMirror)
asMemberOf(DeclaredType, Element)
isAssignable(TypeMirror, TypeMirror)
isSubsignature(ExecutableType, ExecutableType)
Modifier and Type | Method and Description |
---|---|
TypeMirror |
asMemberOf(DeclaredType containing,
Element element) |
TypeElement |
boxedClass(PrimitiveType primitiveType) |
protected WildcardType |
capturedTypeArgument(TypeVariable typeVariable)
Returns the captured wildcard type argument of the given type variable, or null if the given type variable is not
the result of a capture conversion.
|
protected TypeVariable |
createTypeVariable(TypeParameterElement typeParameter,
WildcardType capturedTypeArgument)
Creates a new unfinished type variable for the given formal parameter.
|
List<? extends TypeMirror> |
directSupertypes(TypeMirror t) |
ArrayType |
getArrayType(TypeMirror componentType) |
DeclaredType |
getDeclaredType(DeclaredType containing,
TypeElement typeElem,
TypeMirror... typeArgs) |
DeclaredType |
getDeclaredType(TypeElement typeElem,
TypeMirror... typeArgs) |
static ReflectionTypes |
getInstance()
Returns the singleton instance of this class.
|
IntersectionType |
getIntersectionType(TypeMirror... bounds)
Returns a new intersection type.
|
NoType |
getNoType(TypeKind kind) |
NullType |
getNullType() |
PrimitiveType |
getPrimitiveType(TypeKind kind) |
WildcardType |
getWildcardType(TypeMirror extendsBound,
TypeMirror superBound) |
boolean |
isAssignable(TypeMirror t1,
TypeMirror t2) |
boolean |
isSubsignature(ExecutableType m1,
ExecutableType m2) |
protected void |
requireValidElement(Element element)
Verifies that the given
Element is valid for use with this class. |
protected void |
requireValidType(TypeMirror type)
Verifies that the given
TypeMirror is valid for use with this class, or that it is null . |
protected void |
setTypeVariableBounds(TypeVariable typeVariable,
TypeMirror upperBound,
TypeMirror lowerBound)
Sets the bounds of a type variable previously returned by
AbstractTypes.createTypeVariable(TypeParameterElement, WildcardType) . |
TypeElement |
typeElement(Class<?> clazz)
Returns a type element corresponding to the given
Class object. |
TypeMirror |
typeMirror(Type type)
Returns a type mirror corresponding to the given Java reflection type.
|
PrimitiveType |
unboxedType(TypeMirror type) |
asElement, capture, contains, erasure, getTypeVariable, isSameType, isSubtype, requireValidTypes, resolveActualTypeArguments, substitute, toString
public static ReflectionTypes getInstance()
Since this class does not contain any state, and since it is immutable, the returned instance is thread-safe.
protected void requireValidElement(Element element)
AbstractTypes
Element
is valid for use with this class.
The meaning of valid is implementation-defined, but typically a valid Element
must have been created
by the implementation that belongs to the current AbstractTypes
instance.
requireValidElement
in class AbstractTypes
element
- elementprotected void requireValidType(@Nullable TypeMirror type)
AbstractTypes
TypeMirror
is valid for use with this class, or that it is null
.
The meaning of valid is implementation-defined, but typically a valid TypeMirror
must have been
created by the implementation that belongs to the current AbstractTypes
instance. A null
argument
is always valid. The rationale is that null
TypeMirror
arguments have a special meaning for some
methods such as Types.getWildcardType(TypeMirror, TypeMirror)
or
AbstractTypes.createTypeVariable(TypeParameterElement, WildcardType)
.
requireValidType
in class AbstractTypes
type
- type mirror, may be null
public TypeElement boxedClass(PrimitiveType primitiveType)
public PrimitiveType unboxedType(TypeMirror type)
public TypeElement typeElement(Class<?> clazz)
Class
object.clazz
- class objectClass
objectIllegalArgumentException
- if the given class represents a primitive or array typeUnsupportedOperationException
- if a generic declaration is referenced that is not of type Class
,
see typeMirror(Type)
for detailspublic TypeMirror typeMirror(Type type)
Type parameters in method declarations are not currently supported. That is, if the given type references a
TypeVariable
instance that has a Constructor
or
Method
as generic declaration, an UnsupportedOperationException
will be thrown.
typeMirror
in class AbstractTypes
type
- type as represented by Java Reflection APIUnsupportedOperationException
- if a generic declaration is referenced that is not of type Class
public DeclaredType getDeclaredType(@Nullable DeclaredType containing, TypeElement typeElem, TypeMirror... typeArgs)
public DeclaredType getDeclaredType(TypeElement typeElem, TypeMirror... typeArgs)
public NullType getNullType()
public ArrayType getArrayType(TypeMirror componentType)
protected TypeVariable createTypeVariable(TypeParameterElement typeParameter, @Nullable WildcardType capturedTypeArgument)
AbstractTypes
Whenever this method is called within this class, the returned type variable is guaranteed to be passed to
AbstractTypes.setTypeVariableBounds(TypeVariable, TypeMirror, TypeMirror)
before being used as a TypeVariable
instance. That is, the returned type variable is considered to be under construction until being passed to
AbstractTypes.setTypeVariableBounds(TypeVariable, TypeMirror, TypeMirror)
, and only after that method call, the type
variable will have to satisfy the contract specified by interface TypeVariable
and its super-interfaces.
Before AbstractTypes.setTypeVariableBounds(TypeVariable, TypeMirror, TypeMirror)
is called on the returned
TypeVariable
, calling either TypeVariable.getUpperBound()
or TypeVariable.getLowerBound()
must trigger an IllegalStateException
.
Note that the previous paragraph does not guarantee that
AbstractTypes.setTypeVariableBounds(TypeVariable, TypeMirror, TypeMirror)
is always called on the newly returned
type-variable instance. If any exception occurs before the new type-variable could be used, then
AbstractTypes.setTypeVariableBounds(TypeVariable, TypeMirror, TypeMirror)
may not be called (even if the exception is
unrelated to the construction of the new type-variable instance).
The TypeVariable
interface does not provide access to captured wildcard type arguments. It can be
retrieved by calling AbstractTypes.capturedTypeArgument(TypeVariable)
instead.
createTypeVariable
in class AbstractTypes
typeParameter
- the formal type parametercapturedTypeArgument
- the wildcard type argument that new type variable captures as part of a capture
conversion (ยง5.1.10 JLS), or null
if the new type variable is not the result of a capture conversionTypeVariable
AbstractTypes.setTypeVariableBounds(TypeVariable, TypeMirror, TypeMirror)
,
AbstractTypes.capturedTypeArgument(TypeVariable)
protected WildcardType capturedTypeArgument(TypeVariable typeVariable)
AbstractTypes
This method returns the wildcard type argument that was previously passed to
AbstractTypes.createTypeVariable(TypeParameterElement, WildcardType)
.
capturedTypeArgument
in class AbstractTypes
typeVariable
- the type variable that may be the result of a capture conversionpublic IntersectionType getIntersectionType(TypeMirror... bounds)
AbstractTypes
getIntersectionType
in class AbstractTypes
bounds
- the bounds of the new intersection typeprotected void setTypeVariableBounds(TypeVariable typeVariable, TypeMirror upperBound, TypeMirror lowerBound)
AbstractTypes
AbstractTypes.createTypeVariable(TypeParameterElement, WildcardType)
.
Before an (unfinished) type-variable instance returned by
AbstractTypes.createTypeVariable(TypeParameterElement, WildcardType)
is used by this class, this method is guaranteed
to be called exactly once.
Implementations that create effectively immutable TypeMirror
instances may use this method to "freeze"
the given type-variable instance.
setTypeVariableBounds
in class AbstractTypes
typeVariable
- type variable previously returned by
AbstractTypes.createTypeVariable(TypeParameterElement, WildcardType)
upperBound
- Upper bound for the given type variable. If no explicit upper bound is used, a
DeclaredType
representing Object
will be passed.lowerBound
- Lower bound for the given type variable. This may a NullType
instance, unless capture
conversion produced a type variable with a non-trivial lower bound.AbstractTypes.createTypeVariable(TypeParameterElement, WildcardType)
public PrimitiveType getPrimitiveType(TypeKind kind)
public WildcardType getWildcardType(@Nullable TypeMirror extendsBound, @Nullable TypeMirror superBound)
public boolean isAssignable(TypeMirror t1, TypeMirror t2)
public boolean isSubsignature(ExecutableType m1, ExecutableType m2)
public TypeMirror asMemberOf(DeclaredType containing, Element element)
public List<? extends TypeMirror> directSupertypes(TypeMirror t)
UnsupportedOperationException
- whenever this method is calledCopyright © 2014–2015. All rights reserved.