Class Classes


  • public final class Classes
    extends Object
    Useful methods for working with Class objects and primitive types.
    Author:
    Curtis Rueden
    • 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 specified ClassLoader, 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 return null (rather than throwing IllegalArgumentException) if something goes wrong loading the class.
        Returns:
        The loaded class, or null if the class could not be loaded and the quietly flag is set.
        Throws:
        IllegalArgumentException - If the class cannot be loaded and the quietly flag 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 specified ClassLoader, 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:

        • boolean is loaded as boolean.class
        • Z is loaded as boolean.class
        • double[] is loaded as double[].class
        • string[] is loaded as java.lang.String.class
        • [F is loaded as float[].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 return null (rather than throwing IllegalArgumentException) if something goes wrong loading the class
        Returns:
        The loaded class, or null if the class could not be loaded and the quietly flag is set.
        Throws:
        IllegalArgumentException - If the class cannot be loaded and the quietly flag 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 return null (rather than throwing IllegalArgumentException) if something goes wrong determining the location.
        Returns:
        URL pointing to the class, or null if the location could not be determined and the quietly flag is set.
        Throws:
        IllegalArgumentException - If the location cannot be determined and the quietly flag 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-primitive Class closest 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 primitive Class closest 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 just public. And unlike Class.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 use Field.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 just public. And unlike Class.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 use Method.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) returns double[].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. for void.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) returns double[][].class.

        Parameters:
        componentType - The type of elements which the array possesses
        dim - The dimensionality of the array
      • compare

        public static int compare​(Class<?> c1,
                                  Class<?> c2)
        Compares two Class objects 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 implementing Comparable.compareTo(T) or Comparator.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.