Java Utililty Methods Reflection Field Set

List of utility methods to do Reflection Field Set

Description

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

Method

voidsetField(final Object parent, final String name, final Object obj)
set Field
try {
    final Field f = findField(parent.getClass(), name);
    f.setAccessible(true);
    f.set(parent, obj);
} catch (final Exception e) {
    e.printStackTrace();
voidsetField(java.lang.Object toObj, String tcFieldName, java.lang.Object toNewValue)
set Field
try {
    Field loField = toObj.getClass().getDeclaredField(tcFieldName);
    loField.setAccessible(true);
    loField.set(toObj, toNewValue);
} catch (IllegalArgumentException ex) {
    throw new RuntimeException(ex);
} catch (IllegalAccessException ex) {
    throw new RuntimeException(ex);
...
voidsetField(Object instance, String fieldName, Object value)
Sets the value of the fieldName in a given instance by reflection.
Field field = instance.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(instance, value);
voidsetField(Object instance, String name, Object obj)
set Field
try {
    Field f = instance.getClass().getDeclaredField(name);
    f.setAccessible(true);
    f.set(instance, obj);
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
    throw new RuntimeException(e);
voidsetField(Object instance, String sField, Object value)
set Field
Field field = instance.getClass().getDeclaredField(sField);
field.setAccessible(true);
field.set(instance, value);
voidsetField(Object item, Field f, Object value)
set Field
try {
    f.setAccessible(true);
    f.set(item, value);
} catch (Exception e) {
    e.printStackTrace();
voidsetField(Object o, String fieldName, Object inject)
set Field
Field field = getAccessibleField(fieldName, o.getClass());
try {
    field.set(o, inject);
} catch (IllegalAccessException e) {
    e.printStackTrace();
voidsetField(Object o, String fieldName, Object value)
set Field
try {
    Field field = null;
    try {
        field = o.getClass().getField(fieldName);
    } catch (NoSuchFieldException e) {
        try {
            o.getClass().getDeclaredField(fieldName).setAccessible(true);
            field = o.getClass().getField(fieldName);
...
ObjectsetField(Object o, String fieldName, Object value)
Sets the value of a field on an object
Object oldVal = null;
try {
    final Field field = getFieldInt(o.getClass(), fieldName);
    field.setAccessible(true);
    oldVal = field.get(o);
    field.set(o, value);
} catch (Exception e) {
    throw new RuntimeException("Cannot get value of field " + fieldName, e);
...
voidsetField(Object o, String name, Object value)
set Field
try {
    field(o, name).set(o, value);
} catch (Exception e) {
    e.printStackTrace();
    throw new RuntimeException(e);