Class Types
- java.lang.Object
-
- com.google.api.client.util.Types
-
public class Types extends java.lang.Object
Utilities for working with Java types.- Since:
- 1.4
-
-
Constructor Summary
Constructors Modifier Constructor Description private
Types()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static java.lang.reflect.Type
getActualParameterAtPosition(java.lang.reflect.Type type, java.lang.Class<?> superClass, int position)
static java.lang.reflect.Type
getArrayComponentType(java.lang.reflect.Type array)
Returns the component type of the given array type, assumingisArray(Type)
.static java.lang.reflect.Type
getBound(java.lang.reflect.WildcardType wildcardType)
Returns the only bound of the given wildcard type.static java.lang.reflect.Type
getIterableParameter(java.lang.reflect.Type iterableType)
Returns the type parameter ofIterable
that is assignable from the given iterable type.static java.lang.reflect.Type
getMapValueParameter(java.lang.reflect.Type mapType)
Returns the value type parameter ofMap
that is assignable from the given map type.static java.lang.Class<?>
getRawArrayComponentType(java.util.List<java.lang.reflect.Type> context, java.lang.reflect.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 java.lang.Class<?>
getRawClass(java.lang.reflect.ParameterizedType parameterType)
Returns the raw class for the given parameter type as defined inParameterizedType.getRawType()
.static java.lang.reflect.ParameterizedType
getSuperParameterizedType(java.lang.reflect.Type type, java.lang.Class<?> superClass)
Returns the parameterized type that is or extends the given type that matches the given super class.private static java.lang.IllegalArgumentException
handleExceptionForNewInstance(java.lang.Exception e, java.lang.Class<?> clazz)
static boolean
isArray(java.lang.reflect.Type type)
Returns whether the given type is an array.static boolean
isAssignableToOrFrom(java.lang.Class<?> classToCheck, java.lang.Class<?> anotherClass)
Returns whether a class is either assignable to or from another class.static <T> java.lang.Iterable<T>
iterableOf(java.lang.Object value)
Returns an iterable for an input iterable or array value.static <T> T
newInstance(java.lang.Class<T> clazz)
Creates a new instance of the given class by invoking its default constructor.static java.lang.reflect.Type
resolveTypeVariable(java.util.List<java.lang.reflect.Type> context, java.lang.reflect.TypeVariable<?> typeVariable)
Resolves the actual type of the given type variable that comes from a field type based on the given context list.static java.lang.Object
toArray(java.util.Collection<?> collection, java.lang.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 java.lang.reflect.ParameterizedType getSuperParameterizedType(java.lang.reflect.Type type, java.lang.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(java.lang.Class<?> classToCheck, java.lang.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(java.lang.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.
-
handleExceptionForNewInstance
private static java.lang.IllegalArgumentException handleExceptionForNewInstance(java.lang.Exception e, java.lang.Class<?> clazz)
-
isArray
public static boolean isArray(java.lang.reflect.Type type)
Returns whether the given type is an array.
-
getArrayComponentType
public static java.lang.reflect.Type getArrayComponentType(java.lang.reflect.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:
java.lang.ClassCastException
- ifisArray(Type)
is false
-
getRawClass
public static java.lang.Class<?> getRawClass(java.lang.reflect.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 java.lang.reflect.Type getBound(java.lang.reflect.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 java.lang.reflect.Type resolveTypeVariable(java.util.List<java.lang.reflect.Type> context, java.lang.reflect.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 java.lang.Class<?> getRawArrayComponentType(java.util.List<java.lang.reflect.Type> context, java.lang.reflect.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 java.lang.reflect.Type getIterableParameter(java.lang.reflect.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 java.lang.reflect.Type getMapValueParameter(java.lang.reflect.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
-
getActualParameterAtPosition
private static java.lang.reflect.Type getActualParameterAtPosition(java.lang.reflect.Type type, java.lang.Class<?> superClass, int position)
-
iterableOf
public static <T> java.lang.Iterable<T> iterableOf(java.lang.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 java.lang.Object toArray(java.util.Collection<?> collection, java.lang.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
-
-