Class ReflectionUtil


  • public final class ReflectionUtil
    extends Object
    This utility class provides a way to extend the reflection API and the Class class with autoboxing-compliant functions.
    Since:
    since JDK 1.5
    Version:
    17.0 2020-01-04 14:41:35
    Author:
    Stéphane GALLAND
    Maven Group Id:
    org.arakhne.afc.core
    Maven Artifact Id:
    vmutils
    • Method Detail

      • isInstance

        @Pure
        public static boolean isInstance​(Class<?> type,
                                         Object obj)
        Determines if the specified Object is assignment-compatible with the object represented by the Class. This method extends Class.isInstance(Object) with autoboxing support.
        Parameters:
        type - is the class against which the object must be test
        obj - is the object to check
        Returns:
        true if obj is an instance of the type
        See Also:
        Class.isInstance(Object)
      • isAssignableFrom

        @Pure
        public static boolean isAssignableFrom​(Class<?> assignementTarget,
                                               Class<?> assignementSource)
        Determines if the assignmentTarget object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified assignementSource parameter. This method extends Class.isAssignableFrom(Class) with autoboxing support.
        Parameters:
        assignementTarget - is the class that is tested to be a super class.
        assignementSource - is the class that is tested to be a sub class.
        Returns:
        true if an object of the assignementSource type could be assigned to a variable of assignementTarget type, otherwise false.
      • forName

        @Pure
        public static Class<?> forName​(String name)
                                throws ClassNotFoundException
        Replies the type that corresponds to the specified class. If the name corresponds to a primitive type, the low-level type will be replied. This method extends Class.forName(String) with autoboxing support.
        Parameters:
        name - is the name of the class to load.
        Returns:
        the loaded class
        Throws:
        ClassNotFoundException - if name names an unknown class or primitive
      • forName

        @Pure
        public static Class<?> forName​(String name,
                                       ClassLoader loader)
                                throws ClassNotFoundException
        Replies the type that corresponds to the specified class. If the name corresponds to a primitive type, the low-level type will be replied. This method extends Class.forName(String) with autoboxing support.
        Parameters:
        name - is the name of the class to load.
        loader - is the class loader to use.
        Returns:
        the loaded class
        Throws:
        ClassNotFoundException - if name names an unknown class or primitive
      • forName

        @Pure
        public static Class<?> forName​(String name,
                                       boolean typeInitialization,
                                       ClassLoader loader)
                                throws ClassNotFoundException
        Replies the type that corresponds to the specified class. If the name corresponds to a primitive type, the low-level type will be replied. This method extends Class.forName(String) with autoboxing support.
        Parameters:
        name - is the name of the class to load.
        typeInitialization - must be true to initialize the type, false otherwise.
        loader - is the class loader to use.
        Returns:
        the loaded class
        Throws:
        ClassNotFoundException - if name names an unknown class or primitive
      • getPackageClasses

        @Pure
        public static Collection<Class<?>> getPackageClasses​(Package pkg)
        Replies the list of the classes in the given package.
        Parameters:
        pkg - is the package to explore.
        Returns:
        the list of classes in the package.
      • getPackageClasses

        @Pure
        public static Collection<Class<?>> getPackageClasses​(String packageName)
        Replies the list of the classes in the given package.
        Parameters:
        packageName - is the name of the package to explore.
        Returns:
        the list of classes in the package.
      • getSubClasses

        @Pure
        public static <T> Collection<Class<? extends T>> getSubClasses​(Class<T> className)
        Replies the list of all the subclasses of the given class in the current classpath.
        Type Parameters:
        T - is the type of the superclass.
        Parameters:
        className - is the name of the class to explore.
        Returns:
        the list of subclasses.
      • getSubClasses

        @Pure
        public static <T> Collection<Class<? extends T>> getSubClasses​(Class<T> className,
                                                                       DynamicURLClassLoader classLoader)
        Replies the list of all the subclasses of the given class in the current classpath.
        Type Parameters:
        T - is the type of the superclass.
        Parameters:
        className - is the name of the class to explore.
        classLoader - the class loader that is used for exploring the class path. If null, the default class path is explored.
        Returns:
        the list of subclasses.
        Since:
        16.0
      • getSubClasses

        public static <T> void getSubClasses​(Class<T> className,
                                             boolean allowAbstract,
                                             boolean allowInterface,
                                             boolean allowEnum,
                                             Collection<Class<? extends T>> result)
        Replies the list of all the subclasses of the given class in the current classpath.
        Type Parameters:
        T - is the type of the superclass.
        Parameters:
        className - is the name of the class to explore.
        allowAbstract - is true to allow abstract classes to be put in the replied list
        allowInterface - is true to allow interfaces to be put in the replied list.
        allowEnum - is true to allow enumeration to be put in the replied list.
        result - is the list of subclasses which will be filled by this function.
      • getSubClasses

        public static <T> void getSubClasses​(Class<T> className,
                                             boolean allowAbstract,
                                             boolean allowInterface,
                                             boolean allowEnum,
                                             Collection<Class<? extends T>> result,
                                             DynamicURLClassLoader classLoader)
        Replies the list of all the subclasses of the given class in the current classpath.
        Type Parameters:
        T - is the type of the superclass.
        Parameters:
        className - is the name of the class to explore.
        allowAbstract - is true to allow abstract classes to be put in the replied list
        allowInterface - is true to allow interfaces to be put in the replied list.
        allowEnum - is true to allow enumeration to be put in the replied list.
        classLoader - the class loader that is used for exploring the class path. If null, the default class path is explored.
        result - is the list of subclasses which will be filled by this function.
      • getAllDirectInterfaces

        @Pure
        public static <T,​I> Set<Class<? extends I>> getAllDirectInterfaces​(Class<? extends T> lowestType,
                                                                                 Class<T> highestType,
                                                                                 Class<I> interfaceType)
        Determines the interfaces implemented by the classes from the lowest type to the highestType which are extended the given interfaceType.

        Insteed of Class.getInterfaces(), this function is exploring the super classes. This function does not explore super-interfaces of implemented interfaces.

        
         interface IA {}
         interface IB extends IA {}
         interface IC {}
         interface ID extends IB, IC {}
         class CA implements IC {}
         class CB extends CA {}
         class CC extends CB implements IB {}
         
        This function replies for:
        • getAllDirectInterfaces(IA,null,null)={}
        • getAllDirectInterfaces(IB,null,null)={IA}
        • getAllDirectInterfaces(IC,null,null)={}
        • getAllDirectInterfaces(ID,null,null)={IB,IC}
        • getAllDirectInterfaces(CA,null,null)={IC}
        • getAllDirectInterfaces(CB,null,null)={IC}
        • getAllDirectInterfaces(CC,null,null)={IB,IC}
        Type Parameters:
        T - is the highest type to explore in type hierarchy.
        I - indicates the type of the replied interfaces.
        Parameters:
        lowestType - is the lowest type to explore in type hierarchy.
        highestType - is the highest type to explore in type hierarchy.
        interfaceType - indicates the type of the replied interfaces.
        Returns:
        the implemented interfaces.
        Since:
        5.0
      • getAllDirectInterfaces

        public static <T> Set<Class<?>> getAllDirectInterfaces​(Class<? extends T> lowestType,
                                                               Class<T> highestType)
        Determines the interfaces implemented by the classes from the lowest type to the highestType which are extended the given interfaceType.

        Insteed of Class.getInterfaces(), this function is exploring the super classes. This function does not explore super-interfaces of implemented interfaces.

        
         interface IA {}
         interface IB extends IA {}
         interface IC {}
         interface ID extends IB, IC {}
         class CA implements IC {}
         class CB extends CA {}
         class CC extends CB implements IB {}
         
        This function replies for:
        • getAllDirectInterfaces(IA,null,null)={}
        • getAllDirectInterfaces(IB,null,null)={IA}
        • getAllDirectInterfaces(IC,null,null)={}
        • getAllDirectInterfaces(ID,null,null)={IB,IC}
        • getAllDirectInterfaces(CA,null,null)={IC}
        • getAllDirectInterfaces(CB,null,null)={IC}
        • getAllDirectInterfaces(CC,null,null)={IB,IC}
        Type Parameters:
        T - is the highest type to explore in type hierarchy.
        Parameters:
        lowestType - is the lowest type to explore in type hierarchy.
        highestType - is the highest type to explore in type hierarchy.
        Returns:
        the implemented interfaces.
        Since:
        5.0
      • getSuperClasses

        @Pure
        public static <T> Collection<Class<? super T>> getSuperClasses​(Class<T> className)
        Replies the list of all the superclasses of the given class.

        This function does not replies Object.class.

        Type Parameters:
        T - is the type of the lowest class.
        Parameters:
        className - is the type of the lowest class.
        Returns:
        the list of superclasses.
        Since:
        5.0
      • getCommonType

        public static Class<?> getCommonType​(Class<?> type1,
                                             Class<?> type2)
        Replies the top-most type which is common to both given types.
        Parameters:
        type1 - first type.
        type2 - second type.
        Returns:
        the top-most type which is common to both given types.
        Since:
        6.0
      • getCommonType

        @Pure
        public static Class<?> getCommonType​(Object instance1,
                                             Object instance2)
        Replies the top-most type which is common to both given objects.
        Parameters:
        instance1 - first object.
        instance2 - second object.
        Returns:
        the top-most type which is common to both given objects.
        Since:
        6.0
      • getOutboxingType

        @Pure
        public static Class<?> getOutboxingType​(Class<?> type)
        Replies the outboxing type for the given type.
        Parameters:
        type - the type.
        Returns:
        the outboxing of the given type.
        Since:
        7.1
      • getInboxingType

        @Pure
        public static Class<?> getInboxingType​(Class<?> type)
        Replies the outboxing type for the given type.
        Parameters:
        type - the type.
        Returns:
        the outboxing of the given type.
        Since:
        7.1
      • matchesParameters

        @Pure
        public static boolean matchesParameters​(Class<?>[] formalParameters,
                                                Object... parameterValues)
        Replies if the formal parameters are matching the given values.
        Parameters:
        formalParameters - the types of the formal parameters.
        parameterValues - the values associated to the paramters.
        Returns:
        true if the values could be passed to the method.
        Since:
        7.1
      • matchesParameters

        @Pure
        public static boolean matchesParameters​(Method method,
                                                Object... parameters)
        Replies if the parameters of the given method are matching the given values.
        Parameters:
        method - the method that contains the types.
        parameters - the objects associated to the paramters.
        Returns:
        true if the values could be passed to the method.
        Since:
        7.1
      • toString

        public static String toString​(Object object)
        Replies the string representation of the given object.

        The string representation is based on the values replied by the getters (functions that are starting by "get" or "is" or "has").

        Parameters:
        object - the object to analyze.
        Returns:
        the string representation.
      • toJson

        public static void toJson​(Object object,
                                  JsonBuffer output)
        Replies the string representation of the given object.

        The string representation is based on the values replied by the getters (functions that are starting by "get" or "is" or "has").

        Parameters:
        object - the object to analyze.
        output - the Json string representation.
        Since:
        15.0
      • newInstance

        public static <T> T newInstance​(Class<T> type,
                                        Object... arguments)
                                 throws InstantiationException,
                                        IllegalAccessException,
                                        IllegalArgumentException,
                                        InvocationTargetException
        Create an instance of the given type with the given arguments for the constructor.
        Type Parameters:
        T - the type of the instance to create.
        Parameters:
        type - the type of the instance to create.
        arguments - the arguments to pass to the constructor.
        Returns:
        the instance.
        Throws:
        IllegalAccessException - if this Constructor object is enforcing Java language access control and the underlying constructor is inaccessible.
        IllegalArgumentException - if the number of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal parameter type by a method invocation conversion; if this constructor pertains to an enum type.
        InstantiationException - if the class that declares the underlying constructor represents an abstract class.
        InvocationTargetException - if the underlying constructor throws an exception.
        ExceptionInInitializerError - if the initialization provoked by this method fails.
        Since:
        16.0