Class ClassUtil


  • public final class ClassUtil
    extends Object
    ClassUtil is a utility class providing assistance for Java Classes.

    Copyright (c) 2002-2009 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.

    See for project information www.getSI()mulation.tudelft.nl.

    The DSOL project is distributed under the following BSD-style license:
    Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    • Neither the name of Delft University of Technology, nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    This software is provided by the copyright holders and contributors "as is" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the copyright holder or contributors be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.
    Since:
    1.5
    Version:
    $Revision: 1.3 $ $Date: 2010/08/10 11:39:11 $
    Author:
    Peter Jacobs, Niels Lang, Alexander Verbraeck
    • Method Detail

      • getAllConstructors

        public static Constructor<?>[] getAllConstructors​(Class<?> clazz,
                                                          Constructor<?>[] result)
        gets all the constructors of a class and adds the result to result.
        Parameters:
        clazz - the class
        result - the resulting set
        Returns:
        result
      • resolveConstructor

        public static Constructor<?> resolveConstructor​(Class<?> clazz,
                                                        Class<?> callerClass,
                                                        Class<?>... parameterTypes)
                                                 throws NoSuchMethodException
        returns the interface method.
        Parameters:
        clazz - the class to start with
        callerClass - the calling class
        parameterTypes - the parameterTypes
        Returns:
        Constructor
        Throws:
        NoSuchMethodException - if the method cannot be resolved
      • resolveConstructor

        public static Constructor<?> resolveConstructor​(Class<?> clazz,
                                                        Class<?>... parameterTypes)
                                                 throws NoSuchMethodException
        returns the interface method.
        Parameters:
        clazz - the class to start with
        parameterTypes - the parameterTypes
        Returns:
        Constructor
        Throws:
        NoSuchMethodException - if the method cannot be resolved
      • getAllFields

        public static Set<Field> getAllFields​(Class<?> clazz,
                                              Set<Field> result)
        gets all the fields of a class (public, protected, package, and private) and adds the result to the return value.
        Parameters:
        clazz - the class
        result - the resulting set
        Returns:
        the set of fields including all fields of the field clazz
      • getAllFields

        public static Set<Field> getAllFields​(Class<?> clazz)
        gets all the fields of a class (public, protected, package, and private).
        Parameters:
        clazz - the class
        Returns:
        all fields of the class
      • resolveField

        public static Field resolveField​(Class<?> clazz,
                                         String fieldName)
                                  throws NoSuchFieldException
        resolves the field for a class, taking into account inner classes.
        Parameters:
        clazz - the class to resolve the field for, including inner classes
        fieldName - name of the field
        Returns:
        Field the field
        Throws:
        NoSuchFieldException - on no such field
      • resolveField

        public static Field resolveField​(Class<?> clazz,
                                         Class<?> callerClass,
                                         String name)
                                  throws NoSuchFieldException
        returns the field.
        Parameters:
        clazz - the class to start with
        callerClass - the calling class
        name - the fieldName
        Returns:
        Constructor
        Throws:
        NoSuchFieldException - if the method cannot be resolved
      • resolveField

        public static Field resolveField​(Object object,
                                         String fieldName)
                                  throws NoSuchFieldException
        resolves the field for a given object instance.
        Parameters:
        object - the object to resolve the field for
        fieldName - name of the field to resolve
        Returns:
        the field (if found)
        Throws:
        NoSuchFieldException - if the field cannot be resolved
      • getAllMethods

        public static Method[] getAllMethods​(Class<?> clazz,
                                             String name,
                                             Method[] result)
        gets all the methods of a class and adds the result to result.
        Parameters:
        clazz - the class
        name - the name of the method
        result - the resulting set
        Returns:
        result
      • resolveMethod

        public static Method resolveMethod​(Class<?> clazz,
                                           Class<?> callerClass,
                                           String name,
                                           Class<?>... parameterTypes)
                                    throws NoSuchMethodException
        returns the interface method.
        Parameters:
        clazz - the class to start with
        callerClass - the caller class
        name - the name of the method
        parameterTypes - the parameterTypes
        Returns:
        Method
        Throws:
        NoSuchMethodException - on lookup failure
      • resolveMethod

        public static Method resolveMethod​(Class<?> clazz,
                                           String name,
                                           Class<?>... parameterTypes)
                                    throws NoSuchMethodException
        returns the interface method.
        Parameters:
        clazz - the class to start with
        name - the name of the method
        parameterTypes - the parameterTypes
        Returns:
        Method
        Throws:
        NoSuchMethodException - on lookup failure
      • resolveMethod

        public static Method resolveMethod​(Object object,
                                           String name,
                                           Class<?>... parameterTypes)
                                    throws NoSuchMethodException
        resolves a method the method.
        Parameters:
        object - the object to start with
        name - the name of the method
        parameterTypes - the parameterTypes
        Returns:
        Method
        Throws:
        NoSuchMethodException - on lookup failure
      • isVisible

        public static boolean isVisible​(int modifiers,
                                        Class<?> declaringClass,
                                        Class<?> caller)
        Returns whether a declaringClass is accessible according to the modifiers.
        Parameters:
        modifiers - the modifiers
        declaringClass - the declaringClass
        caller - the caller
        Returns:
        boolean isVisible
      • isMoreSpecific

        public static boolean isMoreSpecific​(Class<?>[] a,
                                             Class<?>[] b)
        Determines & returns whether constructor 'a' is more specific than constructor 'b', as defined in the Java Language Specification ???15.12.
        Parameters:
        a - reflects the first constructor
        b - reflects the second constructor
        Returns:
        true if 'a' is more specific than b, false otherwise. 'false' is also returned when constructors are incompatible, e.g. have different names or a different number of parameters.
      • isMoreSpecific

        public static boolean isMoreSpecific​(Constructor<?> a,
                                             Constructor<?> b)
        Determines & returns whether constructor 'a' is more specific than constructor 'b', as defined in the Java Language Specification ???15.12.
        Parameters:
        a - reflects the first constructor
        b - reflects the second constructor
        Returns:
        true if 'a' is more specific than b, false otherwise. 'false' is also returned when constructors are incompatible, e.g. have different names or a different number of parameters.
      • isMoreSpecific

        public static boolean isMoreSpecific​(Method a,
                                             Method b)
        Determines & returns whether constructor 'a' is more specific than constructor 'b', as defined in the Java Language Specification ???15.12.
        Parameters:
        a - reflects the first method
        b - reflects the second method
        Returns:
        true if 'a' is more specific than b, false otherwise. 'false' is also returned when constructors are incompatible, e.g. have different names or a different number of parameters.
      • isVisible

        public static boolean isVisible​(Field field,
                                        Class<?> caller)
        Returns whether a field is visible for a caller.
        Parameters:
        field - The field
        caller - The class of the caller for whom invocation visibility is checked.
        Returns:
        boolean yes or no
      • isVisible

        public static boolean isVisible​(Constructor<?> constructor,
                                        Class<?> caller)
        Returns whether a constructor is visible for a caller.
        Parameters:
        constructor - The constructor
        caller - The class of the caller for whom invocation visibility is checked.
        Returns:
        boolean yes or no
      • isVisible

        public static boolean isVisible​(Method method,
                                        Class<?> caller)
        Returns whether a method is visible for a caller.
        Parameters:
        method - The method
        caller - The class of the caller for whom invocation visibility is checked.
        Returns:
        boolean yes or no
      • matchSignature

        public static Method[] matchSignature​(Method[] methods,
                                              String name,
                                              Class<?>... argTypes)
        Filters an array methods for signatures that are compatible with a given signature.
        Parameters:
        methods - which are methods to be filtered.
        name - reflects the method's name, part of the signature
        argTypes - are the method's argument types
        Returns:
        Method[] An unordered Method-array consisting of the elements of 'methods' that match with the given signature. An array with 0 elements is returned when no matching Method objects are found.
      • matchSignature

        public static boolean matchSignature​(Method method,
                                             String name,
                                             Class<?>... argTypes)
        Filters an array methods for signatures that are compatible with a given signature.
        Parameters:
        method - The method to be filtered.
        name - reflects the method's name, part of the signature
        argTypes - are the method's argument types
        Returns:
        boolean if methodParameters assignable from argTypes
      • matchSignature

        public static boolean matchSignature​(Constructor<?> constructor,
                                             Class<?>... argTypes)
        Filters an array methods for signatures that are compatible with a given signature.
        Parameters:
        constructor - which are constructors to be filtered.
        argTypes - are the constructor's argument types
        Returns:
        boolean if methodParameters assignable from argTypes
      • matchSignature

        public static Constructor<?>[] matchSignature​(Constructor<?>[] constructors,
                                                      Class<?>... argTypes)
        Filters an array methods for signatures that are compatible with a given signature.
        Parameters:
        constructors - which are constructors to be filtered.
        argTypes - are the constructor's argument types
        Returns:
        Constructor<?>[] An unordered Constructor-array consisting of the elements of 'constructors' that match with the given signature. An array with 0 elements is returned when no matching Method objects are found.
      • getClass

        public static Class<?>[] getClass​(Object[] array)
        converts an array of objects to their corresponding classes.
        Parameters:
        array - the array to invoke
        Returns:
        Class<?>[] the result;