T
- This is the type which the TypeToken object references.public class TypeToken<T> extends java.lang.Object implements DeepCopyable<TypeToken<T>>, ReifiedType<TypeToken<T>>, DeclarationToken<TypeToken<T>>
TypeToken provides reflective operations and services over the Java type
system. It is analogous to Class<?>
, but provides access to a much
richer set of tools, and can be used over the domain of all types, not just
raw types.
TypeToken is effectively immutable, though may perform shared caching of results transparently to the user. Like Class, A TypeToken will always be parameterized with the type it reflects over when used as intended.
Modifier and Type | Class and Description |
---|---|
static interface |
TypeToken.Capture
Specifies behavior of wildcards.
|
static interface |
TypeToken.Infer
Specifies behavior of wildcards.
|
static interface |
TypeToken.Retain
Specifies behavior of wildcards.
|
static class |
TypeToken.Wildcards
Treatment of wildcards for
TypeToken s created over parameterized
types. |
Modifier | Constructor and Description |
---|---|
protected |
TypeToken() |
protected |
TypeToken(BoundSet bounds,
java.lang.reflect.Type type) |
protected |
TypeToken(java.lang.reflect.Type type) |
Modifier and Type | Method and Description |
---|---|
T |
cast(java.lang.Object object) |
java.util.stream.Stream<ExecutableToken<java.lang.Void,T>> |
constructors()
Find which constructors can be invoked for this type.
|
TypeToken<T> |
copy() |
java.util.stream.Stream<ExecutableToken<java.lang.Void,T>> |
declaredConstructors()
Find which constructors can be invoked for this type.
|
java.util.stream.Stream<FieldToken<T,?>> |
declaredFields()
Find which fields are declared on this type.
|
java.util.stream.Stream<ExecutableToken<T,?>> |
declaredMethods()
Find which methods can be invoked on this type, whether statically or on
instances.
|
TypeToken<T> |
deepCopy(Isomorphism isomorphism) |
boolean |
equals(java.lang.Object obj) |
java.util.stream.Stream<FieldToken<T,?>> |
fields()
Find which fields can be resolved on this type.
|
static TypeToken<?> |
forAnnotatedType(java.lang.reflect.AnnotatedType type)
Create a TypeToken for an arbitrary type, preserving wildcards where
possible.
|
static <T> TypeToken<T> |
forClass(java.lang.Class<T> type)
Create a TypeToken for a raw class.
|
static TypeToken<?> |
forNull()
Create a TypeToken over the null type.
|
static TypeToken<?> |
forType(BoundSet bounds,
java.lang.reflect.Type type,
TypeToken.Wildcards wildcards)
Create a TypeToken for an arbitrary type, preserving wildcards where
possible.
|
static TypeToken<?> |
forType(java.lang.reflect.Type type)
Create a TypeToken for an arbitrary type, preserving wildcards where
possible.
|
static TypeToken<?> |
forType(java.lang.reflect.Type type,
TypeToken.Wildcards wildcards)
Create a TypeToken for an arbitrary type.
|
static TypeToken<?> |
fromString(java.lang.String typeString)
Equivalent to the application of
forType(Type) to the
result of Types.fromString(String) . |
static TypeToken<?> |
fromString(java.lang.String typeString,
Imports imports)
Equivalent to the application of
forType(Type) to the
result of AnnotatedTypes.fromString(String, Imports) , with the given
imports. |
java.lang.reflect.AnnotatedType |
getAnnotatedDeclaration() |
BoundSet |
getBounds()
This method returns a copy of the Resolver backing by this TypeToken.
|
java.lang.Class<? super T> |
getErasedType()
|
TypeToken<? super T> |
getErasedTypeToken()
|
java.util.stream.Stream<java.lang.Class<?>> |
getErasedUpperBounds()
Determine the raw types of a given type, accounting for inference variables
which may have instantiations or upper bounds within the context of this
resolver.
|
TypeToken<? extends T> |
getExtending(TypeToken.Wildcards wildcards)
Create a TypeToken over a wildcard type which has the type represented by
this TypeToken as an upper bound.
|
java.util.stream.Stream<InferenceVariable> |
getInferenceVariablesMentioned()
Determine which inference variables are mentioned by the type of this
TypeToken . |
java.util.Optional<DeclarationToken<?>> |
getOwningDeclaration() |
java.util.stream.Stream<InferenceVariable> |
getRemainingInferenceVariableDependencies()
Determine which inference variables are dependencies of those mentioned by
the type of this
TypeToken . |
TypeToken<? super T> |
getSuper(TypeToken.Wildcards wildcards)
Create a TypeToken over a wildcard type which has the type represented by
this TypeToken as a lower bound.
|
TypeToken<T> |
getThis() |
TypeToken<TypeToken<T>> |
getThisTypeToken() |
java.lang.reflect.Type |
getType()
The type represented by this
TypeToken . |
java.util.stream.Stream<TypeArgument<?>> |
getTypeArguments() |
int |
getTypeParameterCount() |
java.util.stream.Stream<TypeParameter<?>> |
getTypeParameters() |
java.util.stream.Stream<java.lang.reflect.Type> |
getUpperBounds()
Find the upper bounding classes and parameterized types of a given type.
|
TypeToken<? extends T> |
getWildcardExtending()
Create a TypeToken over a wildcard type which has the type represented by
this TypeToken as an upper bound.
|
TypeToken<? super T> |
getWildcardSuper()
Create a TypeToken over a wildcard type which has the type represented by
this TypeToken as a lower bound.
|
int |
hashCode() |
TypeToken<? extends T> |
infer()
If the type is raw it is parameterized with inference variables.
|
boolean |
isCastableFrom(java.lang.reflect.Type type) |
boolean |
isCastableFrom(TypeToken<?> type) |
boolean |
isCastableTo(java.lang.reflect.Type type) |
boolean |
isCastableTo(TypeToken<?> type) |
boolean |
isGeneric() |
boolean |
isPrimitive()
Is the type a primitive type as per the Java type system.
|
boolean |
isPrimitiveWrapper()
Is the type a wrapper for a primitive type as per the Java type system.
|
boolean |
isProper()
Determine whether this
TypeToken represents a proper type. |
boolean |
isRaw() |
java.util.stream.Stream<ExecutableToken<T,?>> |
methods()
Find which methods of the given name can be invoked on instances of this
type.
|
TypeToken<? extends T> |
parameterize()
If the declaration is raw, parameterize it with its own type parameters,
otherwise return the declaration itself.
|
TypeToken<T> |
resolve()
This method will attempt to infer the actual type represented by this
TypeToken, which means the types of any inference variables mentioned will be
inferred and substituted.
|
TypeToken<? super T> |
resolveSupertype(java.lang.Class<?> superclass)
|
boolean |
satisfiesConstraintFrom(ConstraintFormula.Kind kind,
java.lang.reflect.Type type)
Derive a new type from this one, with a constraint between this type and a
given type.
|
boolean |
satisfiesConstraintFrom(ConstraintFormula.Kind kind,
TypeToken<?> type)
Derive a new type from this one, with a constraint between this type and a
given type.
|
boolean |
satisfiesConstraintTo(ConstraintFormula.Kind kind,
java.lang.reflect.Type type)
Derive a new type from this one, with a constraint between this type and a
given type.
|
boolean |
satisfiesConstraintTo(ConstraintFormula.Kind kind,
TypeToken<?> type)
Derive a new type from this one, with a constraint between this type and a
given type.
|
TypeToken<T> |
substituteInstantiations()
This method will attempt to substitute any inference variables mentioned by
this type with their instantiations, if instantiations are available, and
return a TypeToken over the resulting type.
|
java.lang.String |
toString() |
java.lang.String |
toString(Imports imports)
Equivalent to the application of
AnnotatedTypes.toString(AnnotatedType, Imports) to
getAnnotatedDeclaration() , with the given imports. |
TypeToken<T> |
unwrapPrimitive()
If this TypeToken is a wrapper of a primitive type, determine the unwrapped
primitive type.
|
TypeToken<T> |
withAllTypeArguments(java.util.List<java.lang.reflect.Type> typeArguments)
Derive a new
DeclarationToken instance with the given type argument
parameterization. |
TypeToken<T> |
withAllTypeArguments(java.lang.reflect.Type... typeArguments) |
TypeToken<T> |
withBounds(BoundSet bounds)
Derive a new
TypeToken instance, with the given bounds incorporated
into the bounds of the underlying resolver. |
TypeToken<T> |
withConstraintFrom(ConstraintFormula.Kind kind,
java.lang.reflect.Type type)
Derive a new type from this one, with a constraint between this type and a
given type.
|
TypeToken<T> |
withConstraintFrom(ConstraintFormula.Kind kind,
TypeToken<?> type)
Derive a new type from this one, with a constraint between this type and a
given type.
|
TypeToken<T> |
withConstraintTo(ConstraintFormula.Kind kind,
java.lang.reflect.Type type)
Derive a new type from this one, with a constraint between this type and a
given type.
|
TypeToken<T> |
withConstraintTo(ConstraintFormula.Kind kind,
TypeToken<?> type)
Derive a new type from this one, with a constraint between this type and a
given type.
|
TypeToken<T> |
withTypeArguments(java.util.Collection<? extends TypeArgument<?>> arguments)
Derive a new
ExecutableToken instance from this, with types
substituted according to the given arguments. |
TypeToken<T> |
withTypeArguments(java.util.List<java.lang.reflect.Type> typeArguments)
As @see
DeclarationToken.withAllTypeArguments(List) , but only providing arguments for
the parameters occurring directly on the declaration. |
TypeToken<T> |
withTypeArguments(java.lang.reflect.Type... typeArguments) |
TypeToken<T> |
wrapPrimitive()
If this TypeToken is a primitive type, determine the wrapped primitive type.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
deepCopy
asTypedObject, getThisType
getAllTypeArguments, getAllTypeParameterCount, getAllTypeParameters, resolveTypeArgument, resolveTypeArgument, withTypeArguments
protected TypeToken()
protected TypeToken(java.lang.reflect.Type type)
protected TypeToken(BoundSet bounds, java.lang.reflect.Type type)
public static <T> TypeToken<T> forClass(java.lang.Class<T> type)
T
- the type of the new TypeToken
type
- the class to create a TypeToken forpublic static TypeToken<?> forType(java.lang.reflect.Type type)
type
- the requested typepublic static TypeToken<?> forType(BoundSet bounds, java.lang.reflect.Type type, TypeToken.Wildcards wildcards)
bounds
- the set of bounds on any inference variables mentioned in the typetype
- the requested typewildcards
- how to deal with wildcard parameters on the given typepublic static TypeToken<?> forAnnotatedType(java.lang.reflect.AnnotatedType type)
type
- the requested typepublic static TypeToken<?> forType(java.lang.reflect.Type type, TypeToken.Wildcards wildcards)
type
- the requested typewildcards
- how to deal with wildcard parameters on the given typepublic static TypeToken<?> forNull()
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public TypeToken<TypeToken<T>> getThisTypeToken()
getThisTypeToken
in interface ReifiedType<TypeToken<T>>
TypeToken
over the value of ReifiedType.getThisType()
public TypeToken<T> deepCopy(Isomorphism isomorphism)
deepCopy
in interface Copyable<TypeToken<T>>
deepCopy
in interface DeepCopyable<TypeToken<T>>
isomorphism
- an object graph Isomorphism
Isomorphism
public TypeToken<T> withBounds(BoundSet bounds)
TypeToken
instance, with the given bounds incorporated
into the bounds of the underlying resolver. The original TypeToken
will remain unmodified.
All bounds are incorporated if and only if they have the potential to affect the resolution of inference variables mentioned by this type.
bounds
- the new bounds to incorporateTypeToken
public static TypeToken<?> fromString(java.lang.String typeString)
forType(Type)
to the
result of Types.fromString(String)
.typeString
- the String to parsepublic static TypeToken<?> fromString(java.lang.String typeString, Imports imports)
forType(Type)
to the
result of AnnotatedTypes.fromString(String, Imports)
, with the given
imports.typeString
- the String to parseimports
- classes and packages for which full package qualification may be
omitted from inputpublic java.lang.String toString(Imports imports)
AnnotatedTypes.toString(AnnotatedType, Imports)
to
getAnnotatedDeclaration()
, with the given imports.imports
- classes and packages for which full package qualification may be
omitted from outputpublic TypeToken<? extends T> getWildcardExtending()
TypeToken<List<?>>
will give a
TypeToken<? extends List<?>>
.public TypeToken<? extends T> getExtending(TypeToken.Wildcards wildcards)
TypeToken.Wildcards
argument
given. Either the wildcard will be preserved, a fresh
TypeVariableCapture
of the wildcard type will be captured, or an
InferenceVariable
will be substituted.
For example, invoking this method on a TypeToken<List<?>>
will give a
TypeToken<? extends List<?>>
.wildcards
- how to deal with the wildcard parameter on the new typepublic TypeToken<? super T> getWildcardSuper()
TypeToken<List<?>>
will give a
TypeToken<? super List<?>>
.public TypeToken<? super T> getSuper(TypeToken.Wildcards wildcards)
TypeToken.Wildcards
argument
given. Either the wildcard will be preserved, a fresh
TypeVariableCapture
of the wildcard type will be captured, or an
InferenceVariable
will be substituted.
For example, invoking this method on a TypeToken<List<?>>
will give a
TypeToken<? super List<?>>
.wildcards
- how to deal with the wildcard parameter on the new typepublic java.lang.Class<? super T> getErasedType()
public TypeToken<? super T> getErasedTypeToken()
public java.util.stream.Stream<java.lang.reflect.Type> getUpperBounds()
Types.getUpperBounds(Type)
this respects bounds on the
inference variables in this resolver.public java.util.stream.Stream<java.lang.Class<?>> getErasedUpperBounds()
public BoundSet getBounds()
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.reflect.Type getType()
TypeToken
.public boolean isPrimitive()
public boolean isPrimitiveWrapper()
public TypeToken<T> wrapPrimitive()
public TypeToken<T> unwrapPrimitive()
public TypeToken<T> withConstraintTo(ConstraintFormula.Kind kind, java.lang.reflect.Type type)
type
- the type to constrain againstkind
- the kind of the constraint formulapublic TypeToken<T> withConstraintTo(ConstraintFormula.Kind kind, TypeToken<?> type)
type
- the type to constrain againstkind
- the kind of the constraint formulapublic TypeToken<T> withConstraintFrom(ConstraintFormula.Kind kind, java.lang.reflect.Type type)
type
- the type to constrain againstkind
- the kind of the constraint formulapublic TypeToken<T> withConstraintFrom(ConstraintFormula.Kind kind, TypeToken<?> type)
type
- the type to constrain againstkind
- the kind of the constraint formulapublic boolean satisfiesConstraintTo(ConstraintFormula.Kind kind, java.lang.reflect.Type type)
type
- the type to constrain againstkind
- the kind of the constraint formulapublic boolean satisfiesConstraintTo(ConstraintFormula.Kind kind, TypeToken<?> type)
type
- the type to constrain againstkind
- the kind of the constraint formulapublic boolean satisfiesConstraintFrom(ConstraintFormula.Kind kind, java.lang.reflect.Type type)
type
- the type to constrain againstkind
- the kind of the constraint formulapublic boolean satisfiesConstraintFrom(ConstraintFormula.Kind kind, TypeToken<?> type)
type
- the type to constrain againstkind
- the kind of the constraint formulapublic TypeToken<? extends T> parameterize()
public TypeToken<T> substituteInstantiations()
public TypeToken<? extends T> infer()
If the type is already parameterized, the existing arguments are substituted according to their type. Bounds are incorporated according to those present on the type variables each argument instantiates.
public TypeToken<T> resolve()
public boolean isProper()
TypeToken
represents a proper type.public java.util.stream.Stream<InferenceVariable> getInferenceVariablesMentioned()
TypeToken
.TypeToken
and which are mentioned by
its type.public java.util.stream.Stream<InferenceVariable> getRemainingInferenceVariableDependencies()
TypeToken
.TypeToken
and
which are mentioned by its type.public java.lang.reflect.AnnotatedType getAnnotatedDeclaration()
public T cast(java.lang.Object object)
object
- an object to cast to this typepublic boolean isCastableTo(TypeToken<?> type)
type
- the type to determine castability topublic boolean isCastableFrom(TypeToken<?> type)
type
- the type to determine castability frompublic boolean isCastableTo(java.lang.reflect.Type type)
type
- the type to determine castability topublic boolean isCastableFrom(java.lang.reflect.Type type)
type
- the type to determine castability frompublic java.util.stream.Stream<FieldToken<T,?>> fields()
Field
objects applicable to this type, wrapped
in FieldToken
instancespublic java.util.stream.Stream<FieldToken<T,?>> declaredFields()
Field
objects applicable to this type, wrapped
in FieldToken
instancespublic java.util.stream.Stream<ExecutableToken<java.lang.Void,T>> constructors()
Constructor
objects applicable to this type,
wrapped in ExecutableToken
instancespublic java.util.stream.Stream<ExecutableToken<java.lang.Void,T>> declaredConstructors()
Constructor
objects applicable to this type,
wrapped in ExecutableToken
instancespublic java.util.stream.Stream<ExecutableToken<T,?>> methods()
Method
objects applicable to this type, wrapped
in ExecutableToken
instancespublic java.util.stream.Stream<ExecutableToken<T,?>> declaredMethods()
Method
objects applicable to this type, wrapped
in ExecutableToken
instancespublic boolean isRaw()
isRaw
in interface DeclarationToken<TypeToken<T>>
public boolean isGeneric()
isGeneric
in interface DeclarationToken<TypeToken<T>>
public int getTypeParameterCount()
getTypeParameterCount
in interface DeclarationToken<TypeToken<T>>
public java.util.stream.Stream<TypeParameter<?>> getTypeParameters()
getTypeParameters
in interface DeclarationToken<TypeToken<T>>
Executable
, or their inference variables if not yet
instantiated.public java.util.stream.Stream<TypeArgument<?>> getTypeArguments()
getTypeArguments
in interface DeclarationToken<TypeToken<T>>
Executable
, or their inference variables if not yet
instantiated.public TypeToken<T> withTypeArguments(java.util.Collection<? extends TypeArgument<?>> arguments)
DeclarationToken
ExecutableToken
instance from this, with types
substituted according to the given arguments.
More specifically, each of the given arguments represents a type variable and an instantiation for that type variable. Occurrences of those type variables in the declaration will be substituted for their instantiations in the derived declaration.
The substitution will only succeed if it results in a valid parameterization of the declaration.
For example, the following method could be used to derive instances of
TypeToken over different parameterizations of List<?>
at runtime.
public TypeToken<List<T>> getListType(TypeToken<T> elementType)} {
return new TypeToken<T>()} {}.withTypeArguments(new TypeParameter<T>() {}.as(elementType));
}
withTypeArguments
in interface DeclarationToken<TypeToken<T>>
arguments
- the type variable instantiationsExecutableToken
instance with the given
instantiation substituted for the given type variablepublic java.util.Optional<DeclarationToken<?>> getOwningDeclaration()
getOwningDeclaration
in interface DeclarationToken<TypeToken<T>>
public TypeToken<T> withTypeArguments(java.util.List<java.lang.reflect.Type> typeArguments)
DeclarationToken
DeclarationToken.withAllTypeArguments(List)
, but only providing arguments for
the parameters occurring directly on the declaration.withTypeArguments
in interface DeclarationToken<TypeToken<T>>
public TypeToken<T> withAllTypeArguments(java.util.List<java.lang.reflect.Type> typeArguments)
DeclarationToken
DeclarationToken
instance with the given type argument
parameterization.
The types in the given list correspond, in order, to the
type parameters
of this declaration. The current
parameterization of the declaration is substituted for that given.
Each substitution will only succeed if it is compatible with the bounds on
that type variable, and if it is more specific than the current argument,
whether it is an InferenceVariable
, a TypeVariableCapture
, or
another kind of Type
.
This behavior is different from DeclarationToken.withTypeArguments(Collection)
, which
performs a substitution for arbitrary type variables rather than
re-instantiating every parameter on the declaration.
withAllTypeArguments
in interface DeclarationToken<TypeToken<T>>
typeArguments
- a list of arguments for each generic type parameter of the
underlying declarationDeclarationToken
instance with the given
instantiations substituted for each generic type parameter, in orderpublic TypeToken<T> withTypeArguments(java.lang.reflect.Type... typeArguments)
withTypeArguments
in interface DeclarationToken<TypeToken<T>>
DeclarationToken.withTypeArguments(List)
public TypeToken<T> withAllTypeArguments(java.lang.reflect.Type... typeArguments)
withAllTypeArguments
in interface DeclarationToken<TypeToken<T>>
DeclarationToken.withTypeArguments(List)