org.scijava.util
Class GenericUtils

java.lang.Object
  extended by org.scijava.util.GenericUtils

public final class GenericUtils
extends Object

Useful methods for working with Type objects, particularly generic types.

This class leans heavily on the excellent gentyref library, and exists mainly to keep the gentyref dependency encapsulated within SciJava Common.

Author:
Curtis Rueden
See Also:
For utility methods specific to {@link Class} objects., For utility methods that convert between {@link Type}s.

Method Summary
static Class<?> getClass(Type type)
          Gets the sole raw class corresponding to the given type, or null if none.
static List<Class<?>> getClasses(Type type)
          Gets all raw classes corresponding to the given type.
static Class<?> getComponentClass(Type type)
          Gets the sole component class of the given array type, or null if none.
static Type getComponentType(Type type)
          Gets the component type of the given array type, or null if not an array.
static List<Class<?>> getFieldClasses(Field field, Class<?> type)
          Returns the "safe" class(es) of the given field, as viewed from the specified type.
static Type getFieldType(Field field, Class<?> type)
          Returns the "safe" generic type of the given field, as viewed from the given type.
static List<Class<?>> getMethodReturnClasses(Method method, Class<?> type)
          As getFieldClasses(Field, Class), but with respect to the return type of the given Method rather than a Field.
static Type getMethodReturnType(Method method, Class<?> type)
          As getFieldType(Field, Class), but with respect to the return type of the given Method rather than a Field.
static Type getTypeParameter(Type type, Class<?> c, int paramNo)
          Gets the given type's nth type parameter of the specified class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getClass

public static Class<?> getClass(Type type)
Gets the sole raw class corresponding to the given type, or null if none.


getClasses

public static List<Class<?>> getClasses(Type type)
Gets all raw classes corresponding to the given type.

For example, a type parameter A extends Number & Iterable will return both Number and Iterable as its raw classes.


getComponentType

public static Type getComponentType(Type type)
Gets the component type of the given array type, or null if not an array.


getComponentClass

public static Class<?> getComponentClass(Type type)
Gets the sole component class of the given array type, or null if none.


getFieldType

public static Type getFieldType(Field field,
                                Class<?> type)
Returns the "safe" generic type of the given field, as viewed from the given type. This may be narrower than what Field.getGenericType() returns, if the field is declared in a superclass, or type has a type parameter that is used in the type of the field.

For example, suppose we have the following three classes:

 public class Thing<T> {
        public T thing;
 }
 
 public class NumberThing<N extends Number> extends Thing<N> { }
 
 public class IntegerThing extends NumberThing<Integer> { }
 
Then this method operates as follows:
 field = ClassUtils.getField(Thing.class, "thing");
 
 field.getType(); // Object
 field.getGenericType(); // T
 
 ClassUtils.getGenericType(field, Thing.class); // T
 ClassUtils.getGenericType(field, NumberThing.class); // N extends Number
 ClassUtils.getGenericType(field, IntegerThing.class); // Integer
 


getFieldClasses

public static List<Class<?>> getFieldClasses(Field field,
                                             Class<?> type)
Returns the "safe" class(es) of the given field, as viewed from the specified type. This may be narrower than what Field.getType() returns, if the field is declared in a superclass, or type has a type parameter that is used in the type of the field.

For example, suppose we have the following three classes:

 
 public class Thing<T> {
 
        public T thing;
 }
 
 public class NumberThing<N extends Number> extends Thing<N> {}
 
 public class IntegerThing extends NumberThing<Integer> {}
 
Then this method operates as follows:
 field = ClassUtils.getField(Thing.class, "thing");
 
 field.getType(); // Object
 
 ClassUtils.getTypes(field, Thing.class).get(0); // Object
 ClassUtils.getTypes(field, NumberThing.class).get(0); // Number
 ClassUtils.getTypes(field, IntegerThing.class).get(0); // Integer
 

In cases of complex generics which take the intersection of multiple types using the & operator, there may be multiple types returned by this method. For example:

 public class ComplexThing<T extends Serializable & Cloneable> extends Thing<T> {}
 
 ClassUtils.getTypes(field, ComplexThing.class); // Serializable, Cloneable
 

See Also:
getFieldType(Field, Class), getClasses(Type)

getMethodReturnType

public static Type getMethodReturnType(Method method,
                                       Class<?> type)
As getFieldType(Field, Class), but with respect to the return type of the given Method rather than a Field.


getMethodReturnClasses

public static List<Class<?>> getMethodReturnClasses(Method method,
                                                    Class<?> type)
As getFieldClasses(Field, Class), but with respect to the return type of the given Method rather than a Field.

See Also:
getMethodReturnType(Method, Class), getClasses(Type)

getTypeParameter

public static Type getTypeParameter(Type type,
                                    Class<?> c,
                                    int paramNo)
Gets the given type's nth type parameter of the specified class.

For example, with class StringList implements List<String>, getTypeParameter(StringList.class, Collection.class, 0) returns String.



Copyright © 2009–2014 SciJava. All rights reserved.