|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.scijava.util.GenericUtils
public final class GenericUtils
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.
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 n th 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 |
---|
public static Class<?> getClass(Type type)
public static List<Class<?>> getClasses(Type type)
For example, a type parameter A extends Number & Iterable
will
return both Number
and Iterable
as its raw classes.
public static Type getComponentType(Type type)
public static Class<?> getComponentClass(Type type)
public static Type getFieldType(Field field, Class<?> type)
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
public static List<Class<?>> getFieldClasses(Field field, Class<?> type)
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
getFieldType(Field, Class)
,
getClasses(Type)
public static Type getMethodReturnType(Method method, Class<?> type)
getFieldType(Field, Class)
, but with respect to the return
type of the given Method
rather than a Field
.
public static List<Class<?>> getMethodReturnClasses(Method method, Class<?> type)
getFieldClasses(Field, Class)
, but with respect to the return
type of the given Method
rather than a Field
.
getMethodReturnType(Method, Class)
,
getClasses(Type)
public static Type getTypeParameter(Type type, Class<?> c, int paramNo)
n
th type parameter of the specified class.
For example, with class StringList implements List<String>
,
getTypeParameter(StringList.class, Collection.class, 0)
returns
String
.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |