Class ReflectUtils


  • public class ReflectUtils
    extends Object
    Methods that help in doing common reflection tasks.
    • Constructor Detail

      • ReflectUtils

        public ReflectUtils()
    • Method Detail

      • getPrivateMethod

        public static Method getPrivateMethod​(Class<?> c,
                                              String name,
                                              Class<?>... args)
                                       throws NoSuchMethodException
        Look up a private method and make it accessible for testing.
        Parameters:
        c - Class to look in.
        name - Name of the method.
        args - Arguments for the method. Note that primitive types are found using XXX.TYPE.
        Returns:
        Method object, accessible for calling.
        Throws:
        NoSuchMethodException
      • getPrivateField

        public static <T> Field getPrivateField​(Class<?> c,
                                                String fieldName)
                                         throws NoSuchFieldException
        Look up a private field and make it accessible for testing.
        Parameters:
        c - The class that declares the field.
        fieldName - The name of the field.
        Returns:
        The field, which can now be set.
        Throws:
        NoSuchFieldException - If there is no such field declared in the class.
      • getPrivateConstructor

        public static <T> Constructor<T> getPrivateConstructor​(Class<T> c,
                                                               Class<?>... args)
                                                        throws NoSuchMethodException
        Look up a private constructor and make it accessible for testing.
        Parameters:
        c - Class to look in.
        args - Arguments for the constructor. Note that primitive types are found using XXX.TYPE.
        Returns:
        Constructor object, accessible for calling.
        Throws:
        NoSuchMethodException
      • testUtilityConstructor

        public static void testUtilityConstructor​(Class c)
        Method for testing the constructor of a utility class (the constructor should be private).