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 host, Field f, Object value)
set Field Value
f.setAccessible(true);
unsafeSetFieldValue(host, f, value);
ObjectsetFieldValue(Object input, Object value, String fieldName)
set Field Value
Class searchClass = input.getClass();
while (searchClass != null) {
    Field[] fields = searchClass.getDeclaredFields();
    for (Field field : fields) {
        if (field.getName().equals(fieldName)) {
            field.setAccessible(true);
            if (field.getType().getName().equals("java.lang.Long")) {
                if (value instanceof String) {
...
voidsetFieldValue(Object instance, Field field, Object value)
Sets a field value via reflection.
try {
    field.set(instance, value);
} catch (IllegalAccessException e) {
    throw new IllegalStateException(e);
voidsetFieldValue(Object instanceContainingField, String fieldName, Object fieldValue)
Sets a value to a field using reflection even if the field is private.
Class<?> targetClass = instanceContainingField.getClass();
while (targetClass != null) {
    for (Field field : targetClass.getDeclaredFields()) {
        if (field.getName().equalsIgnoreCase(fieldName)) {
            try {
                boolean isAccessible = field.isAccessible();
                try {
                    field.setAccessible(true);
...
voidsetFieldValue(Object o, Class baseClass, Class fieldType, T value)
set Field Value
if (o == null) {
    throw new NullPointerException();
if (!baseClass.isAssignableFrom(o.getClass())) {
    throw new IllegalArgumentException(
            "Got a " + o.getClass().getName() + " but expected a " + baseClass.getName());
for (Field f : baseClass.getDeclaredFields()) {
...
voidSetFieldValue(Object o, String field, Object object)
Set Field Value
Field _field = o.getClass().getDeclaredField(field);
_field.setAccessible(true);
_field.set(o, object);
voidsetFieldValue(Object obj, Field f, Object value)
set Field Value
try {
    boolean accessible = f.isAccessible();
    try {
        f.setAccessible(true);
        f.set(obj, value);
    } finally {
        f.setAccessible(accessible);
} catch (IllegalArgumentException e) {
    throw e;
} catch (IllegalAccessException e) {
    assert false;
voidsetFieldValue(Object obj, Field field, Object value)
set Field Value
field.setAccessible(true);
field.set(obj, value);
voidsetFieldValue(Object obj, Field field, Object value)
set Field Value
field.setAccessible(true);
try {
    field.set(obj, value);
} catch (IllegalArgumentException e) {
    throw new RuntimeException("IllegalArgumentException", e);
} catch (IllegalAccessException e) {
    throw new RuntimeException("IllegalAccessExceptione", e);
voidsetFieldValue(Object obj, String field, Object value)
set Field Value
setFieldValue(obj, getField(obj.getClass(), field), value);