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(Field field, Object obj, Object value)
set Field Value
if (field != null) {
    try {
        field.set(obj, value);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
voidsetFieldValue(Field field, Object object, Object value)
set Field Value
try {
    field.set(object, value);
} catch (IllegalAccessException e) {
booleansetFieldValue(Field field, Object object, Object value)
set Field Value
try {
    field.set(object, value);
    return true;
} catch (IllegalAccessException e) {
    return false;
voidsetFieldValue(Field field, Object target, Object value)
set Field Value
try {
    field.setAccessible(true);
    field.set(target, value);
} catch (IllegalAccessException e) {
    throw new IllegalArgumentException("Cannot access (set) " + "[" + field + "] field in target [" + target
            + "]. " + "Reason: " + e.getMessage(), e);
voidsetFieldValue(Field field, Object value, Object instance)
set Field Value
boolean accessible = field.isAccessible();
if (!accessible) {
    field.setAccessible(true);
try {
    field.set(instance, value);
} catch (Exception ex) {
    throw new RuntimeException(ex);
...
voidsetFieldValue(Field field, Object value, Object target)
set Field Value
boolean declaredAccessible = field.isAccessible();
if (!declaredAccessible) {
    field.setAccessible(true);
field.set(target, value);
if (!declaredAccessible) {
    field.setAccessible(false);
voidsetFieldValue(final Class clazz, final String fieldName, final Object fieldValue)
Sets a new value of a static field.
assert clazz != null && fieldName != null;
final Field field = getField(clazz, fieldName);
field.setAccessible(true);
field.set(null, fieldValue);
voidsetFieldValue(final Field field, final Object instance, final Object value)
Sets the value of a Field , making it accessible if required.
makeAccessible(field);
if (!Modifier.isStatic(field.getModifiers())) {
    Objects.requireNonNull(instance, "No instance given for non-static field");
try {
    field.set(instance, value);
} catch (final IllegalAccessException e) {
    throw new UnsupportedOperationException(e);
...
voidsetFieldValue(final Field field, final Object obj, final Object value)
set Field Value
try {
    field.set(obj, value);
} catch (IllegalAccessException e) {
    throw new IllegalStateException(String.format("Failed to set field %s to %s", field, value), e);
voidsetFieldValue(final Field field, final Object value, final Object object)
set Field Value
if (!field.isAccessible()) {
    field.setAccessible(true);
field.set(object, value);