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(Object target, Class targetClass, String fieldName, Object value)
set Field Value
try {
    Field field = getAccessibleField((target != null) ? target.getClass() : targetClass, fieldName);
    field.set(target, value);
} catch (Exception exception) {
    boolean instanceFieldRequested = (target != null);
    throw new IllegalAccessException(String.format("Unable to set field '%s' on %s '%s' due to %s: %s",
            fieldName, instanceFieldRequested ? "object" : "class",
            instanceFieldRequested ? target : targetClass.getName(), exception.getClass().getSimpleName(),
...
voidsetFieldValue(Object target, Field field, Object newValue)
set Field Value
boolean accessible = field.isAccessible();
field.setAccessible(true);
try {
    field.set(target, newValue);
} catch (IllegalAccessException e) {
    throw new NoSuchFieldException("No such field: " + target.getClass() + '.' + field.getName());
} finally {
    field.setAccessible(accessible);
...
voidsetFieldValue(Object target, Field field, Object value)
set Field Value
if (field.isAccessible()) {
    field.set(target, value);
} else {
    field.setAccessible(true);
    field.set(target, value);
    field.setAccessible(false);
voidsetFieldValue(Object target, Object mock, final Field field)
set Field Value
field.setAccessible(true);
removeFinalModifier(field);
field.set(target, mock);
voidsetFieldValue(Object target, String field, Object value)
set Field Value
try {
    Class<?> obj = target.getClass();
    Field[] fields = obj.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        fields[i].setAccessible(true);
        if (field.equals(fields[i].getName())) {
            fields[i].set(target, value);
            break;
...
voidsetFieldValue(Object target, String fieldName, Object fieldValue)
Sets the field value by using reflection.
try {
    Field targetField = findFieldFromClassHierarchy(target.getClass(), fieldName);
    targetField.setAccessible(true);
    targetField.set(target, fieldValue);
} catch (Exception ex) {
    throw new RuntimeException(
            String.format("Cannot set the value of the field: %s because of an error", fieldName), ex);
voidsetFieldValue(Object target, String fieldName, Object value)
set Field Value
try {
    getField(target, fieldName).set(target, value);
} catch (IllegalAccessException e) {
    throw new RuntimeException(e);
} catch (NoSuchFieldException e) {
    throw new RuntimeException(e);
booleansetFieldValue(Object target, String fieldName, Object value)
set Field Value
return setFieldValue(target, fieldName, value, false);
voidsetFieldValue(Object target, String fieldName, String fieldValue)
set Field Value
Field field = getField(target, fieldName);
if (field != null) {
    field.setAccessible(true);
    try {
        field.set(target, fieldValue);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
voidsetFieldValue(Object target, String fname, Class ftype, Object fvalue)
set Field Value
if ((target == null) || (fname == null) || ("".equals(fname))
        || ((fvalue != null) && (!ftype.isAssignableFrom(fvalue.getClass())))) {
    return;
Class clazz = target.getClass();
try {
    Method method = clazz.getDeclaredMethod(
            "set" + Character.toUpperCase(fname.charAt(0)) + fname.substring(1), new Class[] { ftype });
...