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 object, String fieldName, Object fieldValue)
set Field Value
try {
    if (object instanceof Class)
        getField((Class<?>) object, fieldName).set(null, fieldValue);
    else
        getField(object.getClass(), fieldName).set(object, fieldValue);
} catch (Throwable t) {
    throw new IOException(t);
voidsetFieldValue(Object object, String fieldName, Object value)
Convenience method for setting the value of a private object field, without the stress of checked exceptions in the reflection API.
try {
    getDeclaredFieldInHierarchy(object.getClass(), fieldName).set(object, value);
} catch (IllegalArgumentException e) {
    throw new RuntimeException(e);
} catch (IllegalAccessException e) {
    throw new RuntimeException(e);
voidsetFieldValue(Object object, String fieldName, Object value)
set Field Value
Field field = getAccessibleField(object, fieldName);
if (field == null) {
    throw new IllegalArgumentException("Could not find field " + fieldName);
try {
    field.set(object, value);
} catch (IllegalAccessException e) {
voidsetFieldValue(Object object, String fieldName, Object value)
Sets the value of a field in an object.
try {
    Field field = getDeclaredFieldInHierarchy(object.getClass(), fieldName);
    if (field == null) {
        throw new RuntimeException(String.format("Class %s does not have field %s in its hierarchy.",
                object.getClass(), fieldName));
    field.setAccessible(true);
    field.set(object, value);
...
voidsetFieldValue(Object object, String fieldName, Object value)
set Field Value
Field field = getDeclaredField(object, fieldName);
if (!field.isAccessible()) {
    field.setAccessible(true);
try {
    field.set(object, value);
} catch (IllegalAccessException e) {
    throw e;
...
voidsetFieldValue(Object object, String name, Object value)
Utility method to set a field to a value.
try {
    Field field = getField(object.getClass(), name);
    if (!field.isAccessible()) {
        field.setAccessible(true);
    field.set(object, value);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
    throw new RuntimeException(
...
voidsetFieldValue(Object object, String name, Object value)
set Field Value
Field field = null;
boolean isAccessible = true;
try {
    field = object.getClass().getDeclaredField(name); 
    isAccessible = field.isAccessible();
    field.setAccessible(true);
    field.set(object, value);
} catch (NoSuchFieldException | IllegalAccessException e) {
...
voidsetFieldValue(Object objectInstance, String fieldName, Object valueToSet)
Sets the field value.
if (null == objectInstance)
    return;
Field fld = getFieldByName(objectInstance.getClass(), fieldName);
if (fld == null)
    throw new NoSuchFieldException("Field not found: " + fieldName);
if (!fld.isAccessible())
    fld.setAccessible(true);
fld.set(objectInstance, valueToSet);
...
voidsetFieldValue(Object pojo, Field field, Object value)
set Field Value
field.setAccessible(true);
field.set(pojo, value);
voidsetFieldValue(Object receiver, String fieldName, Object value)
set Field Value
Field field = getField(receiver, fieldName);
if (field == null) {
    return;
try {
    field.set(receiver, value);
} catch (IllegalAccessException e) {
    e.printStackTrace();
...