spring-framework / org.springframework.test.util / ReflectionTestUtils / getField

getField

@Nullable open static fun getField(targetObject: Any, name: String): Any

Get the value of the Field with the given name from the provided targetObject.

This method delegates to #getField(Object, Class, String), supplying null for the targetClass argument.

Parameters

targetObject - the target object from which to get the field; never null

name - the name of the field to get; never null

Return
the field's current value

See Also
#getField(Class, String)

@Nullable open static fun getField(targetClass: Class<*>, name: String): Any

Get the value of the static Field with the given name from the provided targetClass.

This method delegates to #getField(Object, Class, String), supplying null for the targetObject argument.

Parameters

targetClass - the target class from which to get the static field; never null

name - the name of the field to get; never null

Return
the field's current value

Since
4.2

See Also
#getField(Object, String)

@Nullable open static fun getField(@Nullable targetObject: Any, @Nullable targetClass: Class<*>, name: String): Any

Get the value of the Field with the given name from the provided targetObject/targetClass.

If the supplied targetObject is a proxy, it will be unwrapped allowing the field to be retrieved from the ultimate target of the proxy.

This method traverses the class hierarchy in search of the desired field. In addition, an attempt will be made to make non-public fields accessible, thus allowing one to get protected, private, and package-private fields.

Parameters

targetObject - the target object from which to get the field; may be null if the field is static

targetClass - the target class from which to get the field; may be null if the field is an instance field

name - the name of the field to get; never null

Return
the field's current value

Since
4.2

See Also
#getField(Object, String)#getField(Class, String)ReflectionUtils#findField(Class, String, Class)ReflectionUtils#makeAccessible(Field)ReflectionUtils#getField(Field, Object)AopTestUtils#getUltimateTargetObject(Object)