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

booleansetField(Object object, String fieldName, Object newValue, boolean isFindDeclaredField, boolean isUpwardFind)
set Field
boolean result = false;
Field field = getField(object.getClass(), fieldName, isFindDeclaredField, isUpwardFind);
if (field != null) {
    try {
        field.setAccessible(true);
        field.set(object, newValue);
        result = true;
    } catch (IllegalAccessException e) {
...
voidsetField(Object object, String fieldName, Object value)
set Field
try {
    Field field = getField(object, fieldName);
    field.set(object, value);
} catch (IllegalAccessException e) {
    throw new IllegalArgumentException(e);
voidsetField(Object object, String fieldName, Object value)
set Field
try {
    getField(object.getClass(), fieldName).set(object, value);
} catch (Exception unexpectedReflectionException) {
    throw new RuntimeException("unexpected exception ", unexpectedReflectionException);
voidsetField(Object object, String fieldName, Object value)
Set the field value of the field with the given name of the specified object.
try {
    Field field;
    if (object instanceof Class) {
        field = findField((Class) object, fieldName, null);
        object = null;
    } else {
        field = findField(object.getClass(), fieldName, null);
    field.setAccessible(true);
    field.set(object, value);
} catch (IllegalAccessException | IllegalArgumentException | SecurityException e) {
    throw new Error(e);
voidsetField(Object object, String fieldName, Object value)
set Field
try {
    Field field = object.getClass().getField(fieldName);
    field.set(object, value);
} catch (NoSuchFieldException | IllegalAccessException e) {
    e.printStackTrace();
voidsetField(Object object, String name, boolean value)
Set the field, even if it's private, to a boolean value.
Field field = findDeclaredField(object.getClass(), name);
field.setAccessible(true);
field.setBoolean(object, value);
voidsetField(Object object, String name, Object value)
set Field
Class<?> clazz = object.getClass();
Field field = clazz.getDeclaredField(name);
boolean accessable = field.isAccessible();
if (!accessable)
    field.setAccessible(true);
field.set(object, value);
if (!accessable)
    field.setAccessible(false);
...
voidsetField(Object object, String name, Object value)
set Field
try {
    Field field = object.getClass().getDeclaredField(name);
    boolean isPrivate = !field.isAccessible();
    if (isPrivate) {
        field.setAccessible(true);
    field.set(object, value);
    if (isPrivate) {
...
voidsetField(Object owner, String name, Object newValue, Class definedIn)
set Field
Field field = getFieldDescriptor(owner, name, definedIn);
field.set(owner, newValue);
voidsetField(Object owner, String targetClass, String fieldName, Object value)
Set object's field with custom value even it's private.
Class<?> ownerclass = getTargetclass(owner, targetClass);
Field field = ownerclass.getDeclaredField(fieldName);
if (!field.isAccessible()) {
    field.setAccessible(true);
field.set(owner, value);