Class ReflectionUtils


  • public final class ReflectionUtils
    extends Object
    Utility methods for doing reflection.
    • Method Detail

      • loadClass

        public static <T> Class<T> loadClass​(Class<?> base,
                                             String name)
      • newInstance

        public static <T> T newInstance​(Class<T> clazz,
                                        Object... params)
      • getByPath

        public static Object getByPath​(Object target,
                                       List<String> path)
        Evaluates the given path expression on the given object and returns the object found.
        Parameters:
        target - the object to reflect on
        path - the path to evaluate
        Returns:
        the result of evaluating the path against the given object
      • getAllByPath

        public static List<Object> getAllByPath​(Object target,
                                                List<String> path)
        Evaluates the given path expression and returns the list of all matching objects. If the path expression does not contain any wildcards, this will return a list of at most one item. If the path contains one or more wildcards, the returned list will include the full set of values obtained by evaluating the expression with all legal value for the given wildcard.
        Parameters:
        target - the object to evaluate the expression against
        path - the path expression to evaluate
        Returns:
        the list of matching values
      • setByPath

        public static void setByPath​(Object target,
                                     Object value,
                                     List<String> path)
        Sets the value of the attribute at the given path in the target object, creating any intermediate values (using the default constructor for the type) if need be.
        Parameters:
        target - the object to modify
        value - the value to add
        path - the path into the target object at which to add the value
      • createDefaultInstance

        public static Object createDefaultInstance​(String type)
      • findAccessor

        public static Method findAccessor​(Object target,
                                          String propertyName)
        Returns the accessor method for the specified member property. For example, if the member property is "Foo", this method looks for a "getFoo()" method and an "isFoo()" method. If no accessor is found, this method throws an IllegalStateException.
        Parameters:
        target - the object to reflect on
        propertyName - the name of the property to search for
        Returns:
        the accessor method
        Throws:
        IllegalStateException - if no matching method is found
      • findMethod

        public static Method findMethod​(Object target,
                                        String name,
                                        Class<?> parameterType)
        Finds a method of the given name that will accept a parameter of the given type. If more than one method matches, returns the first such method found.
        Parameters:
        target - the object to reflect on
        name - the name of the method to search for
        parameterType - the type of the parameter to be passed
        Returns:
        the matching method
        Throws:
        IllegalStateException - if no matching method is found
      • setField

        public static void setField​(Object instance,
                                    Field field,
                                    Object arg)
      • getField

        public static <T> Object getField​(T instance,
                                          Field field)
      • newInstanceWithAllFieldsSet

        public static <T> T newInstanceWithAllFieldsSet​(Class<T> clz)
      • newInstanceWithAllFieldsSet

        public static <T> T newInstanceWithAllFieldsSet​(Class<T> clz,
                                                        software.amazon.awssdk.testutils.smoketest.ReflectionUtils.RandomSupplier<?>... suppliers)
      • newInstanceWithAllFieldsSet

        public static <T> T newInstanceWithAllFieldsSet​(Class<T> clz,
                                                        List<software.amazon.awssdk.testutils.smoketest.ReflectionUtils.RandomSupplier<?>> suppliers)