Google Cloud Dataflow SDK for Java, version 1.9.1
Class TypeDescriptor<T>
- java.lang.Object
-
- com.google.cloud.dataflow.sdk.values.TypeDescriptor<T>
-
- Type Parameters:
T
- the type represented by thisTypeDescriptor
- All Implemented Interfaces:
- Serializable
public abstract class TypeDescriptor<T> extends Object implements Serializable
A description of a Java type, including actual generic parameters where possible.To prevent losing actual type arguments due to erasure, create an anonymous subclass with concrete types:
TypeDecriptor<List<String>> = new TypeDescriptor<List<String>>() {};
If the above were not an anonymous subclass, the type
List<String>
would be erased and unavailable at run time.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor and Description protected
TypeDescriptor()
Creates aTypeDescriptor
representing the type parameterT
.protected
TypeDescriptor(Class<?> clazz)
Creates aTypeDescriptor
representing the type parameterT
, which should resolve to a concrete type in the context of the classclazz
.protected
TypeDescriptor(Object instance)
Creates aTypeDescriptor
representing the type parameterT
, which should resolve to a concrete type in the context of the classclazz
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description boolean
equals(Object other)
Two type descriptor are equal if and only if they represent the same type.List<TypeDescriptor<?>>
getArgumentTypes(Method method)
Returns a list of argument types for the given method, which must be a part of the class.Iterable<TypeDescriptor>
getClasses()
Returns a set ofTypeDescriptor
s, one for each superclass (including this class).TypeDescriptor<?>
getComponentType()
Returns the component type if this type is an array type, otherwise returnsnull
.Iterable<TypeDescriptor>
getInterfaces()
Returns a set ofTypeDescriptor
s, one for each interface implemented by this class.Class<? super T>
getRawType()
TypeDescriptor<? super T>
getSupertype(Class<? super T> superclass)
Returns the generic form of a supertype.Type
getType()
Returns theType
represented by thisTypeDescriptor
.TypeVariable<Class<? super T>>
getTypeParameter(String paramName)
Returns aTypeVariable
for the named type parameter.int
hashCode()
boolean
isArray()
Returns true if this type is known to be an array type.boolean
isSubtypeOf(TypeDescriptor<?> parent)
Return true if this type is a subtype of the given type.boolean
isSupertypeOf(TypeDescriptor<?> source)
Returns true if this type is assignable from the given type.static <T> TypeDescriptor<T>
of(Class<T> type)
Returns aTypeDescriptor
representing the given type.static TypeDescriptor<?>
of(Type type)
Returns aTypeDescriptor
representing the given type.TypeDescriptor<?>
resolveType(Type type)
Returns aTypeDescriptor
representing the given type, with type variables resolved according to the specialization in this type.String
toString()
-
-
-
Constructor Detail
-
TypeDescriptor
protected TypeDescriptor()
Creates aTypeDescriptor
representing the type parameterT
. To use this constructor properly, the type parameter must be a concrete type, for examplenew TypeDescriptor<List<String>>(){}
.
-
TypeDescriptor
protected TypeDescriptor(Object instance)
Creates aTypeDescriptor
representing the type parameterT
, which should resolve to a concrete type in the context of the classclazz
.Unlike
TypeDescriptor(Class)
this will also use context's of the enclosing instances while attempting to resolve the type. This means that the types of any classes instantiated in the concrete instance should be resolvable.
-
TypeDescriptor
protected TypeDescriptor(Class<?> clazz)
Creates aTypeDescriptor
representing the type parameterT
, which should resolve to a concrete type in the context of the classclazz
.
-
-
Method Detail
-
of
public static <T> TypeDescriptor<T> of(Class<T> type)
Returns aTypeDescriptor
representing the given type.
-
of
public static TypeDescriptor<?> of(Type type)
Returns aTypeDescriptor
representing the given type.
-
getType
public Type getType()
Returns theType
represented by thisTypeDescriptor
.
-
getComponentType
public TypeDescriptor<?> getComponentType()
Returns the component type if this type is an array type, otherwise returnsnull
.
-
getSupertype
public final TypeDescriptor<? super T> getSupertype(Class<? super T> superclass)
Returns the generic form of a supertype.
-
isArray
public final boolean isArray()
Returns true if this type is known to be an array type.
-
getTypeParameter
public final TypeVariable<Class<? super T>> getTypeParameter(String paramName)
Returns aTypeVariable
for the named type parameter. ThrowsIllegalArgumentException
if a type variable by the requested type parameter is not found.For example,
new TypeDescriptor<List>(){}.getTypeParameter("T")
returns aTypeVariable<? super List>
representing the formal type parameterT
.Do not mistake the type parameters (formal type argument list) with the actual type arguments. For example, if a class
Foo
extendsList<String>
, it does not make sense to ask for a type parameter, becauseFoo
does not have any.
-
isSupertypeOf
public final boolean isSupertypeOf(TypeDescriptor<?> source)
Returns true if this type is assignable from the given type.
-
isSubtypeOf
public final boolean isSubtypeOf(TypeDescriptor<?> parent)
Return true if this type is a subtype of the given type.
-
getArgumentTypes
public List<TypeDescriptor<?>> getArgumentTypes(Method method)
Returns a list of argument types for the given method, which must be a part of the class.
-
resolveType
public TypeDescriptor<?> resolveType(Type type)
Returns aTypeDescriptor
representing the given type, with type variables resolved according to the specialization in this type.For example, consider the following class:
class MyList implements List<String> { ... }
The
TypeDescriptor
returned byTypeDescriptor.of(MyList.class) .resolveType(Mylist.class.getMethod("get", int.class).getGenericReturnType)
String
.
-
getInterfaces
public Iterable<TypeDescriptor> getInterfaces()
Returns a set ofTypeDescriptor
s, one for each interface implemented by this class.
-
getClasses
public Iterable<TypeDescriptor> getClasses()
Returns a set ofTypeDescriptor
s, one for each superclass (including this class).
-
equals
public boolean equals(Object other)
Two type descriptor are equal if and only if they represent the same type.
-
-