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(Object service, String name, Object obj)
set Field
Field f = service.getClass().getDeclaredField(name);
f.setAccessible(true);
try {
    f.set(service, obj);
} catch (Exception e) {
    e.printStackTrace();
voidsetField(Object source, String fieldName, Object value)
set Field
Class cl = source.getClass();
Field f = cl.getDeclaredField(fieldName);
f.setAccessible(true);
f.set(source, value);
voidsetField(Object sourceObj, Object targetObj, Field valueField, List targetFields)
set Field
valueField.setAccessible(true);
for (Field target : targetFields) {
    if (target.getName().equals(valueField.getName())) {
        target.setAccessible(true);
        try {
            target.set(targetObj, valueField.get(sourceObj));
        } catch (IllegalAccessException e) {
            e.printStackTrace();
...
voidsetField(Object target, Field field, Object value)
Set target Object field to a certain value
try {
    field.set(target, value);
} catch (IllegalAccessException e) {
    throw new IllegalArgumentException("Field " + field + " could not be set", e);
voidsetField(Object target, Field field, Object value)
set Field
try {
    field.setAccessible(true);
    field.set(target, value);
} catch (IllegalAccessException ex) {
    throw new RuntimeException(
            "Unable to set field '" + field.getName() + "' for class " + target.getClass().getName(), ex);
} catch (IllegalArgumentException ex) {
    throw new RuntimeException(
...
voidsetField(Object target, String fieldName, Class fieldType, T value)
set Field
Field field = findField(target.getClass(), fieldName, fieldType);
if (field == null) {
    throw new RuntimeException("unknown given field '" + fieldName + "'");
makeAccessible(field);
try {
    field.set(target, value);
} catch (IllegalArgumentException e) {
...
voidsetField(Object target, String fieldname, Object value)
Try to directly set a (possibly private) field on an Object.
Field field = findDeclaredField(target.getClass(), fieldname);
field.setAccessible(true);
field.set(target, value);
voidsetField(Object target, String name, Object value)
Set the java.lang.reflect.Field field with the given name on the provided Object target object to the supplied value.
try {
    Field field = findField(target.getClass(), name);
    if (field == null) {
        throw new IllegalArgumentException(
                "Could not find field [" + name + "] on target [" + target + "]");
    field.setAccessible(true);
    field.set(target, value);
...
booleansetField(Object target, String name, Object value)
Set the field identified by name to the given value.
try {
    Field field = target.getClass().getDeclaredField(name);
    field.setAccessible(true);
    field.set(target, value);
    return true;
} catch (NoSuchFieldException e) {
    return false;
} catch (Exception e) {
...
voidsetField(Object target, String name, Object value)
set Field
Class<?> theClass = null;
if (target == null)
    throw new RuntimeException("no target object or class in setField(...)");
if (target instanceof Class<?>) {
    theClass = (Class<?>) target;
} else {
    theClass = target.getClass();
Field theField = null;
while (theClass != null) {
    try {
        theField = theClass.getDeclaredField(name);
        break;
    } catch (NoSuchFieldException e) {
        theClass = theClass.getSuperclass();
if (theField == null) {
    throw new RuntimeException(
            "Didn't find the field " + name + " on " + target.toString() + " or any of its superclasses.");
try {
    theField.setAccessible(true);
    theField.set(target, value);
} catch (SecurityException e) {
    throw new RuntimeException(e);
} catch (IllegalArgumentException e) {
    throw new RuntimeException(e);
} catch (IllegalAccessException e) {
    throw new RuntimeException(e);