Class Types
- java.lang.Object
-
- org.scijava.util.Types
-
public final class Types extends Object
Utility class for working with generic types, fields and methods.Logic and inspiration were drawn from the following excellent libraries:
- Google Guava's
com.google.common.reflectpackage. - Apache Commons Lang 3's
org.apache.commons.lang3.reflectpackage. - GenTyRef (Generic Type Reflector), a library for runtime generic type introspection.
All three of these libraries contain fantastic generics-related logic, but none of the three contained everything that SciJava needed for all its use cases. Hence, we have drawn from sources as needed to create a unified generics API for use from SciJava applications. See in particular the SciJava Ops project, which utilizes these functions heavily.
NB: The
org.apache.commons.reflect.TypeUtilsclass of Apache Commons Lang version 3.4 is forked internally within this class. We did this for two reasons: 1) to avoid bringing in the whole of Apache Commons Lang as a dependency; and 2) to fix an infinite recursion bug in theTypeUtils.toString(Type)method.- Author:
- Curtis Rueden, Gabe Selzer
- Google Guava's
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Map<TypeVariable<?>,Type>args(Class<?> c, ParameterizedType superType)Tries to determine the type arguments of a class/interface based on a super parameterized type's type arguments.static Map<TypeVariable<?>,Type>args(Type type, Class<?> toClass)Gets the type arguments of a class/interface based on a subtype.static Class<?>array(Class<?> componentType)Gets the array class corresponding to the given element type.static Class<?>array(Class<?> componentType, int dim)Gets the array class corresponding to the given element type and dimensionality.static Typearray(Type componentType)Gets the array type—which might be aClassor aGenericArrayTypedepending on the argument—corresponding to the given element type.static <T> Class<T>box(Class<T> type)Returns the non-primitiveClassclosest to the given type.static <T> Tcast(Object src, Class<T> dest)Casts the given object to the specified type, or null if the types are incompatible.static Typecomponent(Type type)Gets the component type of the given array type, or null if not an array.static booleancontainsTypeVars(Type type)Learn, recursively, whether any of the type parameters associated withtypeare bound to variables.static <T> TenumFromLabel(String label, Class<T> dest)Converts the given string label to an enumeration constant of the specified type.static <T> TenumFromString(String s, Class<T> dest)Converts the given string value or label to an enumeration constant of the specified type.static <T> TenumValue(String name, Class<T> dest)Converts the given string value to an enumeration constant of the specified type.static Fieldfield(Class<?> c, String name)Gets the field with the specified name, of the given class, or superclass thereof.static TypefieldType(Field field, Class<?> type)Returns the "safe" generic type of the given field, as viewed from the given type.static booleanisAssignable(Type source, Type target)Discerns whether it would be legal to assign a reference of typesourceto a reference of typetarget.static booleanisBoolean(Class<?> type)static booleanisByte(Class<?> type)static booleanisCharacter(Class<?> type)static booleanisDouble(Class<?> type)static booleanisFloat(Class<?> type)static booleanisInstance(Object obj, Class<?> dest)Checks whether the given object can be cast to the specified type.static booleanisInteger(Class<?> type)static booleanisLong(Class<?> type)static booleanisNumber(Class<?> type)static booleanisShort(Class<?> type)static booleanisText(Class<?> type)static Class<?>load(String name)Loads the class with the given name, using the current thread's context class loader, or null if it cannot be loaded.static Class<?>load(String className, boolean quietly)Loads the class with the given name, using the current thread's context class loader.static Class<?>load(String name, ClassLoader classLoader)Loads the class with the given name, using the specifiedClassLoader, or null if it cannot be loaded.static Class<?>load(String name, ClassLoader classLoader, boolean quietly)Loads the class with the given name, using the specifiedClassLoader, or null if it cannot be loaded.static URLlocation(Class<?> c)Gets the base location of the given class.static URLlocation(Class<?> c, boolean quietly)Gets the base location of the given class.static Methodmethod(Class<?> c, String name, Class<?>... parameterTypes)Gets the method with the specified name and argument types, of the given class, or superclass thereof.static Type[]methodParamTypes(Method method, Class<?> type)AsfieldType(Field, Class), but with respect to the parameter types of the givenMethodrather than aField.static TypemethodReturnType(Method method, Class<?> type)AsfieldType(Field, Class), but with respect to the return type of the givenMethodrather than aField.static Stringname(Type t)Gets a string representation of the given type.static <T> TnullValue(Class<T> type)Gets the "null" value for the given type.static Typeparam(Type type, Class<?> c, int no)Gets the given type'snth type parameter of the specified class.static ParameterizedTypeparameterize(Class<?> rawType, Type... typeArgs)Creates a newParameterizedTypeof the given class together with the specified type arguments.static ParameterizedTypeparameterize(Class<?> raw, Map<TypeVariable<?>,Type> typeArgMappings)Create a parameterized type instance.static ParameterizedTypeparameterizeWithOwner(Type ownerType, Class<?> rawType, Type... typeArgs)Creates a newParameterizedTypeof the given class together with the specified type arguments.static Class<?>raw(Type type)Gets the (first) raw class of the given type.static List<Class<?>>raws(Type type)Gets all raw classes corresponding to the given type.static <T> Class<T>unbox(Class<T> type)Returns the primitiveClassclosest to the given type.static WildcardTypewildcard()Creates a newWildcardTypewith no upper or lower bounds (i.e.,?).static WildcardTypewildcard(Type[] upperBounds, Type[] lowerBounds)Creates a newWildcardTypewith the given upper and/or lower bounds.static WildcardTypewildcard(Type upperBound, Type lowerBound)Creates a newWildcardTypewith the given upper and/or lower bound.
-
-
-
Method Detail
-
load
public static Class<?> load(String name)
Loads the class with the given name, using the current thread's context class loader, or null if it cannot be loaded.- Parameters:
name- The name of the class to load.- Returns:
- The loaded class, or null if the class could not be loaded.
- See Also:
load(String, ClassLoader, boolean)
-
load
public static Class<?> load(String name, ClassLoader classLoader)
Loads the class with the given name, using the specifiedClassLoader, or null if it cannot be loaded.- Parameters:
name- The name of the class to load.classLoader- The class loader with which to load the class; if null, the current thread's context class loader will be used.- Returns:
- The loaded class, or null if the class could not be loaded.
- See Also:
load(String, ClassLoader, boolean)
-
load
public static Class<?> load(String className, boolean quietly)
Loads the class with the given name, using the current thread's context class loader.- Parameters:
className- the name of the class to load.quietly- Whether to returnnull(rather than throwingIllegalArgumentException) if something goes wrong loading the class.- Returns:
- The loaded class, or
nullif the class could not be loaded and thequietlyflag is set. - Throws:
IllegalArgumentException- If the class cannot be loaded and thequietlyflag is not set.- See Also:
load(String, ClassLoader, boolean)
-
load
public static Class<?> load(String name, ClassLoader classLoader, boolean quietly)
Loads the class with the given name, using the specifiedClassLoader, or null if it cannot be loaded.This method is capable of parsing several different class name syntaxes. In particular, array classes (including primitives) represented using either square brackets or internal Java array name syntax are supported. Examples:
booleanis loaded asboolean.classZis loaded asboolean.classdouble[]is loaded asdouble[].classstring[]is loaded asjava.lang.String.class[Fis loaded asfloat[].class
- Parameters:
name- The name of the class to load.classLoader- The class loader with which to load the class; if null, the current thread's context class loader will be used.quietly- Whether to returnnull(rather than throwingIllegalArgumentException) if something goes wrong loading the class- Returns:
- The loaded class, or
nullif the class could not be loaded and thequietlyflag is set. - Throws:
IllegalArgumentException- If the class cannot be loaded and thequietlyflag is not set.
-
location
public static URL location(Class<?> c)
Gets the base location of the given class.- Parameters:
c- The class whose location is desired.- Returns:
- URL pointing to the class, or null if the location could not be determined.
- See Also:
location(Class, boolean)
-
location
public static URL location(Class<?> c, boolean quietly)
Gets the base location of the given class.If the class is directly on the file system (e.g., "/path/to/my/package/MyClass.class") then it will return the base directory (e.g., "file:/path/to").
If the class is within a JAR file (e.g., "/path/to/my-jar.jar!/my/package/MyClass.class") then it will return the path to the JAR (e.g., "file:/path/to/my-jar.jar").
- Parameters:
c- The class whose location is desired.quietly- Whether to returnnull(rather than throwingIllegalArgumentException) if something goes wrong determining the location.- Returns:
- URL pointing to the class, or null if the location could not be
determined and the
quietlyflag is set. - Throws:
IllegalArgumentException- If the location cannot be determined and thequietlyflag is not set.- See Also:
to convert the result to a .
-
name
public static String name(Type t)
Gets a string representation of the given type.- Parameters:
t- Type whose name is desired.- Returns:
- The name of the given type.
-
raw
public static Class<?> raw(Type type)
Gets the (first) raw class of the given type.- If the type is a
Classitself, the type itself is returned. - If the type is a
ParameterizedType, the raw type of the parameterized type is returned. - If the type is a
GenericArrayType, the returned type is the corresponding array class. For example:List<Integer>[] => List[]. - If the type is a type variable or wildcard type, the raw type of the
first upper bound is returned. For example:
<X extends Foo & Bar> => Foo.
If you want all raw classes of the given type, use
raws(java.lang.reflect.Type).- Parameters:
type- The type from which to discern the (first) raw class.- Returns:
- The type's first raw class.
- If the type is a
-
raws
public static List<Class<?>> raws(Type type)
Gets all raw classes corresponding to the given type.For example, a type parameter
A extends Number & Iterablewill return bothNumberandIterableas its raw classes.- Parameters:
type- The type from which to discern the raw classes.- Returns:
- List of the type's raw classes.
- See Also:
raw(java.lang.reflect.Type)
-
isBoolean
public static boolean isBoolean(Class<?> type)
-
isByte
public static boolean isByte(Class<?> type)
-
isCharacter
public static boolean isCharacter(Class<?> type)
-
isDouble
public static boolean isDouble(Class<?> type)
-
isFloat
public static boolean isFloat(Class<?> type)
-
isInteger
public static boolean isInteger(Class<?> type)
-
isLong
public static boolean isLong(Class<?> type)
-
isShort
public static boolean isShort(Class<?> type)
-
isNumber
public static boolean isNumber(Class<?> type)
-
isText
public static boolean isText(Class<?> type)
-
box
public static <T> Class<T> box(Class<T> type)
Returns the non-primitiveClassclosest to the given type.Specifically, the following type conversions are done:
- boolean.class becomes Boolean.class
- byte.class becomes Byte.class
- char.class becomes Character.class
- double.class becomes Double.class
- float.class becomes Float.class
- int.class becomes Integer.class
- long.class becomes Long.class
- short.class becomes Short.class
- void.class becomes Void.class
All other types are unchanged.
-
unbox
public static <T> Class<T> unbox(Class<T> type)
Returns the primitiveClassclosest to the given type.Specifically, the following type conversions are done:
- Boolean.class becomes boolean.class
- Byte.class becomes byte.class
- Character.class becomes char.class
- Double.class becomes double.class
- Float.class becomes float.class
- Integer.class becomes int.class
- Long.class becomes long.class
- Short.class becomes short.class
- Void.class becomes void.class
All other types are unchanged.
-
nullValue
public static <T> T nullValue(Class<T> type)
Gets the "null" value for the given type. For non-primitives, this will actually be null. For primitives, it will be zero for numeric types, false for boolean, and the null character for char.
-
field
public static Field field(Class<?> c, String name)
Gets the field with the specified name, of the given class, or superclass thereof.Unlike
Class.getField(String), this method will return fields of any visibility, not justpublic. And unlikeClass.getDeclaredField(String), it will do so recursively, returning the first field of the given name from the class's superclass hierarchy.Note that this method does not guarantee that the returned field is accessible; if the field is not
public, calling code will need to useAccessibleObject.setAccessible(boolean)in order to manipulate the field's contents.- Parameters:
c- The class (or subclass thereof) containing the desired field.name-- Returns:
- The first field with the given name in the class's superclass hierarchy.
- Throws:
IllegalArgumentException- if the specified class does not contain a method with the given name
-
method
public static Method method(Class<?> c, String name, Class<?>... parameterTypes)
Gets the method with the specified name and argument types, of the given class, or superclass thereof.Unlike
Class.getMethod(String, Class[]), this method will return methods of any visibility, not justpublic. And unlikeClass.getDeclaredMethod(String, Class[]), it will do so recursively, returning the first method of the given name and argument types from the class's superclass hierarchy.Note that this method does not guarantee that the returned method is accessible; if the method is not
public, calling code will need to useAccessibleObject.setAccessible(boolean)in order to invoke the method.- Parameters:
c- The class (or subclass thereof) containing the desired method.name- Name of the method.parameterTypes- Types of the method parameters.- Returns:
- The first method with the given name and argument types in the class's superclass hierarchy.
- Throws:
IllegalArgumentException- If the specified class does not contain a method with the given name and argument types.
-
array
public static Class<?> array(Class<?> componentType)
Gets the array class corresponding to the given element type.For example,
arrayType(double.class)returnsdouble[].class.- Parameters:
componentType- The type of elements which the array possesses- Throws:
IllegalArgumentException- if the type cannot be the component type of an array (this is the case e.g. forvoid.class).
-
array
public static Class<?> array(Class<?> componentType, int dim)
Gets the array class corresponding to the given element type and dimensionality.For example,
arrayType(double.class, 2)returnsdouble[][].class.- Parameters:
componentType- The type of elements which the array possessesdim- The dimensionality of the array
-
array
public static Type array(Type componentType)
Gets the array type—which might be aClassor aGenericArrayTypedepending on the argument—corresponding to the given element type.For example,
arrayType(double.class)returnsdouble[].class.- Parameters:
componentType- The type of elements which the array possesses- See Also:
component(java.lang.reflect.Type)
-
component
public static Type component(Type type)
Gets the component type of the given array type, or null if not an array.If you have a
Class, you can callClass.getComponentType()for a narrower return type.This is the opposite of
array(Type).
-
fieldType
public static Type fieldType(Field field, Class<?> type)
Returns the "safe" generic type of the given field, as viewed from the given type. This may be narrower than whatField.getGenericType()returns, if the field is declared in a superclass, ortypehas 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 = Types.field(Thing.class, "thing"); field.getType(); // Object field.getGenericType(); // T Types.fieldType(field, Thing.class); // T Types.fieldType(field, NumberThing.class); // N extends Number Types.fieldType(field, IntegerThing.class); // Integer
-
methodReturnType
public static Type methodReturnType(Method method, Class<?> type)
AsfieldType(Field, Class), but with respect to the return type of the givenMethodrather than aField.
-
methodParamTypes
public static Type[] methodParamTypes(Method method, Class<?> type)
AsfieldType(Field, Class), but with respect to the parameter types of the givenMethodrather than aField.
-
param
public static Type param(Type type, Class<?> c, int no)
Gets the given type'snth type parameter of the specified class.For example, with class
StringList implements List<String>,Types.param(StringList.class, Collection.class, 0)returnsString.
-
isAssignable
public static boolean isAssignable(Type source, Type target)
Discerns whether it would be legal to assign a reference of typesourceto a reference of typetarget.- Parameters:
source- The type from which assignment is desired.target- The type to which assignment is desired.- Returns:
- True if the source is assignable to the target.
- Throws:
NullPointerException- iftargetis null.- See Also:
Class.isAssignableFrom(Class)
-
isInstance
public static boolean isInstance(Object obj, Class<?> dest)
Checks whether the given object can be cast to the specified type.- Returns:
- true If the destination class is assignable from the source object's class, or if the source object is null and destination class is non-null.
- See Also:
cast(Object, Class)
-
cast
public static <T> T cast(Object src, Class<T> dest)
Casts the given object to the specified type, or null if the types are incompatible.
-
enumValue
public static <T> T enumValue(String name, Class<T> dest)
Converts the given string value to an enumeration constant of the specified type. For example,enumValue("APPLE", Fruit.class)returnsFruit.APPLEif such a value is among those of the requested enum class.- Parameters:
name- The value to convert.dest- The type of the enumeration constant.- Returns:
- The converted enumeration constant.
- Throws:
IllegalArgumentException- if the type is not an enumeration type, or has no such constant.
-
enumFromLabel
public static <T> T enumFromLabel(String label, Class<T> dest)
Converts the given string label to an enumeration constant of the specified type. An enum label is the string returned by the enum constant'sObject.toString()method. For example,enumFromLabel("Apple", Fruit.class)returnsFruit.APPLEifFruit.APPLE.toString()is implemented to return"Apple".- Parameters:
label- ThetoString()result of the desired enum value.dest- The type of the enumeration constant.- Returns:
- The matching enumeration constant.
- Throws:
IllegalArgumentException- if the type is not an enumeration type, or has no constant with the given label.
-
enumFromString
public static <T> T enumFromString(String s, Class<T> dest)
Converts the given string value or label to an enumeration constant of the specified type.If the string matches one of the enum values directly, that value will be returned via
enumValue(String, Class). Otherwise, the result ofenumFromLabel(java.lang.String, java.lang.Class<T>)is returned.- Parameters:
s- The name or label of the desired enum value.dest- The type of the enumeration constant.- Returns:
- The matching enumeration constant.
- Throws:
IllegalArgumentException- if the type is not an enumeration type, or has no such constant with the given name nor label.
-
parameterize
public static ParameterizedType parameterize(Class<?> rawType, Type... typeArgs)
Creates a newParameterizedTypeof the given class together with the specified type arguments.- Parameters:
rawType- The class of theParameterizedType.typeArgs- The type arguments to use in parameterizing it.- Returns:
- The newly created
ParameterizedType.
-
parameterizeWithOwner
public static ParameterizedType parameterizeWithOwner(Type ownerType, Class<?> rawType, Type... typeArgs)
Creates a newParameterizedTypeof the given class together with the specified type arguments.- Parameters:
ownerType- The owner type of the parameterized class.rawType- The class of theParameterizedType.typeArgs- The type arguments to use in parameterizing it.- Returns:
- The newly created
ParameterizedType.
-
wildcard
public static WildcardType wildcard()
Creates a newWildcardTypewith no upper or lower bounds (i.e.,?).- Returns:
- The newly created
WildcardType.
-
wildcard
public static WildcardType wildcard(Type upperBound, Type lowerBound)
Creates a newWildcardTypewith the given upper and/or lower bound.- Parameters:
upperBound- Upper bound of the wildcard, or null for none.lowerBound- Lower bound of the wildcard, or null for none.- Returns:
- The newly created
WildcardType.
-
wildcard
public static WildcardType wildcard(Type[] upperBounds, Type[] lowerBounds)
Creates a newWildcardTypewith the given upper and/or lower bounds.- Parameters:
upperBounds- Upper bounds of the wildcard, or null for none.lowerBounds- Lower bounds of the wildcard, or null for none.- Returns:
- The newly created
WildcardType.
-
containsTypeVars
public static boolean containsTypeVars(Type type)
Learn, recursively, whether any of the type parameters associated withtypeare bound to variables.- Parameters:
type- the type to check for type variables- Returns:
- boolean
-
args
public static Map<TypeVariable<?>,Type> args(Type type, Class<?> toClass)
Gets the type arguments of a class/interface based on a subtype. For instance, this method will determine that both of the parameters for the interfaceMapareObjectfor the subtypePropertieseven though the subtype does not directly implement theMapinterface.This method returns
nulliftypeis not assignable totoClass. It returns an empty map if none of the classes or interfaces in its inheritance hierarchy specify any type arguments.A side effect of this method is that it also retrieves the type arguments for the classes and interfaces that are part of the hierarchy between
typeandtoClass. So with the above example, this method will also determine that the type arguments forHashtableare also bothObject. In cases where the interface specified bytoClassis (indirectly) implemented more than once (e.g. wheretoClassspecifies the interfaceIterableandtypespecifies a parameterized type that implements bothSetandCollection), this method will look at the inheritance hierarchy of only one of the implementations/subclasses; the first interface encountered that isn't a subinterface to one of the others in thetypetotoClasshierarchy.- Parameters:
type- the type from which to determine the type parameters oftoClasstoClass- the class whose type parameters are to be determined based on the subtypetype- Returns:
- a
Mapof the type assignments for the type variables in each type in the inheritance hierarchy fromtypetotoClassinclusive.
-
args
public static Map<TypeVariable<?>,Type> args(Class<?> c, ParameterizedType superType)
Tries to determine the type arguments of a class/interface based on a super parameterized type's type arguments. This method is the inverse ofargs(Type, Class)which gets a class/interface's type arguments based on a subtype. It is far more limited in determining the type arguments for the subject class's type variables in that it can only determine those parameters that map from the subjectClassobject to the supertype.Example:
TreeSetsets its parameter as the parameter forNavigableSet, which in turn sets the parameter ofSortedSet, which in turn sets the parameter ofSet, which in turn sets the parameter ofCollection, which in turn sets the parameter ofIterable. SinceTreeSet's parameter maps (indirectly) toIterable's parameter, it will be able to determine that based on the super typeIterable<? extends Map<Integer, ? extends Collection<?>>>, the parameter ofTreeSetis? extends Map<Integer, ? extends Collection<?>>.- Parameters:
c- the class whose type parameters are to be determined, notnullsuperType- the super type from whichc's type arguments are to be determined, notnull- Returns:
- a
Mapof the type assignments that could be determined for the type variables in each type in the inheritance hierarchy fromtypetocinclusive.
-
parameterize
public static final ParameterizedType parameterize(Class<?> raw, Map<TypeVariable<?>,Type> typeArgMappings)
Create a parameterized type instance.- Parameters:
raw- the raw class to create a parameterized type instance fortypeArgMappings- the mapping used for parameterization- Returns:
ParameterizedType
-
-