Class Classes


  • public class Classes
    extends java.lang.Object
    Utilities to handle java.lang.Class
    Author:
    tomyeh
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int B_GET
      Search the get method; not imply B_METHODONLY.
      static int B_METHOD_ONLY
      Only search for methods; excluding fields.
      static int B_PUBLIC_ONLY
      Only search for public methods or fields.
      static int B_SET
      Search the set method; not imply B_METHODONLY.
    • Constructor Summary

      Constructors 
      Constructor Description
      Classes()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Object coerce​(java.lang.Class<?> cls, java.lang.Object val)
      Converts an object to the specified class.
      static java.lang.Object coerce​(java.lang.Class<?> cls, java.lang.Object val, boolean nullable)
      Converts to the specified type.
      static void configureContextClassLoader()
      Sets the customized context ClassLoader name by the library property of org.zkoss.lang.contextClassLoader.class.
      static boolean containsMethod​(java.lang.Class<?> cls, java.lang.String name, java.lang.Class<?>[] paramTypes)
      Tests whether a class contains the specified method.
      static java.lang.String correctFieldName​(java.lang.String name)
      Corrects a string to a valid Java name.
      static boolean existsByThread​(java.lang.String clsnm)
      Returns whether the specified class exists for the current thread's context class loader.
      static java.lang.Class<?> forNameByThread​(java.lang.String clsName)
      Returns the Class object of the specified class name, using the current thread's context class loader and the class will be initialized by default.
      static java.lang.Class<?> forNameByThread​(java.lang.String clsName, boolean initialize)
      Returns the Class object of the specified class name, using the current thread's context class loader.
      static java.lang.reflect.AccessibleObject getAccessibleObject​(java.lang.Class<?> cls, java.lang.String name, java.lang.Class<?>[] argTypes, int flags)
      Gets the specified accessible object, either a method or a field, by searching the specified name.
      static java.lang.Class<?>[] getAllInterfaces​(java.lang.Class<?> cls)
      Returns all interfaces that are implemented by the specified class.
      static java.lang.reflect.Field getAnyField​(java.lang.Class<?> cls, java.lang.String name)
      Gets the specified field by searching all fields including any access control and any base class.
      static java.lang.reflect.Method getAnyMethod​(java.lang.Class<?> cls, java.lang.String name, java.lang.Class<?>[] argTypes)
      Gets the specified method by searching all methods including any access control and any base class.
      static java.lang.reflect.Method getCloseMethod​(java.lang.Class<?> cls, java.lang.String name, java.lang.Class<?>[] argTypes)
      Gets one of the close methods -- a close method is a method with the same name and the compatible argument type.
      static java.lang.reflect.Method getCloseMethodBySubclass​(java.lang.Class<?> cls, java.lang.String name, java.lang.Class<?>[] argTypes)
      Like getCloseMethod(java.lang.Class<?>, java.lang.String, java.lang.Class<?>[]) to get a 'close' method, but it look for subclass of the argument (instead of superclass).
      static java.lang.reflect.Method[] getCloseMethods​(java.lang.Class<?> cls, java.lang.String name, java.lang.Class<?>[] argTypes)
      Returns all close methods that match the specified condition, or a zero-length array if none is found.
      static java.lang.reflect.Method[] getCloseMethodsBySubclass​(java.lang.Class<?> cls, java.lang.String name, java.lang.Class<?>[] argTypes)
      Like getCloseMethods(java.lang.Class<?>, java.lang.String, java.lang.Class<?>[]) to get all 'close' methods, but it look for subclass of the argument (instead of superclass).
      static java.lang.ClassLoader getContextClassLoader​(java.lang.Class<?> reference)
      Returns the context ClassLoader for the reference class.
      static java.lang.ClassLoader getContextClassLoaderForName​(java.lang.String className)
      Returns the context ClassLoader for the given class name
      static java.lang.reflect.Method getMethodByObject​(java.lang.Class<?> cls, java.lang.String name, java.lang.Object[] args)
      Gets one of the close method by specifying the arguments, rather than the argument types.
      static java.lang.reflect.Method getMethodBySignature​(java.lang.Class<?> cls, java.lang.String signature, java.util.Collection<java.lang.String> params)
      Gets the method based on the signature.
      static java.lang.reflect.Method getMethodBySignature​(java.lang.Class<?> cls, java.lang.String signature, java.util.Collection<java.lang.String> params, ClassResolver resolver)
      Gets the method based on the signature with a class resolver.
      static java.lang.reflect.Method getMethodInPublic​(java.lang.Class<?> cls, java.lang.String name, java.lang.Class<?>[] argTypes)
      Gets the method that is declared in a public class/interface.
      static java.lang.Class<?>[] getSuperClasses​(java.lang.Class<?> cls, java.lang.Class<?>[] clsToCheck)
      Searches thru each element of the specified array of classes, and returns classes that are super-classes (or equal) of the specified class.
      static java.lang.Class<?> getTopmostInterface​(java.lang.Class<?> cls, java.lang.Class<?> subIF)
      Gets the topmost interface of the specified class or interface that implements or extends the specified interface.
      static boolean isAttribute​(java.lang.String methodName)
      Tests if a method name is an attribute, i.e., prefixing with is, get or set.
      static boolean isNumeric​(java.lang.Class<?> cls, boolean extend)
      Checks whether the specified class is a numeric class.
      static boolean isPrimitiveWrapper​(java.lang.Class<?> cls)
      Check whether the specified class is a primitive or a primitive wrapper.
      static java.lang.Object newInstance​(java.lang.Class<?> cls, java.lang.Class<?>[] argTypes, java.lang.Object[] args)
      Instantiates a new instance of the specified class with the specified arguments and argument types.
      static java.lang.Object newInstance​(java.lang.Class<?> cls, java.lang.Object[] args)
      Instantiates a new instance of the specified class with the specified argument.
      static java.lang.Object newInstance​(java.lang.String clsName, java.lang.Class<?>[] argTypes, java.lang.Object[] args)
      Instantiates a new instance of the specified class name with the specified arguments.
      static java.lang.Object newInstanceByThread​(java.lang.String clsName)
      Creates and initializes a new instance of the specified class name with default constructor, by use of forNameByThread(java.lang.String).
      static java.lang.Object newInstanceByThread​(java.lang.String clsName, java.lang.Class<?>[] argTypes, java.lang.Object[] args)
      Creates and initializes a new instance of the specified class name with the specified arguments, by use of forNameByThread(java.lang.String).
      static Classes.MethodInfo parseMethod​(java.lang.String signature)
      Gets the method information from a signature.
      static java.lang.String toAttributeName​(java.lang.String methodName)
      Converts a method name to an attribute name by removing the prefix is, get or set, or null if it doesn't start with is, get or set.
      static java.lang.String toInternalForm​(java.lang.String clsName)
      Change class name to internal form (e.g. byte[] -> [B]).
      static java.lang.String toMethodName​(java.lang.String attrName, java.lang.String prefix)
      Convert an attribute name, returned by toAttributeName, to a method name.
      • Methods inherited from class java.lang.Object

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

      • B_GET

        public static final int B_GET
        Search the get method; not imply B_METHODONLY.
        See Also:
        Constant Field Values
      • B_SET

        public static final int B_SET
        Search the set method; not imply B_METHODONLY.
        See Also:
        Constant Field Values
      • B_PUBLIC_ONLY

        public static final int B_PUBLIC_ONLY
        Only search for public methods or fields.
        See Also:
        Constant Field Values
      • B_METHOD_ONLY

        public static final int B_METHOD_ONLY
        Only search for methods; excluding fields.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Classes

        public Classes()
    • Method Detail

      • newInstance

        public static final java.lang.Object newInstance​(java.lang.Class<?> cls,
                                                         java.lang.Class<?>[] argTypes,
                                                         java.lang.Object[] args)
                                                  throws java.lang.NoSuchMethodException,
                                                         java.lang.InstantiationException,
                                                         java.lang.reflect.InvocationTargetException,
                                                         java.lang.IllegalAccessException
        Instantiates a new instance of the specified class with the specified arguments and argument types.

        Note only public constructors are searched.

        Parameters:
        cls - the class of the instance to create
        argTypes - the argument types of the constructor to invoke
        args - the arguments to initialize the instance
        Returns:
        the new instance
        Throws:
        java.lang.NoSuchMethodException - if a matching method is not found
        java.lang.InstantiationException - if the class that declares the underlying constructor represents an abstract class
        java.lang.reflect.InvocationTargetException - if the underlying constructor throws an exception
        java.lang.IllegalAccessException
        See Also:
        newInstance(String, Class[], Object[])
      • newInstance

        public static final java.lang.Object newInstance​(java.lang.Class<?> cls,
                                                         java.lang.Object[] args)
                                                  throws java.lang.NoSuchMethodException,
                                                         java.lang.InstantiationException,
                                                         java.lang.reflect.InvocationTargetException,
                                                         java.lang.IllegalAccessException
        Instantiates a new instance of the specified class with the specified argument.

        It searches all constructors for the first one that matches the specified arguments.

        Throws:
        java.lang.NoSuchMethodException
        java.lang.InstantiationException
        java.lang.reflect.InvocationTargetException
        java.lang.IllegalAccessException
        Since:
        3.0.1
      • newInstance

        public static final java.lang.Object newInstance​(java.lang.String clsName,
                                                         java.lang.Class<?>[] argTypes,
                                                         java.lang.Object[] args)
                                                  throws java.lang.NoSuchMethodException,
                                                         java.lang.InstantiationException,
                                                         java.lang.reflect.InvocationTargetException,
                                                         java.lang.ClassNotFoundException,
                                                         java.lang.IllegalAccessException
        Instantiates a new instance of the specified class name with the specified arguments.

        It uses Class.forName to get the class.

        Parameters:
        clsName - the class name of the instance to create
        argTypes - the argument types of the constructor to invoke
        args - the arguments to initialize the instance
        Returns:
        the new instance
        Throws:
        java.lang.NoSuchMethodException - if a matching method is not found
        java.lang.InstantiationException - if the class that declares the underlying constructor represents an abstract class
        java.lang.reflect.InvocationTargetException - if the underlying constructor throws an exception
        java.lang.ClassNotFoundException - if the specified class name is not a class
        java.lang.IllegalAccessException
        See Also:
        newInstance(Class, Class[], Object[])
      • newInstanceByThread

        public static final java.lang.Object newInstanceByThread​(java.lang.String clsName,
                                                                 java.lang.Class<?>[] argTypes,
                                                                 java.lang.Object[] args)
                                                          throws java.lang.NoSuchMethodException,
                                                                 java.lang.InstantiationException,
                                                                 java.lang.reflect.InvocationTargetException,
                                                                 java.lang.ClassNotFoundException,
                                                                 java.lang.IllegalAccessException
        Creates and initializes a new instance of the specified class name with the specified arguments, by use of forNameByThread(java.lang.String).

        It uses forNameByThread(java.lang.String) to get the class.

        Parameters:
        clsName - the class name of the instance to create
        argTypes - the argument types of the constructor to invoke
        args - the arguments to initialize the instance
        Returns:
        the new instance
        Throws:
        java.lang.NoSuchMethodException - if a matching method is not found
        java.lang.InstantiationException - if the class that declares the underlying constructor represents an abstract class
        java.lang.reflect.InvocationTargetException - if the underlying constructor throws an exception
        java.lang.ClassNotFoundException - if the specified class name is not a class
        java.lang.IllegalAccessException
        See Also:
        newInstance(Class, Class[], Object[])
      • newInstanceByThread

        public static final java.lang.Object newInstanceByThread​(java.lang.String clsName)
                                                          throws java.lang.NoSuchMethodException,
                                                                 java.lang.InstantiationException,
                                                                 java.lang.reflect.InvocationTargetException,
                                                                 java.lang.ClassNotFoundException,
                                                                 java.lang.IllegalAccessException
        Creates and initializes a new instance of the specified class name with default constructor, by use of forNameByThread(java.lang.String).
        Throws:
        java.lang.NoSuchMethodException
        java.lang.InstantiationException
        java.lang.reflect.InvocationTargetException
        java.lang.ClassNotFoundException
        java.lang.IllegalAccessException
      • forNameByThread

        public static final java.lang.Class<?> forNameByThread​(java.lang.String clsName)
                                                        throws java.lang.ClassNotFoundException
        Returns the Class object of the specified class name, using the current thread's context class loader and the class will be initialized by default.

        It first tries Thread.currentThread().getContextClassLoader(), and then Classes's class loader if not found.

        In additions, it handles the primitive types, such as int and double.

        Parameters:
        clsName - fully qualified name of the desired class
        Returns:
        the Class object representing the desired class
        Throws:
        java.lang.ClassNotFoundException - if the class cannot be located by the specified class loader
        See Also:
        forNameByThread(String, boolean)
      • forNameByThread

        public static final java.lang.Class<?> forNameByThread​(java.lang.String clsName,
                                                               boolean initialize)
                                                        throws java.lang.ClassNotFoundException
        Returns the Class object of the specified class name, using the current thread's context class loader.

        It first tries Thread.currentThread().getContextClassLoader(), and then Classes's class loader if not found.

        In additions, it handles the primitive types, such as int and double.

        Parameters:
        clsName - fully qualified name of the desired class
        initialize - if true the class will be initialized.
        Returns:
        the Class object representing the desired class
        Throws:
        java.lang.ClassNotFoundException - if the class cannot be located by the specified class loader
        Since:
        10.0.0
        See Also:
        forNameByThread(String)
      • existsByThread

        public static final boolean existsByThread​(java.lang.String clsnm)
        Returns whether the specified class exists for the current thread's context class loader.
        Parameters:
        clsnm - the class name to test
        Since:
        3.0.7
      • toInternalForm

        public static final java.lang.String toInternalForm​(java.lang.String clsName)
        Change class name to internal form (e.g. byte[] -> [B]). If already in internal form, then just return it.
      • getTopmostInterface

        public static java.lang.Class<?> getTopmostInterface​(java.lang.Class<?> cls,
                                                             java.lang.Class<?> subIF)
        Gets the topmost interface of the specified class or interface that implements or extends the specified interface. For example, if A extends B, and C implements A, then getTopInterface(C, B) returns A.

        The interfaces implemented by the specified class is checked first, and then the subclass.

        Parameters:
        cls - the class or interface
        subIF - the sub-interface
        Returns:
        the topmost interface extending subIF, or null if subIF is not implemented by cls
      • getAllInterfaces

        public static java.lang.Class<?>[] getAllInterfaces​(java.lang.Class<?> cls)
        Returns all interfaces that are implemented by the specified class.

        Unlike Class.getInterfaces(), it recursively searches for all derived classes.

      • containsMethod

        public static final boolean containsMethod​(java.lang.Class<?> cls,
                                                   java.lang.String name,
                                                   java.lang.Class<?>[] paramTypes)
        Tests whether a class contains the specified method. Only public methods are tested.
        Parameters:
        cls - the class to test
        name - the method name
        paramTypes - the list of parameter types
        Returns:
        true if it contains the method
      • correctFieldName

        public static final java.lang.String correctFieldName​(java.lang.String name)
        Corrects a string to a valid Java name. Currently, it only removes '-' and capitalizes the succeeding character. Example, 'field-name' becomes 'fieldName'.
      • toMethodName

        public static final java.lang.String toMethodName​(java.lang.String attrName,
                                                          java.lang.String prefix)
        Convert an attribute name, returned by toAttributeName, to a method name.

        toMethodName("true", "is") => "isTrue"
        toMethodName("true", "") => "true"

        Parameters:
        attrName - the attribute name
        prefix - the prefix; one of is, get and set
        Returns:
        the method name
        See Also:
        toAttributeName(java.lang.String)
      • isAttribute

        public static final boolean isAttribute​(java.lang.String methodName)
        Tests if a method name is an attribute, i.e., prefixing with is, get or set. Caller could then test if it is a setter or getter by charAt(0)=='s'.

        Note 'set' is considered as an attribute, whose name is an empty string.

        Parameters:
        methodName - the method name to test
        Returns:
        true if it is setter or getter
      • toAttributeName

        public static final java.lang.String toAttributeName​(java.lang.String methodName)
        Converts a method name to an attribute name by removing the prefix is, get or set, or null if it doesn't start with is, get or set.

        The code is optimized for better performance.

        Parameters:
        methodName - the method name
        Returns:
        the attribute name; null if it is not an attribute name
        See Also:
        toMethodName(java.lang.String, java.lang.String)
      • parseMethod

        public static final Classes.MethodInfo parseMethod​(java.lang.String signature)
                                                    throws IllegalSyntaxException
        Gets the method information from a signature. It returns a method info with the return type, method name and two collections of arguments type and arguments name.
        Parameters:
        signature - the method signature.
        Returns:
        MethodInfo The method information including return type, method name and two collections for argument type and arguments name.
        Throws:
        IllegalSyntaxException
      • getMethodBySignature

        public static final java.lang.reflect.Method getMethodBySignature​(java.lang.Class<?> cls,
                                                                          java.lang.String signature,
                                                                          java.util.Collection<java.lang.String> params,
                                                                          ClassResolver resolver)
                                                                   throws java.lang.NoSuchMethodException,
                                                                          java.lang.ClassNotFoundException
        Gets the method based on the signature with a class resolver. It also returns the parameter names to the params list.

        Like getMethodInPublic(Class, String, Class[]), it returns only public method in a public class/interface.

        For example, "find(java.lang.String name)" will return the method with one String-typed argument and params will hold "name". The return type is optional (actually ignored).

        If params is null, the parameter names are not returned and the signature could be simplified as "find(java.lang.String)".

        A cache mechanism is implemented, so you don't need to cache it again in the caller.

        Parameters:
        cls - the class to look
        signature - the method signature; the return type is optional.
        Notice that the argument's type must be a full-qualified class name, unless its package is java.lang or it can be resolved by resolver.
        params - the collection to hold the parameter names returned; null means no parameter names to return
        resolver - the class resolver used to resolve the class specified in the signature. Ignored if null.
        Throws:
        java.lang.NoSuchMethodException
        java.lang.ClassNotFoundException
        Since:
        6.0.0
      • getMethodBySignature

        public static final java.lang.reflect.Method getMethodBySignature​(java.lang.Class<?> cls,
                                                                          java.lang.String signature,
                                                                          java.util.Collection<java.lang.String> params)
                                                                   throws java.lang.NoSuchMethodException,
                                                                          java.lang.ClassNotFoundException
        Gets the method based on the signature. It also returns the parameter names to the params list.

        Like getMethodInPublic(Class, String, Class[]), it returns only public method in a public class/interface.

        For example, "find(java.lang.String name)" will return the method with one String-typed argument and params will hold "name". The return type is optional (actually ignored).

        If params is null, the parameter names are not returned and the signature could be simplified as "find(java.lang.String)".

        A cache mechanism is implemented, so you don't need to cache it again in the caller.

        Parameters:
        cls - the class to look
        signature - the method signature; the return type is optional
        Notice that the argument's type must be a full-qualified class name, unless its package is java.lang.
        params - the collection to hold the parameter names returned; null means no parameter names to return
        Throws:
        java.lang.NoSuchMethodException
        java.lang.ClassNotFoundException
      • getMethodInPublic

        public static final java.lang.reflect.Method getMethodInPublic​(java.lang.Class<?> cls,
                                                                       java.lang.String name,
                                                                       java.lang.Class<?>[] argTypes)
                                                                throws java.lang.NoSuchMethodException
        Gets the method that is declared in a public class/interface.

        Class.getMethod returns a public method but the class itself might not be public. However, in many cases, that class also implements a public interface or class.

        This method will search all its public classes to look for the method that is 'real' public.

        NoSuchMethodException is thrown if no public class/interface is found to have the method.

        Throws:
        java.lang.NoSuchMethodException
      • getMethodByObject

        public static final java.lang.reflect.Method getMethodByObject​(java.lang.Class<?> cls,
                                                                       java.lang.String name,
                                                                       java.lang.Object[] args)
                                                                throws java.lang.NoSuchMethodException
        Gets one of the close method by specifying the arguments, rather than the argument types. It actually calls getCloseMethod(java.lang.Class<?>, java.lang.String, java.lang.Class<?>[]).
        Throws:
        java.lang.NoSuchMethodException
      • getCloseMethod

        public static final java.lang.reflect.Method getCloseMethod​(java.lang.Class<?> cls,
                                                                    java.lang.String name,
                                                                    java.lang.Class<?>[] argTypes)
                                                             throws java.lang.NoSuchMethodException
        Gets one of the close methods -- a close method is a method with the same name and the compatible argument type. By compatible we mean the real method's argument type is the same as or a superclass of the specified one.

        It might not be the best fit one, unless there is a method whose argument types are exactly argTypes.

        You might specify the exact number in argTypes. If any of them is unknown, use null. Example, in the following, the first argument could be anything and the second is anything deriving from MyClass:
        new Class[] {null, MyClass.class}

        Note: if an argument accepts int, then Integer is considered as compatible (unlike Class.getMethod). So are long, byte...

        A cache mechanism is implemented, so you don't need to cache it again in the caller.

        Parameters:
        cls - the class to locate the method
        name - the method name
        argTypes - an array of the argument classes; null to denote no argument at all (i.e., exact match). Any argTypes[i] could be null to denote any class.
        Returns:
        the method
        Throws:
        java.lang.NoSuchMethodException - if the method is not found
      • getCloseMethodBySubclass

        public static final java.lang.reflect.Method getCloseMethodBySubclass​(java.lang.Class<?> cls,
                                                                              java.lang.String name,
                                                                              java.lang.Class<?>[] argTypes)
                                                                       throws java.lang.NoSuchMethodException
        Like getCloseMethod(java.lang.Class<?>, java.lang.String, java.lang.Class<?>[]) to get a 'close' method, but it look for subclass of the argument (instead of superclass). In other words, it looks for the method whose argument type is the same as or a subclass of the specified one.
        Throws:
        java.lang.NoSuchMethodException
      • getCloseMethods

        public static final java.lang.reflect.Method[] getCloseMethods​(java.lang.Class<?> cls,
                                                                       java.lang.String name,
                                                                       java.lang.Class<?>[] argTypes)
        Returns all close methods that match the specified condition, or a zero-length array if none is found.

        Unlike getCloseMethod(java.lang.Class<?>, java.lang.String, java.lang.Class<?>[]), we don't cache the searched result, and it won't throw any exception.

      • getCloseMethodsBySubclass

        public static final java.lang.reflect.Method[] getCloseMethodsBySubclass​(java.lang.Class<?> cls,
                                                                                 java.lang.String name,
                                                                                 java.lang.Class<?>[] argTypes)
        Like getCloseMethods(java.lang.Class<?>, java.lang.String, java.lang.Class<?>[]) to get all 'close' methods, but it look for subclass of the argument (instead of superclass). In other words, it looks for the method whose argument type is the same as or a subclass of the specified one.
      • getAccessibleObject

        public static final java.lang.reflect.AccessibleObject getAccessibleObject​(java.lang.Class<?> cls,
                                                                                   java.lang.String name,
                                                                                   java.lang.Class<?>[] argTypes,
                                                                                   int flags)
                                                                            throws java.lang.NoSuchMethodException
        Gets the specified accessible object, either a method or a field, by searching the specified name.

        The search sequence is: (assume field name is body)>
        getBody(...)
        isBody(...)
        body(...)
        body

        If B_SET is specified, setBody(...) is searched instead of getBody(...) and isBody(...). The field is searched only if argsType.length is 0 or 1.

        Note: it uses getCloseMethod(java.lang.Class<?>, java.lang.String, java.lang.Class<?>[]) to get the method.

        A cache mechanism is implemented, so you don't need to cache it again in the caller.

        Parameters:
        cls - the class to find
        name - the name of the accessible object
        argTypes - the parameter type of the method to find
        flags - a combination of B_xxx or zero
        Returns:
        the accessible object; either Field or Method
        Throws:
        java.lang.NoSuchMethodException - if neither the set method of specified field nor the field itself not found
        java.lang.SecurityException - if access to the information is denied
      • getAnyMethod

        public static final java.lang.reflect.Method getAnyMethod​(java.lang.Class<?> cls,
                                                                  java.lang.String name,
                                                                  java.lang.Class<?>[] argTypes)
                                                           throws java.lang.NoSuchMethodException
        Gets the specified method by searching all methods including any access control and any base class. Note: you rarely need to call this method. In most cases, Class.getMethod, getCloseMethod(java.lang.Class<?>, java.lang.String, java.lang.Class<?>[]), and getMethodInPublic(java.lang.Class<?>, java.lang.String, java.lang.Class<?>[]) are what you need.

        The search sequence is: this class's methods, and then the superclass's methods.

        Note: public methods don't be treated different. If the caller wants to search public methods first, it has to call Class.getMethod first.

        Parameters:
        cls - the class to search
        name - the method name
        argTypes - the parameter array of types
        Returns:
        the Method object
        Throws:
        java.lang.NoSuchMethodException - if a matching method is not found.
        java.lang.SecurityException - if access to the information is denied.
        See Also:
        getAccessibleObject(Class, String, Class[], int), getAnyField(Class, String)
      • getAnyField

        public static final java.lang.reflect.Field getAnyField​(java.lang.Class<?> cls,
                                                                java.lang.String name)
                                                         throws java.lang.NoSuchFieldException
        Gets the specified field by searching all fields including any access control and any base class. The search sequence is: this class's fields, and then the superclass's fields.

        Note: public fields don't be treated different. If the caller wants to search public fields first, it has to call Class.getField first.

        Parameters:
        cls - the class to search
        name - the field name
        Returns:
        the Field object
        Throws:
        java.lang.NoSuchFieldException - if a matching field is not found.
        java.lang.SecurityException - if access to the information is denied.
        See Also:
        getAccessibleObject(Class, String, Class[], int), getAnyMethod(Class, String, Class[])
      • getSuperClasses

        public static final java.lang.Class<?>[] getSuperClasses​(java.lang.Class<?> cls,
                                                                 java.lang.Class<?>[] clsToCheck)
        Searches thru each element of the specified array of classes, and returns classes that are super-classes (or equal) of the specified class.
        Parameters:
        cls - the specified class; null is not allowed
        clsToCheck - the class array to check; null is acceptable
        Returns:
        a subset of clsToCheck that are super-class of cls; null if no one qualified
      • isPrimitiveWrapper

        public static final boolean isPrimitiveWrapper​(java.lang.Class<?> cls)
        Check whether the specified class is a primitive or a primitive wrapper.
      • isNumeric

        public static final boolean isNumeric​(java.lang.Class<?> cls,
                                              boolean extend)
        Checks whether the specified class is a numeric class.
        Parameters:
        extend - whether to consider Date, char, boolean, Character and Boolean as a numeric object.
      • coerce

        public static java.lang.Object coerce​(java.lang.Class<?> cls,
                                              java.lang.Object val)
                                       throws java.lang.ClassCastException
        Converts an object to the specified class. It is the same as coerce(cls, val, true).
        Parameters:
        val - the value.
        Throws:
        java.lang.ClassCastException - if failed to convert
        See Also:
        coerce(Class, Object, boolean)
      • coerce

        public static java.lang.Object coerce​(java.lang.Class<?> cls,
                                              java.lang.Object val,
                                              boolean nullable)
                                       throws java.lang.ClassCastException
        Converts to the specified type.
        Parameters:
        nullable - whether the result could be null. If false, 0 is used for number, the default constructor is used for others. coerce(Class, Object) is a special case that is equivalent to nullable=true.
        Throws:
        java.lang.ClassCastException - if failed to convert
      • getContextClassLoader

        public static java.lang.ClassLoader getContextClassLoader​(java.lang.Class<?> reference)
        Returns the context ClassLoader for the reference class.

        Default: return from the current thread.
        Or specify the library property of org.zkoss.lang.contextClassLoader.class in zk.xml to provide a customized class loader.

        Parameters:
        reference - the reference class where it is invoked from.
        Since:
        8.0.2
      • getContextClassLoaderForName

        public static java.lang.ClassLoader getContextClassLoaderForName​(java.lang.String className)
        Returns the context ClassLoader for the given class name

        Default: return from the current thread.
        Or specify the library property of org.zkoss.lang.contextClassLoader.class in zk.xml to provide a customized class loader.

        Parameters:
        className - the class name to be loaded by the returned class loader
        Since:
        8.5.1
      • configureContextClassLoader

        public static void configureContextClassLoader()
        Sets the customized context ClassLoader name by the library property of org.zkoss.lang.contextClassLoader.class. It is intended to be called by ZK internally.
        Since:
        8.5.1