public class InstantiatedType extends ParameterizedType
Note that ParameterizedType
is an interface that can represent
either a parameterized type in the sense meant here, or a generic class. Conversion to this type
from Type
interfaces is handled by Type.forType(java.lang.reflect.Type)
.
Modifier and Type | Field and Description |
---|---|
private java.util.List<TypeArgument> |
argumentList
The type arguments for this class.
|
private GenericClassType |
genericType
The generic class for this type.
|
enclosingType
Constructor and Description |
---|
InstantiatedType(GenericClassType genericType,
java.util.List<TypeArgument> argumentList)
Create a parameterized type from the generic class type.
|
Modifier and Type | Method and Description |
---|---|
InstantiatedType |
applyCaptureConversion()
Constructs a capture conversion for this type.
|
boolean |
equals(@Nullable java.lang.Object obj) |
GenericClassType |
getGenericClassType()
Returns the
GenericClassType for this parameterized type. |
@Nullable Substitution |
getInstantiatingSubstitution(ReferenceType goalType)
Computes a substitution that can be applied to the type variables of the generic goal type to
instantiate operations of this type, possibly inherited from from the goal type.
|
java.util.List<ClassOrInterfaceType> |
getInterfaces()
Constructs the list of interfaces for this parameterized type.
|
InstantiatedType |
getMatchingSupertype(GenericClassType goalType)
Finds the parameterized type that is a supertype of this class that also matches the given
generic class.
|
NonParameterizedType |
getRawtype()
Returns the non-parameterized form of this class type.
|
(package private) java.util.List<ReferenceType> |
getReferenceArguments()
Returns the list of reference type arguments of this type if there are no wildcards.
|
java.lang.Class<?> |
getRuntimeClass()
Returns the runtime
Class object for this type. |
ClassOrInterfaceType |
getSuperclass()
Constructs the superclass type for this parameterized type.
|
java.util.List<TypeArgument> |
getTypeArguments()
Returns the type arguments for this type.
|
java.util.List<TypeVariable> |
getTypeParameters()
Returns the list of type parameters for this type.
|
@Nullable Substitution |
getTypeSubstitution()
Creates the type substitution of the type arguments of this type for the type variables of the
instantiated class, if the type arguments are reference types.
|
boolean |
hasCaptureVariable()
Indicates whether this
ReferenceType has a capture variable. |
int |
hashCode() |
boolean |
hasWildcard()
Indicates whether this type has a wildcard anywhere within it.
|
boolean |
isAbstract()
Indicate whether this class is abstract.
|
boolean |
isAssignableFrom(Type otherType)
Indicates whether there is an assignment conversion from a source
Type to this type. |
boolean |
isGeneric(boolean ignoreWildcards)
Indicate whether this type is generic.
|
boolean |
isInstantiationOf(ReferenceType otherType)
Checks whether this type is an instantiation of the given instantiated type.
|
boolean |
isInterface()
Indicates whether this object is an interface type.
|
boolean |
isParameterized()
Indicate whether this type is a parameterized type.
|
boolean |
isRecursiveType()
Determines if this type is recursive in the sense that the type is the bound of its type
argument.
|
boolean |
isStatic()
Indicates whether this class is static.
|
boolean |
isSubtypeOf(Type otherType)
Test whether this type is a subtype of the given type according to transitive closure of
definition of the direct supertype relation in section 4.10.2
of JLS for JavaSE 8.
|
InstantiatedType |
substitute(Substitution substitution)
Applies a substitution to a
ReferenceType . |
forClass, forType, getBinaryName, getFqName, getUnqualifiedBinaryName
applyCaptureConversion, getAllSupertypesInclusive, getCanonicalName, getImmediateSupertypes, getPackage, getPackagePrefix, getSimpleName, getSuperTypes, isClassOrInterfaceType, isMemberClass, isNestedClass, substitute
getInstantiatingSubstitutionforTypeVariable, isCaptureVariable, isReferenceType
compareTo, forFullyQualifiedName, forName, forValue, getTypeforFullyQualifiedName, isArray, isAssignableFromTypeOf, isBoxedPrimitive, isClass, isEnum, isGeneric, isNonreceiverType, isObject, isPrimitive, isRawtype, isString, isVariable, isVoid, runtimeClassIs, toString
private final GenericClassType genericType
private final java.util.List<TypeArgument> argumentList
InstantiatedType(GenericClassType genericType, java.util.List<TypeArgument> argumentList)
genericType
- the generic class typeargumentList
- the list of type argumentsjava.lang.IllegalArgumentException
- if either argument is nullpublic boolean equals(@Nullable java.lang.Object obj)
Test if the given object is equal to this parameterized type. Two parameterized types are equal if they have the same raw type and the same type arguments.
equals
in class ClassOrInterfaceType
public int hashCode()
hashCode
in class ClassOrInterfaceType
public InstantiatedType substitute(Substitution substitution)
ClassOrInterfaceType
ReferenceType
. If the type is parameterized then replaces
type variables that occur, otherwise returns this type.
This abstract method forces typing of substitutions applied to ReferenceType
objects
without an explicit cast.
This abstract method allows substitutions to be applied to ClassOrInterfaceType
objects without casting.
substitute
in class ParameterizedType
substitution
- the type substitutionpublic InstantiatedType applyCaptureConversion()
CaptureTypeVariable
for each wildcard as described in the JLS, section
5.1.10, Capture
Conversion.
Based on algorithm in Mads Torgerson et al. "Adding Wildcards to the Java Programming Language", Journal of Object Technology, 3 (December 2004) 11, 97-116. Special Issue: OOPS track at SAC 2004.
If this type has no wildcards, then returns this type.
applyCaptureConversion
in class ClassOrInterfaceType
public java.util.List<ClassOrInterfaceType> getInterfaces()
See the implementation note for getSuperclass()
.
getInterfaces
in class ClassOrInterfaceType
public GenericClassType getGenericClassType()
ParameterizedType
GenericClassType
for this parameterized type.getGenericClassType
in class ParameterizedType
public InstantiatedType getMatchingSupertype(GenericClassType goalType)
class C<T> implements Comparator<T>
and class A
extends C<String>
, then when applied to A
, this method would return C<String>
when given C<T>
, and Comparator<String>
when given Comparator<E>
.
Returns null if there is no such type.
Performs a depth-first search of the supertype relation for this type. If the goal type is an interface, then searches the interfaces of this type first.
An instantiated type may have a wildcard, and so must perform capture conversion before doing supertype search.
getMatchingSupertype
in class ClassOrInterfaceType
goalType
- the generic class typejava.util.List<ReferenceType> getReferenceArguments()
public java.lang.Class<?> getRuntimeClass()
Type
Class
object for this type. For use when reflection is needed.
Note that type variables and the null reference type do not have a runtime class, and this method will return null in those cases.
This method should not be confused with the inherited Object.getClass()
method,
which returns the Class<?>
for the Type
object, and not of the represented
type. For instance, if a Type
object t
represented the Java type int
,
then t.getRuntimeClass()
would return int.class
while t.getClass()
would return Type.class
.
getRuntimeClass
in class Type
Class
that is the runtime representation of the type, or null if this type
is a type variable or null reference typepublic ClassOrInterfaceType getSuperclass()
Implementation note: we can think of an InstantiatedType
A<T1,...,Tk>
as
being represented as a generic class A<F1,...,Fk>
with a substitution [ Fi :=
Ti]
for all of the type parameters Fi
. So, when we compute a superclass, we first find
the supertype of the generic class B<F1,...,Fk>
, and then apply the substitution [ Fi := Ti]
using the method GenericClassType.getSuperclass(Substitution)
.
getSuperclass
in class ClassOrInterfaceType
public java.util.List<TypeArgument> getTypeArguments()
getTypeArguments
in class ClassOrInterfaceType
public java.util.List<TypeVariable> getTypeParameters()
ReferenceType
ArrayType
(such as E[]
), and subclasses of ParameterizedType
.getTypeParameters
in class ClassOrInterfaceType
public @Nullable Substitution getTypeSubstitution()
public boolean hasWildcard()
Type
hasWildcard
in class ClassOrInterfaceType
public boolean hasCaptureVariable()
Type
ReferenceType
has a capture variable.hasCaptureVariable
in class ClassOrInterfaceType
public boolean isAbstract()
ClassOrInterfaceType
isAbstract
in class ClassOrInterfaceType
public boolean isAssignableFrom(Type otherType)
ReferenceType
Type
to this type.
(In other words, a value of the source type can be assigned to an l-value of this type.)
Returns true if this is a legal assignment conversion:
Variablethis = Expressionsourcetype.
Based on the definition of assignment context in section 5.2 of the Java Language Specification, a value of one type is assignable to a variable of another type if the first type can be converted to the second by
For assignment to ReferenceType
, checks for widening reference conversion when the
source type is also a reference type. See section JLS
5.1.5 for details.
isAssignableFrom
in class ReferenceType
otherType
- the type to test for assignabilitypublic boolean isGeneric(boolean ignoreWildcards)
Type
isGeneric
in class ClassOrInterfaceType
ignoreWildcards
- if true, ignore wildcards; that is, treat wildcards as not making the
operation genericpublic boolean isInstantiationOf(ReferenceType otherType)
A<T1,...,Tk>
where all Ti
are instantiated by ground
types (e.g., does not have type variables), the other type is A<S1,...,Sk>
, and each
Ti
matches Si
for i = 1,...,k
as follows:
Si
is the variable X
with lower bound L
and upper bound U
, then Ti
is a supertype of L
and a subtype of U
Si
is identical to Ti
isInstantiationOf
in class ClassOrInterfaceType
otherType
- the other InstantiatedType
ReferenceType.isInstantiationOf(ReferenceType)
public @Nullable Substitution getInstantiatingSubstitution(ReferenceType goalType)
ReferenceType
If there is no unifying substitution, returns null
.
getInstantiatingSubstitution
in class ClassOrInterfaceType
goalType
- the generic type for which a substitution is neededpublic boolean isInterface()
Type
isInterface
in class Type
public boolean isParameterized()
Type
C<T1,...,Tk>
that instantiates a generic class C<F1,...,Fk>
.
If inputType.isParameterized returns true, there are two possibilities: inputType
instanceof InstantiatedType
, or inputType is a member class and the enclosing type is a
parameterized type
isParameterized
in class ClassOrInterfaceType
public boolean isRecursiveType()
public boolean isStatic()
ClassOrInterfaceType
isStatic
in class ClassOrInterfaceType
public boolean isSubtypeOf(Type otherType)
Handles specific cases of supertypes of a parameterized type C<T1,...,Tn>
instantiating the generic type C<F1,...,Fn>
by substitution θ=[F1:=T1,...,Fn:=Tn]
for which direct supertypes are:
D<U1
θ,...,Uk
θ>
where D<U1,...,Uk>
is a
supertype of C<F1,...,Fn>
.
C<S1,...,Sn>
where Si contains Ti (JLS section 4.5.1).
C
.
Object
if generic form is interface with no interfaces as supertypes.
isSubtypeOf
in class ClassOrInterfaceType
otherType
- the possible supertypeReferenceType.isAssignableFrom(Type)
,
ClassOrInterfaceType.isSubtypeOf(Type)
public NonParameterizedType getRawtype()
ClassOrInterfaceType
getRawtype
in class ClassOrInterfaceType