Class Types
- java.lang.Object
-
- com.google.api.client.util.Types
-
public class Types extends Object
Utilities for working with Java types.- Since:
- 1.4
- Author:
- Yaniv Inbar
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Type
getArrayComponentType(Type array)
Returns the component type of the given array type, assumingisArray(Type)
.static Type
getBound(WildcardType wildcardType)
Returns the only bound of the given wildcard type.static Type
getIterableParameter(Type iterableType)
Returns the type parameter ofIterable
that is assignable from the given iterable type.static Type
getMapValueParameter(Type mapType)
Returns the value type parameter ofMap
that is assignable from the given map type.static Class<?>
getRawArrayComponentType(List<Type> context, Type componentType)
Returns the raw array component type to use -- for example for the first parameter ofArray.newInstance(Class, int)
-- for the given component type.static Class<?>
getRawClass(ParameterizedType parameterType)
Returns the raw class for the given parameter type as defined inParameterizedType.getRawType()
.static ParameterizedType
getSuperParameterizedType(Type type, Class<?> superClass)
Returns the parameterized type that is or extends the given type that matches the given super class.static boolean
isArray(Type type)
Returns whether the given type is an array.static boolean
isAssignableToOrFrom(Class<?> classToCheck, Class<?> anotherClass)
Returns whether a class is either assignable to or from another class.static <T> Iterable<T>
iterableOf(Object value)
Returns an iterable for an input iterable or array value.static <T> T
newInstance(Class<T> clazz)
Creates a new instance of the given class by invoking its default constructor.static Type
resolveTypeVariable(List<Type> context, TypeVariable<?> typeVariable)
Resolves the actual type of the given type variable that comes from a field type based on the given context list.static Object
toArray(Collection<?> collection, Class<?> componentType)
Returns a new array of the given component type (possibly a Java primitive) that is a copy of the content of the given collection.
-
-
-
Method Detail
-
getSuperParameterizedType
public static ParameterizedType getSuperParameterizedType(Type type, Class<?> superClass)
Returns the parameterized type that is or extends the given type that matches the given super class.For example, if the input type is
HashMap<String,Integer>
and the input super class isMap.class
, it will return the extended parameterized typeMap
, but which retains the actual type information from the originalHashMap
.- Parameters:
type
- class or parameterized typesuperClass
- super class- Returns:
- matching parameterized type or
null
-
isAssignableToOrFrom
public static boolean isAssignableToOrFrom(Class<?> classToCheck, Class<?> anotherClass)
Returns whether a class is either assignable to or from another class.- Parameters:
classToCheck
- class to checkanotherClass
- another class
-
newInstance
public static <T> T newInstance(Class<T> clazz)
Creates a new instance of the given class by invoking its default constructor.The given class must be public and must have a public default constructor, and must not be an array or an interface or be abstract. If an enclosing class, it must be static.
-
isArray
public static boolean isArray(Type type)
Returns whether the given type is an array.
-
getArrayComponentType
public static Type getArrayComponentType(Type array)
Returns the component type of the given array type, assumingisArray(Type)
.Return type will either be class, parameterized type, generic array type, or type variable, but not a wildcard type.
- Throws:
ClassCastException
- ifisArray(Type)
is false
-
getRawClass
public static Class<?> getRawClass(ParameterizedType parameterType)
Returns the raw class for the given parameter type as defined inParameterizedType.getRawType()
.- Parameters:
parameterType
- parameter type- Returns:
- raw class
-
getBound
public static Type getBound(WildcardType wildcardType)
Returns the only bound of the given wildcard type.- Parameters:
wildcardType
- wildcard type- Returns:
- only bound or
Object.class
for none
-
resolveTypeVariable
public static Type resolveTypeVariable(List<Type> context, TypeVariable<?> typeVariable)
Resolves the actual type of the given type variable that comes from a field type based on the given context list.In case the type variable can be resolved partially, it will return the partially resolved type variable.
- Parameters:
context
- context list, ordering from least specific to most specific type context, for example container class and then its fieldtypeVariable
- type variable- Returns:
- resolved or partially resolved actual type (type variable, class, parameterized type,
or generic array type, but not wildcard type) or
null
if unable to resolve at all
-
getRawArrayComponentType
public static Class<?> getRawArrayComponentType(List<Type> context, Type componentType)
Returns the raw array component type to use -- for example for the first parameter ofArray.newInstance(Class, int)
-- for the given component type.- Parameters:
context
- context list, ordering from least specific to most specific type context, for example container class and then its fieldcomponentType
- array component type ornull
forObject.class
result- Returns:
- raw array component type
-
getIterableParameter
public static Type getIterableParameter(Type iterableType)
Returns the type parameter ofIterable
that is assignable from the given iterable type.For example, for the type
ArrayList<Integer>
-- or for a class that extendsArrayList<Integer>
-- it will returnInteger
.- Parameters:
iterableType
- iterable type (must extendIterable
)- Returns:
- type parameter, which may be any type
-
getMapValueParameter
public static Type getMapValueParameter(Type mapType)
Returns the value type parameter ofMap
that is assignable from the given map type.For example, for the type
Map<String, Integer>
-- or for a class that extendsMap<String, Integer>
-- it will returnInteger
.- Parameters:
mapType
- map type (must extendMap
)- Returns:
- type parameter, which may be any type
-
iterableOf
public static <T> Iterable<T> iterableOf(Object value)
Returns an iterable for an input iterable or array value.If the input value extends
Iterable
, it will just return the input value. Otherwise, it will return an iterable that can handle arrays of primitive and non-primitive component type.- Parameters:
value
- iterable (extendsIterable
) or array value- Returns:
- iterable
-
toArray
public static Object toArray(Collection<?> collection, Class<?> componentType)
Returns a new array of the given component type (possibly a Java primitive) that is a copy of the content of the given collection.- Parameters:
collection
- collectioncomponentType
- component type (possibly a Java primitive)- Returns:
- new array
-
-