Class ReflectionUtils
- java.lang.Object
-
- software.amazon.awssdk.testutils.smoketest.ReflectionUtils
-
public final class ReflectionUtils extends Object
Utility methods for doing reflection.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ObjectcreateDefaultInstance(String type)static MethodfindAccessor(Object target, String propertyName)Returns the accessor method for the specified member property.static MethodfindMethod(Object target, String name, Class<?> parameterType)Finds a method of the given name that will accept a parameter of the given type.static List<Object>getAllByPath(Object target, List<String> path)Evaluates the given path expression and returns the list of all matching objects.static ObjectgetByPath(Object target, List<String> path)Evaluates the given path expression on the given object and returns the object found.static <T> ObjectgetField(T instance, Field field)static Class<?>getParameterTypes(Object target, List<String> path)static <T> Class<T>loadClass(Class<?> base, String name)static <T> Class<T>loadClass(ClassLoader classloader, String name)static <T> TnewInstance(Class<T> clazz, Object... params)static <T> TnewInstanceWithAllFieldsSet(Class<T> clz)static <T> TnewInstanceWithAllFieldsSet(Class<T> clz, List<software.amazon.awssdk.testutils.smoketest.ReflectionUtils.RandomSupplier<?>> suppliers)static <T> TnewInstanceWithAllFieldsSet(Class<T> clz, software.amazon.awssdk.testutils.smoketest.ReflectionUtils.RandomSupplier<?>... suppliers)static voidsetByPath(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.static voidsetField(Object instance, Field field, Object arg)
-
-
-
Method Detail
-
loadClass
public static <T> Class<T> loadClass(ClassLoader classloader, String name)
-
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 onpath- 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 againstpath- 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 modifyvalue- the value to addpath- the path into the target object at which to add the value
-
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 onpropertyName- 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 onname- the name of the method to search forparameterType- the type of the parameter to be passed- Returns:
- the matching method
- Throws:
IllegalStateException- if no matching method is found
-
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)
-
-