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

setField

open static fun setField(targetObject: Any, name: String, @Nullable value: Any): Unit

Set the Field with the given name on the provided targetObject to the supplied value.

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

Parameters

targetObject - the target object on which to set the field; never null

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

value - the value to set

open static fun setField(targetObject: Any, @Nullable name: String, @Nullable value: Any, @Nullable type: Class<*>): Unit

Set the Field with the given name/type on the provided targetObject to the supplied value.

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

Parameters

targetObject - the target object on which to set the field; never null

name - the name of the field to set; may be null if type is specified

value - the value to set

type - the type of the field to set; may be null if name is specified

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

Set the static Field with the given name on the provided targetClass to the supplied value.

This method delegates to #setField(Object, Class, String, Object, Class), supplying null for the targetObject and type arguments.

Parameters

targetClass - the target class on which to set the static field; never null

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

value - the value to set

Since
4.2

open static fun setField(targetClass: Class<*>, @Nullable name: String, @Nullable value: Any, @Nullable type: Class<*>): Unit

Set the static Field with the given name/type on the provided targetClass to the supplied value.

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

Parameters

targetClass - the target class on which to set the static field; never null

name - the name of the field to set; may be null if type is specified

value - the value to set

type - the type of the field to set; may be null if name is specified

Since
4.2

open static fun setField(@Nullable targetObject: Any, @Nullable targetClass: Class<*>, @Nullable name: String, @Nullable value: Any, @Nullable type: Class<*>): Unit

Set the Field with the given name/type on the provided targetObject/targetClass to the supplied value.

If the supplied targetObject is a proxy, it will be unwrapped allowing the field to be set on 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 set protected, private, and package-private fields.

Parameters

targetObject - the target object on which to set the field; may be null if the field is static

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

name - the name of the field to set; may be null if type is specified

value - the value to set

type - the type of the field to set; may be null if name is specified

Since
4.2

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