Class ReflectionRecipes


  • public final class ReflectionRecipes
    extends java.lang.Object
    Utility class to handle reflection.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.util.Map<java.lang.Class<?>,​java.util.List<java.lang.Class<?>>> ASSIGNABLE_MAPPING  
      private static java.util.Map<java.lang.Class<?>,​java.lang.Class<?>> PRIMITIVE_MAPPING  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ReflectionRecipes()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static boolean canInject​(Parameter parameter, InjectableParameter injectableParameter)  
      static java.lang.Class<?>[] classesFromParameters​(Parameter[] parameters)
      Extracts class instances from parameters.
      static boolean exactMatch​(java.lang.Class<?>[] classes, java.lang.Object[] args)
      Matches an array of class instances to an array of instances.
      static boolean exactMatch​(Parameter[] parameters, java.lang.Object[] args)
      Matches an array of parameters to an array of instances.
      static Parameter[] filter​(Parameter[] parameters, java.util.Set<InjectableParameter> filters)
      Omits 1.
      static Parameter[] getConstructorParameters​(java.lang.reflect.Constructor<?> constructor)
      Extracts constructor parameters.
      static Parameter[] getMethodParameters​(java.lang.reflect.Method method)
      Extracts method parameters.
      private static Parameter[] getParameters​(java.lang.Class<?>[] parametersTypes, java.lang.annotation.Annotation[][] parametersAnnotations)  
      private static void initAssignableMapping()  
      private static void initPrimitiveMapping()  
      static java.lang.Object[] inject​(Parameter[] parameters, java.util.Set<InjectableParameter> filters, java.lang.Object[] args, java.lang.reflect.Constructor<?> constructor, ITestContext context, ITestResult testResult)
      Injects appropriate arguments.
      static java.lang.Object[] inject​(Parameter[] parameters, java.util.Set<InjectableParameter> filters, java.lang.Object[] args, java.lang.reflect.Method injectionMethod, ITestContext context, ITestResult testResult)
      Injects appropriate arguments.
      static boolean isInstanceOf​(java.lang.Class<?> reference, java.lang.Object object)
      Checks if an instance is an instance of the given class.
      static boolean isOrExtends​(java.lang.Class<?> reference, java.lang.Class<?> clazz)
      Checks a class instance for being a given class or its sub-class.
      static boolean isOrImplementsInterface​(java.lang.Class<?> reference, java.lang.Class<?> clazz)
      Checks a class instance for being a given interface or its implementation.
      static boolean lenientMatch​(java.lang.Class<?>[] classes, java.lang.Object[] args)
      Matches an array of class instances to an array of instances.
      static boolean lenientMatch​(Parameter[] parameters, java.lang.Object[] args)
      Matches an array of parameters to an array of instances.
      static boolean matchArrayEnding​(java.lang.Class<?>[] classes, java.lang.Object[] args)
      Matches an array of class instances to an array of instances having last class instance an array.
      static boolean matchArrayEnding​(Parameter[] parameters, java.lang.Object[] param)  
      private static java.lang.Object[] nativelyInject​(Parameter[] parameters, java.util.Set<InjectableParameter> filters, java.lang.Object[] args, java.lang.Object injectionMethod, ITestContext context, ITestResult testResult)  
      • Methods inherited from class java.lang.Object

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

      • PRIMITIVE_MAPPING

        private static final java.util.Map<java.lang.Class<?>,​java.lang.Class<?>> PRIMITIVE_MAPPING
      • ASSIGNABLE_MAPPING

        private static final java.util.Map<java.lang.Class<?>,​java.util.List<java.lang.Class<?>>> ASSIGNABLE_MAPPING
    • Constructor Detail

      • ReflectionRecipes

        private ReflectionRecipes()
    • Method Detail

      • initPrimitiveMapping

        private static void initPrimitiveMapping()
      • initAssignableMapping

        private static void initAssignableMapping()
      • isInstanceOf

        public static boolean isInstanceOf​(java.lang.Class<?> reference,
                                           java.lang.Object object)
        Checks if an instance is an instance of the given class.
        Parameters:
        reference - reference class.
        object - instance to be tested.
        Returns:
        is an instance-of or not
      • isOrImplementsInterface

        public static boolean isOrImplementsInterface​(java.lang.Class<?> reference,
                                                      java.lang.Class<?> clazz)
        Checks a class instance for being a given interface or its implementation.
        Parameters:
        reference - reference interface instance.
        clazz - class instance to be tested.
        Returns:
        would an instance of 'clazz' be an instance of reference interface.
      • isOrExtends

        public static boolean isOrExtends​(java.lang.Class<?> reference,
                                          java.lang.Class<?> clazz)
        Checks a class instance for being a given class or its sub-class.
        Parameters:
        reference - reference class instance.
        clazz - class instance to be tested.
        Returns:
        would an instance of 'clazz' be an instance of reference class.
      • classesFromParameters

        public static java.lang.Class<?>[] classesFromParameters​(Parameter[] parameters)
        Extracts class instances from parameters.
        Parameters:
        parameters - an array of parameters.
        Returns:
        parameter types.
      • getMethodParameters

        public static Parameter[] getMethodParameters​(java.lang.reflect.Method method)
        Extracts method parameters.
        Parameters:
        method - any valid method.
        Returns:
        extracted method parameters.
      • getConstructorParameters

        public static Parameter[] getConstructorParameters​(java.lang.reflect.Constructor<?> constructor)
        Extracts constructor parameters.
        Parameters:
        constructor - any valid constructor.
        Returns:
        extracted constructor parameters.
      • getParameters

        private static Parameter[] getParameters​(java.lang.Class<?>[] parametersTypes,
                                                 java.lang.annotation.Annotation[][] parametersAnnotations)
      • matchArrayEnding

        public static boolean matchArrayEnding​(java.lang.Class<?>[] classes,
                                               java.lang.Object[] args)
        Matches an array of class instances to an array of instances having last class instance an array.

        Assuming upper case letters denote classes and corresponding lowercase its instances. Classes {A,B,C...}, instances {a,b,c1,c2} ==> check for {a,b,{c1,c2}} match or Classes {A,B,C[]}, instances {a,b,c1,c2} ==> check for {a,b,{c1,c2}} match both of the above cases are equivalent.

        Parameters:
        classes - array of class instances to check against.
        args - instances to be verified.
        Returns:
        matches or not
      • exactMatch

        public static boolean exactMatch​(Parameter[] parameters,
                                         java.lang.Object[] args)
        Matches an array of parameters to an array of instances.
        Returns:
        matches or not
        See Also:
        exactMatch(Class[], Object[])
      • exactMatch

        public static boolean exactMatch​(java.lang.Class<?>[] classes,
                                         java.lang.Object[] args)
        Matches an array of class instances to an array of instances.
        Parameters:
        classes - array of class instances to check against.
        args - instances to be verified.
        Returns:
        matches or not
      • lenientMatch

        public static boolean lenientMatch​(Parameter[] parameters,
                                           java.lang.Object[] args)
        Matches an array of parameters to an array of instances.
        Returns:
        matches or not
        See Also:
        lenientMatch(Class[], Object[])
      • lenientMatch

        public static boolean lenientMatch​(java.lang.Class<?>[] classes,
                                           java.lang.Object[] args)
        Matches an array of class instances to an array of instances. Such that {int, boolean, float} matches {int, boolean}
        Parameters:
        classes - array of class instances to check against.
        args - instances to be verified.
        Returns:
        matches or not
      • filter

        public static Parameter[] filter​(Parameter[] parameters,
                                         java.util.Set<InjectableParameter> filters)
        Omits 1. org.testng.ITestContext or its implementations from input array 2. org.testng.ITestResult or its implementations from input array 3. org.testng.xml.XmlTest or its implementations from input array 4. First method depending on filters.

        An example would be Input: {ITestContext.class, int.class, Boolean.class, TestContext.class} Output: {int.class, Boolean.class}

        Parameters:
        parameters - array of parameter instances under question.
        filters - filters to use.
        Returns:
        Injects free array of class instances.
      • inject

        public static java.lang.Object[] inject​(Parameter[] parameters,
                                                java.util.Set<InjectableParameter> filters,
                                                java.lang.Object[] args,
                                                java.lang.reflect.Method injectionMethod,
                                                ITestContext context,
                                                ITestResult testResult)
        Injects appropriate arguments.
        Parameters:
        parameters - array of parameter instances under question.
        filters - filters to use.
        args - user supplied arguments.
        injectionMethod - current test method.
        context - current test context.
        testResult - on going test results.
        Returns:
        injected arguments.
      • inject

        public static java.lang.Object[] inject​(Parameter[] parameters,
                                                java.util.Set<InjectableParameter> filters,
                                                java.lang.Object[] args,
                                                java.lang.reflect.Constructor<?> constructor,
                                                ITestContext context,
                                                ITestResult testResult)
        Injects appropriate arguments.
        Parameters:
        parameters - array of parameter instances under question.
        filters - filters to use.
        args - user supplied arguments.
        constructor - current test method.
        context - current test context.
        testResult - on going test results.
        Returns:
        injected arguments.