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(Field field, U instance, Object value)
set Field
boolean isAccessible = field.isAccessible();
if (!isAccessible) {
    field.setAccessible(true);
try {
    field.set(instance, value);
} finally {
    field.setAccessible(isAccessible);
...
voidsetField(final Field field, final Object instance, final Object value)
set Field
if (field == null) {
    throw new RuntimeException("No such field");
field.setAccessible(true);
try {
    field.set(instance, value);
} catch (final Exception ex) {
    throw new RuntimeException(ex);
...
voidsetField(final Field field, final Object object, final Object value)
Sets the supplied value of the supplied field on the supplied object.
try {
    field.set(object, value);
} catch (IllegalAccessException e) {
    throw new IllegalArgumentException(e);
VsetField(final O object, final String fieldName, final V value)
Sets a field on an object, even if the field would not normally be accessible (such as a private field).
Field objectField = null;
Class<?> workingClass = object.getClass();
while (objectField == null) {
    try {
        objectField = workingClass.getDeclaredField(fieldName);
    } catch (final NoSuchFieldException e) {
        if (workingClass == Object.class) {
            throw e;
...
voidsetField(final Object instance, final String fieldName, final T value)
set Field
Class<?> instanceClass = instance.getClass();
Field field = getDeclaredField(instanceClass, fieldName);
while (field == null) {
    instanceClass = instanceClass.getSuperclass();
    field = getDeclaredField(instanceClass, fieldName);
field.setAccessible(true);
try {
...
voidsetField(final Object obj, final String fieldName, final int value)
set Field
try {
    final Field field = obj.getClass().getField(fieldName);
    field.set(obj, new Integer(value));
} catch (final Throwable t) {
voidsetField(final Object obj, final String fieldName, final Object value)
set Field
Field field = null;
try {
    field = obj.getClass().getDeclaredField(fieldName);
} catch (NoSuchFieldException ex) {
    throw new IllegalArgumentException(
            "Unexpected reflection exception - " + ex.getClass().getName() + ": " + ex.getMessage());
field.setAccessible(true);
...
voidsetField(final Object obj, final String name, final Object value)
Set a (final) field during deserialization.
try {
    final Field field = obj.getClass().getDeclaredField(name);
    if (field == null) {
        throw new IOException("Field " + name + " not found in class " + obj.getClass());
    field.setAccessible(true);
    field.set(obj, value);
} catch (final SecurityException e) {
...
voidsetField(final Object obj, final String variableName, final Object variableValue)
Sets variable by reflection down to 3 levels of abstracts classes.
Field declaredField = null;
Class<?> clazz = obj.getClass();
for (int i = 0; i <= 3; i++) {
    try {
        declaredField = clazz.getDeclaredField(variableName);
        break;
    } catch (NoSuchFieldException e) {
        clazz = clazz.getSuperclass();
...
voidsetField(final Object object, final String name, final Object value)
set Field
try {
    final Field field = findField(object.getClass(), name);
    field.setAccessible(true);
    field.set(object, value);
} catch (ReflectiveOperationException e) {
    throw new IllegalStateException(e);