Java Utililty Methods Reflection Field Value Set

List of utility methods to do Reflection Field Value Set

Description

The list of methods to do Reflection Field Value Set are organized into topic(s).

Method

voidsetFieldValue(Class c, Object instance, String name, Object value)
set Field Value
try {
    Field declaredField = c.getDeclaredField(name);
    declaredField.setAccessible(true);
    declaredField.set(instance, value);
} catch (IllegalAccessException e) {
    throw new RuntimeException(e);
} catch (NoSuchFieldException e) {
    throw new RuntimeException(e);
...
voidsetFieldValue(Class clazz, Object instance, String fieldName, T value)
Sets the value of a field using Reflection API.
try {
    getField(clazz, fieldName).set(instance, value);
} catch (IllegalAccessException e) {
    throw new RuntimeException(e);
voidsetFieldValue(Class clazz, Object target, String fieldName, Object value)
Set class field value, returns success status
HashMap<String, Object> result = new HashMap<>();
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(target, value);
voidsetFieldValue(Class clazz, E instance, Object value, String... names)
set Field Value
try {
    Field field = getField(clazz, names);
    if (field != null)
        field.set(instance, value);
} catch (Exception e) {
booleansetFieldValue(Class clazz, Object entity, String fieldName, V value)
set Field Value
try {
    Field f = clazz.getDeclaredField(fieldName);
    f.setAccessible(true);
    f.set(entity, value);
    return true;
} catch (Exception ex) {
    throw new RuntimeException("Cannot set field " + fieldName + " value to " + value, ex);
voidsetFieldValue(Class clazz, String fieldName, Object value)
set Field Value
setFieldValue(clazz, null, fieldName, value);
voidsetFieldValue(Class clazz, String fieldName, Object value)
Set static field value.
final Field field = findField(clazz, fieldName);
try {
    field.set(null, value);
} catch (Exception e) {
    throw new RuntimeException(e);
TsetFieldValue(Class expectedType, Object target, String fieldName, T value)
set Field Value
try {
    Class<? extends Object> targetClass = target.getClass();
    while (targetClass != null) {
        Field field = locateField(expectedType, targetClass, fieldName);
        if (field != null) {
            T oldValue = (T) field.get(target);
            field.set(target, value);
            return oldValue;
...
voidsetFieldValue(Field f, Object target, Object value)
Convenience method; Wraps Field#set(Object,Object) to propagate RuntimeException s.
try {
    f.set(target, value);
} catch (IllegalArgumentException | IllegalAccessException e) {
    throw Throwables.propagate(e);
TsetFieldValue(Field field, Object instance, T value)
Sets the value for the field of the instance
if (instance != null) {
    boolean oldAccessible = field.isAccessible();
    field.setAccessible(true);
    field.set(instance, value);
    field.setAccessible(oldAccessible);
    return value;
return null;
...