com.google.api.client.util
Class Types

java.lang.Object
  extended by 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
static Type getArrayComponentType(Type array)
          Returns the component type of the given array type, assuming isArray(Type).
static Type getBound(WildcardType wildcardType)
          Returns the only bound of the given wildcard type.
static Type getIterableParameter(Type iterableType)
          Returns the type parameter of Iterable that is assignable from the given iterable type.
static Type getMapValueParameter(Type mapType)
          Returns the value type parameter of Map 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 of Array.newInstance(Class, int) -- for the given component type.
static Class<?> getRawClass(ParameterizedType parameterType)
          Returns the raw class for the given parameter type as defined in ParameterizedType.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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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 is Map.class, it will return the extended parameterized type Map, but which retains the actual type information from the original HashMap.

Parameters:
type - class or parameterized type
superClass - 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 check
anotherClass - 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, assuming isArray(Type).

Return type will either be class, parameterized type, generic array type, or type variable, but not a wildcard type.

Throws:
ClassCastException - if isArray(Type) is false

getRawClass

public static Class<?> getRawClass(ParameterizedType parameterType)
Returns the raw class for the given parameter type as defined in ParameterizedType.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 field
typeVariable - 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 of Array.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 field
componentType - array component type or null for Object.class result
Returns:
raw array component type

getIterableParameter

public static Type getIterableParameter(Type iterableType)
Returns the type parameter of Iterable that is assignable from the given iterable type.

For example, for the type ArrayList<Integer> -- or for a class that extends ArrayList<Integer> -- it will return Integer.

Parameters:
iterableType - iterable type (must extend Iterable)
Returns:
type parameter, which may be any type

getMapValueParameter

public static Type getMapValueParameter(Type mapType)
Returns the value type parameter of Map that is assignable from the given map type.

For example, for the type Map<String, Integer> -- or for a class that extends Map<String, Integer> -- it will return Integer.

Parameters:
mapType - map type (must extend Map)
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 (extends Iterable) 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 - collection
componentType - component type (possibly a Java primitive)
Returns:
new array


Copyright © 2011-2012 Google. All Rights Reserved.