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(Class clazz, Object obj, String fieldName, Object value)
Set the value of a field to value.
try {
    Field field = clazz.getDeclaredField(fieldName);
    boolean oldAccess = field.isAccessible();
    field.setAccessible(true);
    field.set(obj, value);
    field.setAccessible(oldAccess);
} catch (Exception e) {
    e.printStackTrace();
...
voidsetField(Class ownerClass, Object owner, String fieldName, Object newValue)
set Field
Field f;
try {
    f = ownerClass.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
    throw new RuntimeException(e);
f.setAccessible(true);
try {
...
voidsetField(Class target, Class fieldType, int index, Object obj, Object value)
set Field
try {
    getField(target, fieldType, index).set(obj, value);
} catch (Exception ex) {
    ex.printStackTrace();
voidsetField(Class clazz, T instance, String fieldName, Object value)
set Field
try {
    Field field = clazz.getDeclaredField(fieldName);
    field.setAccessible(true);
    field.set(instance, value);
} catch (SecurityException e) {
    handleException(e);
} catch (NoSuchFieldException e) {
    handleException(e);
...
voidsetField(Class clazz, T instance, String fieldName, Object value)
Set the field of a given object to a new value with reflection.
try {
    Field field = getField(clazz, instance, fieldName);
    field.set(instance, value);
} catch (IllegalAccessException e) {
    throw new RuntimeException(format("Could not set value to field '%s' for instance '%s' of class '%s'",
            fieldName, instance, clazz.getName()), e);
voidsetField(Field f, Object instance, Object value)
set Field
try {
    boolean oldAccessible = f.isAccessible();
    boolean shouldSetAccessible = !Modifier.isPublic(f.getModifiers()) && !oldAccessible;
    if (shouldSetAccessible) {
        f.setAccessible(true);
    f.set(instance, value);
    if (shouldSetAccessible) {
...
voidsetField(Field f, Object this_, Object value)
Sets field with specified value in spite of access permission.
try {
    f.setAccessible(true);
    f.set(this_, value);
} catch (IllegalAccessException e) {
    throw new AssertionError(e);
voidsetField(Field field, @Nullable Object instance, Object thing)
set Field
Object fieldAccessor = newFieldAccessor.invoke(reflectionFactory, field, false);
fieldAccessorSet.invoke(fieldAccessor, instance, thing);
voidsetField(Field field, Object classWithField, Object value)
set Field
try {
    field.set(classWithField, value);
} catch (IllegalArgumentException | IllegalAccessException e) {
    e.printStackTrace();
booleansetField(Field field, Object instance, Object value)
Sets the specified field to the specified value in the instance.
if (field == null) {
    System.err.println("Null field");
    return false;
try {
    field.set(instance, value);
} catch (Exception e) {
    System.err.println(field.getType() + " not assignable from " + value.getClass());
...