Class ReflectionTools


  • public class ReflectionTools
    extends Object
    Various helper methods for reflection.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  ReflectionTools.ClassVisitor<T>
      Interface for visiting a class hierarchy and accumulating the result.
    • Constructor Summary

      Constructors 
      Constructor Description
      ReflectionTools()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      protected static Map<Class<?>,​Map<TypeVariable<?>,​Type>> addGenericParents​(Type current, Map<TypeVariable<?>,​Type> subTypeArguments, Map<Class<?>,​Map<TypeVariable<?>,​Type>> result)
      Adds the transitive parent classes and interfaces of the designated type to the designated result map (which is also returned).
      protected static byte compare​(Class<?> c1, Class<?> c2)
      Compares the designated classes, -1 if c1 is assignable (a subclass/implementation) of c2, 1 if c2 is assignable (a subclass/implementation) of c1, 0 if they are unrelated and Byte.MIN_VALUE if they are equal.
      static int distance​(Class<?> sup, Class<?> sub)
      Calculates the distance from the designated subclass to the designated superclass: Equal classes have the distance 0. An interface and a class directly implementing it have the distance 1. A super-class and its subclass have the distance 2. A super-interface and its subinterface have the distance 2. If the subclass cannot be converted to the superclass or one of the classes is null, the distance will be Integer.MAX_VALUE. the used type variables including the corresponding type where possible.
      static MethodWrapper getCorrespondingVarArgsMethod​(String name, Class<?>[] paramTypes, Object[] paramValues, Class<?> searchedClass)
      Gets the method from the designated class that corresponds to the designated method but has an additional varargs parameter or the designated method has an varargs parameter but the returned method from the designated class has not.
      static Map<Class<?>,​Map<TypeVariable<?>,​Type>> getGenericParents​(Type t)
      Gets the transitive parent classes and interfaces of the designated type mapped to the type variables these classes and interfaces declare and which are mapped to a type.
      static String getJavadocRefString​(Class<?> cls)
      Gets the name and path of the Javadoc HTML file (relative, without leading "/" for the designated class.
      static String getJavadocRefString​(Method m)
      Gets fragment identifier of the designated method including the name and path of the Javadoc HTML file (relative, without leading "/" of the corresponding declaring class.
      protected static StringBuilder getJavadocRefType​(Class<?> cls)
      Gets a string representation for the designated class used in Javadoc.
      static boolean overrides​(Class<?> overridingCls, Method overriding, Class<?> overriddenCls, Method overridden)
      Gets whether the designated first method overrides the second one.
      static Pair<Method,​Object[]> searchForMethod​(Class<?> typeToScan, String methodName, Class<?>[] methodParameterTypes, Object[] paramValues, Logger logger)
      Searches the designated method within the designated type and returns the corresponding Method object.
      static List<Class<?>> sortSubclassRelation​(Collection<Class<?>> coll)
      Sorts the designated classes according to their relation, that is, subclasses are successors of the corresponding parent classes/interfaces.
      static Throwable unwrapIte​(InvocationTargetException ite, Method m, Object o, boolean unwrapUnchecked)
      Unwraps an exception declared by the designated Method from the designated InvocationTargetException or returns the designated InvocationTargetException.
      static <T> T visitClassHierarchy​(Class<?> cls, ReflectionTools.ClassVisitor<T> visitor, boolean includeTransIfaces)
      Visits the (complete) class hierarchy (interfaces, parent classes and their interfaces as well as all parent interfaces if desired) of the designated class with the designated visitor.
    • Constructor Detail

      • ReflectionTools

        public ReflectionTools()
    • Method Detail

      • distance

        public static int distance​(Class<?> sup,
                                   Class<?> sub)
        Calculates the distance from the designated subclass to the designated superclass:
        • Equal classes have the distance 0.
        • An interface and a class directly implementing it have the distance 1.
        • A super-class and its subclass have the distance 2.
        • A super-interface and its subinterface have the distance 2.
        • If the subclass cannot be converted to the superclass or one of the classes is null, the distance will be Integer.MAX_VALUE.
        the used type variables including the corresponding type where possible. Distances spanning several classes/instances are totalled. If there are several ways to get from one class to the other (via interfaces), the shortest one will be chosen.
        Parameters:
        sup - The class (as fixed point) to which to calculate the distance.
        sub - The class from which to calculate the distance.
        Returns:
        The distance between the two classes (or interfaces).
      • searchForMethod

        public static Pair<Method,​Object[]> searchForMethod​(Class<?> typeToScan,
                                                                  String methodName,
                                                                  Class<?>[] methodParameterTypes,
                                                                  Object[] paramValues,
                                                                  Logger logger)
                                                           throws SecurityException,
                                                                  NoSuchMethodException
        Searches the designated method within the designated type and returns the corresponding Method object.

        If the specified method cannot be found directly within the designated type then extended super types or interfaces are searched for.

        Parameters:
        typeToScan - The type that should be scanned for the specified method.
        methodName - The name of the method that should be returned.
        methodParameterTypes - The declared parameter types of the designated method.
        paramValues - The parameter values for the method. This is used to check for whether narrowing to a corresponding non-varargs method is valid. If this is null, no var args method will be searched.
        logger - The logger for problems and progress. This may be null.
        Returns:
        The Method object representing the designated method and the appropriate parameter values.
        Throws:
        SecurityException - If the given type cannot be scanned due to a security violation, a SecurityException is thrown.
        NoSuchMethodException - If the method cannot be found within the given type and all its sub types, a NoSuchMethodException is thrown.
      • getCorrespondingVarArgsMethod

        public static MethodWrapper getCorrespondingVarArgsMethod​(String name,
                                                                  Class<?>[] paramTypes,
                                                                  Object[] paramValues,
                                                                  Class<?> searchedClass)
        Gets the method from the designated class that corresponds to the designated method but has an additional varargs parameter or the designated method has an varargs parameter but the returned method from the designated class has not. If no corresponding method is found or there are two methods found with an additional varargs parameter so we cannot decide which one to choose, null will be returned.
        This also applies to constructors. With "corresponding" including name equality, this method is of restricted use for constructors since they need to have the same (class) name. A corresponding method without a varargs parameter will only be searched if parameter values are provided and the last one is null or an array only containing null. In this case we expect the call to the varargs method equal to calling the method without the last parameter and the varargs parameter to be irrelevant.
        Parameters:
        name - The name of the method/constructor to get the corresponding method/constructor for.
        paramTypes - The parameter types of the method/constructor to get a corresponding method/constructor for. This must not be null.
        paramValues - The parameter values of the method/constructor. This is only relevant when looking for a corresponding non-varargs method/constructor. In all other cases simply use null.
        searchedClass - The class to get the corresponding method/constructor having an additional varargs parameter or not having the varargs parameter.
        Returns:
        The method/constructor corresponding to the designated method by having an additional varargs parameter or not having the varargs parameter and the last parameter value being null. If no such method/constructor is found or if there are several methods/constructors found matching this criteria, null will be returned.
      • visitClassHierarchy

        public static <T> T visitClassHierarchy​(Class<?> cls,
                                                ReflectionTools.ClassVisitor<T> visitor,
                                                boolean includeTransIfaces)
        Visits the (complete) class hierarchy (interfaces, parent classes and their interfaces as well as all parent interfaces if desired) of the designated class with the designated visitor.
        Parameters:
        cls - The class for which to visit the hierarchy.
        visitor - The visitor with which to visit each class of the hierarchy.
        includeTransIfaces - Whether to include transitive interfaces. If so, parent interfaces of interfaces will be considered. Otherwise only the direct interfaces of this class and its parent classes will be considered.
        Returns:
        The result of the visits.
      • getJavadocRefType

        protected static StringBuilder getJavadocRefType​(Class<?> cls)
        Gets a string representation for the designated class used in Javadoc.
        Parameters:
        cls - The class for which to get the javadoc-compatible string representation.
        Returns:
        A StringBuilder containing the javadoc-compatible string representation of the designated class.
      • getJavadocRefString

        public static String getJavadocRefString​(Class<?> cls)
        Gets the name and path of the Javadoc HTML file (relative, without leading "/" for the designated class.
        Parameters:
        cls - The class for which to get the name and path of the corresponding Javadoc HTML file.
        Returns:
        The name and path of the Javadoc HTML file (relative, without leading "/" for the designated class.
      • getJavadocRefString

        public static String getJavadocRefString​(Method m)
        Gets fragment identifier of the designated method including the name and path of the Javadoc HTML file (relative, without leading "/" of the corresponding declaring class.
        Parameters:
        m - The method for which to get the path and the fragment identifier of the corresponding Javadoc file.
        Returns:
        The fragment identifier of the designated method including the name and path of the Javadoc HTML file (relative, without leading "/" of the corresponding declaring class.
      • getGenericParents

        public static Map<Class<?>,​Map<TypeVariable<?>,​Type>> getGenericParents​(Type t)
        Gets the transitive parent classes and interfaces of the designated type mapped to the type variables these classes and interfaces declare and which are mapped to a type. This type will either be a Class for a completely bound type, a TypeVariable providing the most specific bounds found for the type or null if it cannot be resolved.
        Parameters:
        t - The type for which to transitively retrieve all parent classes and interfaces including all used type variables resolved to the corresponding type where possible.
        Returns:
        The transitive parent classes and interfaces of the designated type mapped to the used type variables including the corresponding type or at least a type variable with the most specific bounds where possible.
      • addGenericParents

        protected static Map<Class<?>,​Map<TypeVariable<?>,​Type>> addGenericParents​(Type current,
                                                                                               Map<TypeVariable<?>,​Type> subTypeArguments,
                                                                                               Map<Class<?>,​Map<TypeVariable<?>,​Type>> result)
        Adds the transitive parent classes and interfaces of the designated type to the designated result map (which is also returned). This requires the mapping of type variable and corresponding resolved types of the corresponding subtype. This subtype may provide types for type variables or the (most specific) type variables with appropriate bounds.
        Parameters:
        current - The type for which to transitively retrieve all parent classes and interfaces including all used type variables resolved to the corresponding type where possible.
        subTypeArguments - The type variables mapped to the corresponding type where possible of the direct subtype of the designated type.
        result - The subtypes of the designated type investigated so far mapped to the used type variables including the corresponding type or at least the most specific bounds where possible.
        Returns:
        The transitive parent classes and interfaces of the designated type mapped to the used type variables including the corresponding type or at least the most specific bounds where possible.
      • overrides

        public static boolean overrides​(Class<?> overridingCls,
                                        Method overriding,
                                        Class<?> overriddenCls,
                                        Method overridden)
        Gets whether the designated first method overrides the second one. For this the method name has to be equal, the owning class of the overridden method needs to be assignable from the owning class of the overriding method and the types of all parameters need to be equal.
        Parameters:
        overridingCls - The class containing the overriding method.
        overriding - The method which to check for whether it overrides the other method.
        overriddenCls - The class containing the overridden method.
        overridden - The method which to check for whether it is overridden by the other method.
        Returns:
        Whether the designated first method overrides (or equals) the second one.
      • sortSubclassRelation

        public static List<Class<?>> sortSubclassRelation​(Collection<Class<?>> coll)
        Sorts the designated classes according to their relation, that is, subclasses are successors of the corresponding parent classes/interfaces. Note that classes may be unrelated. In this case the order is arbitrary. Also the order of sibling classes that have a transitive relation via a parent class/interface, is undefined with respect to each other. But their parent classes and subclasses are predecessors of both classes.
        Therefore the returned list generally contains the most common ancestor classes in front and the most specific classes at the end.
        Parameters:
        coll - A collection of classes about to be ordered based on their relation.
        Returns:
        A list of classes ordered by their relation, that is, common ancestors before the corresponding descendants.
      • compare

        protected static byte compare​(Class<?> c1,
                                      Class<?> c2)
        Compares the designated classes, -1 if c1 is assignable (a subclass/implementation) of c2, 1 if c2 is assignable (a subclass/implementation) of c1, 0 if they are unrelated and Byte.MIN_VALUE if they are equal.
        Parameters:
        c1 - The first class which to compare (must not be null).
        c2 - The second class which to compare (must not be null).
        Returns:
        -1 if c1 is assignable (a subclass/implementation) of c2, 1 if c2 is assignable (a subclass/implementation) of c1, 0 if they are unrelated and Byte.MIN_VALUE if they are equal.
      • unwrapIte

        public static Throwable unwrapIte​(InvocationTargetException ite,
                                          Method m,
                                          Object o,
                                          boolean unwrapUnchecked)
        Unwraps an exception declared by the designated Method from the designated InvocationTargetException or returns the designated InvocationTargetException. This is needed since all throwables thrown by a dynamically invoked method will be wrapped by an InvocationTargetException. For transparent handling, unwrapping is required.
        Besides the declared exceptions, unchecked exceptions can also be unwrapped since they do not need to be declared.
        Parameters:
        ite - The InvocationTargetException thrown by a dynamically invoked method.
        m - The method throwing the designated InvocationTargetException.
        o - The object the method has been invoked on or null. The object allows to retrieve more specific type information in case of the type of the exceptions type variables. The object may provide additional type information compared to a method called on a generic superclass.
        unwrapUnchecked - Whether to also unwrap an unchecked exception (RuntimeException or Error) that is the cause of the designated InvocationTargetException.
        Returns:
        A throwable (checked, runtime exception or error) declared by the designated method (or optionally unchecked) being the cause for the designated InvocationTargetException or the designated InvocationTargetException itself if the cause is not declared by the designated method (and not unchecked).