- java.lang.Object
-
- org.scijava.common3.Classes
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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 <T> Class<T>box(Class<T> type)Returns the non-primitiveClassclosest to the given type.static ClassLoaderclassLoader()Gets the class loader to use.static intcompare(Class<?> c1, Class<?> c2)Compares twoClassobjects using their fully qualified names.static Fieldfield(Class<?> c, String name)Gets the field with the specified name, of the given class, or superclass thereof.static booleanisBoolean(Class<?> type)static booleanisByte(Class<?> type)static booleanisCharacter(Class<?> type)static booleanisDouble(Class<?> type)static booleanisFloat(Class<?> 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 <T> TnullValue(Class<T> type)Gets the "null" value for the given type.static <T> Class<T>unbox(Class<T> type)Returns the primitiveClassclosest to the given type.
-
-
-
Method Detail
-
classLoader
public static ClassLoader classLoader()
Gets the class loader to use. This will be the current thread's context class loader if non-null; otherwise it will be the system class loader.
-
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.
-
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
-
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
-
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 useField.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 field 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 useMethod.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
-
compare
public static int compare(Class<?> c1, Class<?> c2)
Compares twoClassobjects using their fully qualified names.Note: this method provides a natural ordering that may be inconsistent with equals. Specifically, two unequal classes may return 0 when compared in this fashion if they represent the same class loaded using two different
ClassLoaders. Hence, if this method is used as a basis for implementingComparable.compareTo(T)orComparator.compare(T, T), that implementation may want to impose logic beyond that of this method, for breaking ties, if a total ordering consistent with equals is always required.
-
-