Class Classes


  • public final class Classes
    extends java.lang.Object
    A collection of Class utilities.
    Version:
    $Revision$
    • Constructor Summary

      Constructors 
      Constructor Description
      Classes()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.lang.Class convertToJavaClass​(java.lang.String name, java.lang.ClassLoader cl)
      Convert a given String into the appropriate Class.
      static java.lang.Class<?>[] convertToJavaClasses​(java.util.Iterator<java.lang.String> it, java.lang.ClassLoader cl)
      Convert a list of Strings from an Interator into an array of Classes (the Strings are taken as classnames).
      static void describe​(java.lang.StringBuffer buffer, java.lang.Class clazz)
      Describe the class
      static void describe​(java.lang.StringBuffer buffer, java.lang.Object object)
      Describe the class of an object
      static void displayClassInfo​(java.lang.Class clazz, java.lang.StringBuffer results)
      Format a string buffer containing the Class, Interfaces, CodeSource, and ClassLoader information for the given object clazz.
      static void forceLoad​(java.lang.Class type)
      Force the given class to be loaded fully.
      static void getAllInterfaces​(java.util.List allIfaces, java.lang.Class c)
      Populates a list with all the interfaces implemented by the argument class c and all its superclasses.
      static java.lang.Class[] getAllUniqueInterfaces​(java.lang.Class c)
      Returns an array containing all the unique interfaces implemented by the argument class c and all its superclasses.
      static java.lang.reflect.Method getAttributeGetter​(java.lang.Class cls, java.lang.String attr)
      Returns attribute's getter method.
      static java.lang.reflect.Method getAttributeSetter​(java.lang.Class cls, java.lang.String attr, java.lang.Class type)
      Returns attribute's setter method.
      static java.net.URL[] getClassLoaderURLs​(java.lang.ClassLoader cl)
      Use reflection to access a URL[] getURLs or URL[] getClasspath method so that non-URLClassLoader class loaders, or class loaders that override getURLs to return null or empty, can provide the true classpath info.
      static java.lang.String getDescription​(java.lang.Object object)
      Describe the class of an object
      static java.lang.String getPackageName​(java.lang.Class type)
      Get the package name of the specified class.
      static java.lang.String getPackageName​(java.lang.String classname)
      Get the package name of the specified class.
      static java.lang.Class getPrimitive​(java.lang.Class wrapper)  
      static java.lang.Class getPrimitiveTypeForName​(java.lang.String name)
      Get the primitive type for the given primitive name.
      static java.lang.Class getPrimitiveWrapper​(java.lang.Class type)
      Get the wrapper class for the given primitive type.
      private static java.lang.String getProperty​(java.lang.String name, java.lang.String defaultValue)
      Get a system property
      static java.lang.Object instantiate​(java.lang.Class expected, java.lang.String property, java.lang.String defaultClassName)
      Instantiate a java class object
      static boolean isPrimitive​(java.lang.Class type)
      Check if the given class is a primitive class or a primitive wrapper class.
      static boolean isPrimitive​(java.lang.String type)
      Check type against boolean, byte, char, short, int, long, float, double.
      static boolean isPrimitiveWrapper​(java.lang.Class type)
      Check if the given class is a primitive wrapper class.
      static java.lang.Class loadClass​(java.lang.String className)
      This method acts equivalently to invoking Thread.currentThread().getContextClassLoader().loadClass(className); but it also supports primitive types and array classes of object types or primitive types.
      static java.lang.Class loadClass​(java.lang.String className, java.lang.ClassLoader classLoader)
      This method acts equivalently to invoking classLoader.loadClass(className) but it also supports primitive types and array classes of object types or primitive types.
      static java.lang.String stripPackageName​(java.lang.Class type)
      Get the short name of the specified class by striping off the package name.
      static java.lang.String stripPackageName​(java.lang.String classname)
      Get the short name of the specified class by striping off the package name.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • PACKAGE_SEPARATOR

        public static final java.lang.String PACKAGE_SEPARATOR
        The string used to separator packages
        See Also:
        Constant Field Values
      • PACKAGE_SEPARATOR_CHAR

        public static final char PACKAGE_SEPARATOR_CHAR
        The characther used to separator packages
        See Also:
        Constant Field Values
      • DEFAULT_PACKAGE_NAME

        public static final java.lang.String DEFAULT_PACKAGE_NAME
        The default package name.
        See Also:
        Constant Field Values
      • PRIMITIVE_NAME_TYPE_MAP

        private static final java.util.Map PRIMITIVE_NAME_TYPE_MAP
        Primitive type name -> class map.
      • PRIMITIVE_WRAPPER_MAP

        private static final java.lang.Class[] PRIMITIVE_WRAPPER_MAP
        Map of primitive types to their wrapper classes
    • Constructor Detail

      • Classes

        public Classes()
    • Method Detail

      • displayClassInfo

        public static void displayClassInfo​(java.lang.Class clazz,
                                            java.lang.StringBuffer results)
        Format a string buffer containing the Class, Interfaces, CodeSource, and ClassLoader information for the given object clazz.
        Parameters:
        clazz - the Class
        results - - the buffer to write the info to
      • getClassLoaderURLs

        public static java.net.URL[] getClassLoaderURLs​(java.lang.ClassLoader cl)
        Use reflection to access a URL[] getURLs or URL[] getClasspath method so that non-URLClassLoader class loaders, or class loaders that override getURLs to return null or empty, can provide the true classpath info.
        Parameters:
        cl -
        Returns:
        the urls
      • getDescription

        public static java.lang.String getDescription​(java.lang.Object object)
        Describe the class of an object
        Parameters:
        object - the object
        Returns:
        the description
      • describe

        public static void describe​(java.lang.StringBuffer buffer,
                                    java.lang.Object object)
        Describe the class of an object
        Parameters:
        buffer - the string buffer
        object - the object
      • describe

        public static void describe​(java.lang.StringBuffer buffer,
                                    java.lang.Class clazz)
        Describe the class
        Parameters:
        buffer - the string buffer
        clazz - the clazz
      • stripPackageName

        public static java.lang.String stripPackageName​(java.lang.String classname)
        Get the short name of the specified class by striping off the package name.
        Parameters:
        classname - Class name.
        Returns:
        Short class name.
      • stripPackageName

        public static java.lang.String stripPackageName​(java.lang.Class type)
        Get the short name of the specified class by striping off the package name.
        Parameters:
        type - Class name.
        Returns:
        Short class name.
      • getPackageName

        public static java.lang.String getPackageName​(java.lang.String classname)
        Get the package name of the specified class.
        Parameters:
        classname - Class name.
        Returns:
        Package name or "" if the classname is in the default package.
        Throws:
        EmptyStringException - Classname is an empty string.
      • getPackageName

        public static java.lang.String getPackageName​(java.lang.Class type)
        Get the package name of the specified class.
        Parameters:
        type - Class.
        Returns:
        Package name.
      • forceLoad

        public static void forceLoad​(java.lang.Class type)
        Force the given class to be loaded fully.

        This method attempts to locate a static method on the given class the attempts to invoke it with dummy arguments in the hope that the virtual machine will prepare the class for the method call and call all of the static class initializers.

        Parameters:
        type - Class to force load.
        Throws:
        NullArgumentException - Type is null.
      • getPrimitiveTypeForName

        public static java.lang.Class getPrimitiveTypeForName​(java.lang.String name)
        Get the primitive type for the given primitive name.

        For example, "boolean" returns Boolean.TYPE and so on...

        Parameters:
        name - Primitive type name (boolean, int, byte, ...)
        Returns:
        Primitive type or null.
        Throws:
        java.lang.IllegalArgumentException - Type is not a primitive class
      • getPrimitiveWrapper

        public static java.lang.Class getPrimitiveWrapper​(java.lang.Class type)
        Get the wrapper class for the given primitive type.
        Parameters:
        type - Primitive class.
        Returns:
        Wrapper class for primitive.
        Throws:
        java.lang.IllegalArgumentException - Type is not a primitive class
      • getAllInterfaces

        public static void getAllInterfaces​(java.util.List allIfaces,
                                            java.lang.Class c)
        Populates a list with all the interfaces implemented by the argument class c and all its superclasses.
        Parameters:
        allIfaces - - the list to populate with the interfaces
        c - - the class to start scanning for interfaces
      • getAllUniqueInterfaces

        public static java.lang.Class[] getAllUniqueInterfaces​(java.lang.Class c)
        Returns an array containing all the unique interfaces implemented by the argument class c and all its superclasses. Interfaces that appear multiple times through inheritence are only accounted for once.
        Parameters:
        c - - the class to start scanning for interfaces
        Returns:
        the interfaces
      • isPrimitiveWrapper

        public static boolean isPrimitiveWrapper​(java.lang.Class type)
        Check if the given class is a primitive wrapper class.
        Parameters:
        type - Class to check.
        Returns:
        True if the class is a primitive wrapper.
      • isPrimitive

        public static boolean isPrimitive​(java.lang.Class type)
        Check if the given class is a primitive class or a primitive wrapper class.
        Parameters:
        type - Class to check.
        Returns:
        True if the class is a primitive or primitive wrapper.
      • isPrimitive

        public static boolean isPrimitive​(java.lang.String type)
        Check type against boolean, byte, char, short, int, long, float, double.
        Parameters:
        type - The java type name
        Returns:
        true if this is a primative type name.
      • getPrimitive

        public static java.lang.Class getPrimitive​(java.lang.Class wrapper)
        Parameters:
        wrapper - a primitive wrapper type
        Returns:
        primitive type the passed in wrapper type corresponds to
      • instantiate

        public static java.lang.Object instantiate​(java.lang.Class expected,
                                                   java.lang.String property,
                                                   java.lang.String defaultClassName)
        Instantiate a java class object
        Parameters:
        expected - the expected class type
        property - the system property defining the class
        defaultClassName - the default class name
        Returns:
        the instantiated object
      • loadClass

        public static java.lang.Class loadClass​(java.lang.String className)
                                         throws java.lang.ClassNotFoundException
        This method acts equivalently to invoking Thread.currentThread().getContextClassLoader().loadClass(className); but it also supports primitive types and array classes of object types or primitive types.
        Parameters:
        className - the qualified name of the class or the name of primitive type or array in the same format as returned by the java.lang.Class.getName() method.
        Returns:
        the Class object for the requested className
        Throws:
        java.lang.ClassNotFoundException - when the classLoader can not find the requested class
      • loadClass

        public static java.lang.Class loadClass​(java.lang.String className,
                                                java.lang.ClassLoader classLoader)
                                         throws java.lang.ClassNotFoundException
        This method acts equivalently to invoking classLoader.loadClass(className) but it also supports primitive types and array classes of object types or primitive types.
        Parameters:
        className - the qualified name of the class or the name of primitive type or array in the same format as returned by the java.lang.Class.getName() method.
        classLoader - the ClassLoader used to load classes
        Returns:
        the Class object for the requested className
        Throws:
        java.lang.ClassNotFoundException - when the classLoader can not find the requested class
      • convertToJavaClasses

        public static final java.lang.Class<?>[] convertToJavaClasses​(java.util.Iterator<java.lang.String> it,
                                                                      java.lang.ClassLoader cl)
                                                               throws java.lang.ClassNotFoundException
        Convert a list of Strings from an Interator into an array of Classes (the Strings are taken as classnames).
        Parameters:
        it - A java.util.Iterator pointing to a Collection of Strings
        cl - The ClassLoader to use
        Returns:
        Array of Classes
        Throws:
        java.lang.ClassNotFoundException - When a class could not be loaded from the specified ClassLoader
      • getAttributeGetter

        public static final java.lang.reflect.Method getAttributeGetter​(java.lang.Class cls,
                                                                        java.lang.String attr)
                                                                 throws java.lang.NoSuchMethodException
        Returns attribute's getter method. If the method not found then NoSuchMethodException will be thrown.
        Parameters:
        cls - the class the attribute belongs too
        attr - the attribute's name
        Returns:
        attribute's getter method
        Throws:
        java.lang.NoSuchMethodException - if the getter was not found
      • getAttributeSetter

        public static final java.lang.reflect.Method getAttributeSetter​(java.lang.Class cls,
                                                                        java.lang.String attr,
                                                                        java.lang.Class type)
                                                                 throws java.lang.NoSuchMethodException
        Returns attribute's setter method. If the method not found then NoSuchMethodException will be thrown.
        Parameters:
        cls - the class the attribute belongs to
        attr - the attribute's name
        type - the attribute's type
        Returns:
        attribute's setter method
        Throws:
        java.lang.NoSuchMethodException - if the setter was not found
      • convertToJavaClass

        private static final java.lang.Class convertToJavaClass​(java.lang.String name,
                                                                java.lang.ClassLoader cl)
                                                         throws java.lang.ClassNotFoundException
        Convert a given String into the appropriate Class.
        Parameters:
        name - Name of class
        cl - ClassLoader to use
        Returns:
        The class for the given name
        Throws:
        java.lang.ClassNotFoundException - When the class could not be found by the specified ClassLoader
      • getProperty

        private static java.lang.String getProperty​(java.lang.String name,
                                                    java.lang.String defaultValue)
        Get a system property
        Parameters:
        name - the property name
        defaultValue - the default value