Class ReflectUtils


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

      Constructors 
      Constructor Description
      ReflectUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.lang.reflect.Constructor<T> getPrivateConstructor​(java.lang.Class<T> c, java.lang.Class<?>... args)
      Look up a private constructor and make it accessible for testing.
      static <T> java.lang.reflect.Field getPrivateField​(java.lang.Class<?> c, java.lang.String fieldName)
      Look up a private field and make it accessible for testing.
      static java.lang.reflect.Method getPrivateMethod​(java.lang.Class<?> c, java.lang.String name, java.lang.Class<?>... args)
      Look up a private method and make it accessible for testing.
      static void testUtilityConstructor​(java.lang.Class c)
      Method for testing the constructor of a utility class (the constructor should be private).
      • Methods inherited from class java.lang.Object

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

      • getPrivateMethod

        public static java.lang.reflect.Method getPrivateMethod​(java.lang.Class<?> c,
                                                                java.lang.String name,
                                                                java.lang.Class<?>... args)
                                                         throws java.lang.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:
        java.lang.NoSuchMethodException
      • getPrivateField

        public static <T> java.lang.reflect.Field getPrivateField​(java.lang.Class<?> c,
                                                                  java.lang.String fieldName)
                                                           throws java.lang.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:
        java.lang.NoSuchFieldException - If there is no such field declared in the class.
      • getPrivateConstructor

        public static <T> java.lang.reflect.Constructor<T> getPrivateConstructor​(java.lang.Class<T> c,
                                                                                 java.lang.Class<?>... args)
                                                                          throws java.lang.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:
        java.lang.NoSuchMethodException
      • testUtilityConstructor

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