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

voidsetFieldAccessible(Field field)
set Field Accessible
if (!field.isAccessible()) {
    field.setAccessible(true);
voidsetFieldBySomeMethod(List setMethods, Object object, Object value)
set Field By Some Method
Object t = getFieldBySomeMethod(setMethods, object);
setMethods.get(setMethods.size() - 1).invoke(t, value);
voidsetFieldContent(final Object obj, final String name, final Object value)
This method sets the contents of an object variable.
final Field field = getField(obj, name);
try {
    field.set(obj, value);
} catch (IllegalArgumentException e) {
    throw new RuntimeException(e);
} catch (IllegalAccessException e) {
    throw new RuntimeException(e);
FieldsetFieldEditable(Class clazz, String fieldName)
Sets a field in a class to be modifiable regardless of finality or privacy
try {
    Field targetField = clazz.getDeclaredField(fieldName);
    Field modifierField = Field.class.getDeclaredField("modifiers");
    targetField.setAccessible(true);
    modifierField.setAccessible(true);
    modifierField.setInt(targetField, targetField.getModifiers() & ~Modifier.FINAL);
    return targetField;
} catch (Exception ex) {
...
voidsetFieldForAnnotation(Object target, Class annotation, Object value)
set Field For Annotation
boolean found = setFieldForAnnotation(target, annotation, value, target.getClass());
if (!found) {
    Class superClass = target.getClass().getSuperclass();
    while (!found) {
        found = setFieldForAnnotation(target, annotation, value, superClass);
        if (!found) {
            superClass = superClass.getSuperclass();
        if (superClass == Object.class || superClass == null) {
            break;
booleansetFieldHelper(Object o, String name, Object v)
set Field Helper
try {
    Class c = o.getClass();
    Field f = null;
    while (f == null) {
        if (c != null) {
            try {
                f = c.getDeclaredField(name);
            } catch (Throwable t) {
...
booleansetFieldIfExists(final Object instance, final String fieldName, final Object value)
set Field If Exists
try {
    final Field f = instance.getClass().getField(fieldName);
    if (value instanceof Boolean || f.getType().isInstance(value)) {
        f.set(instance, value);
        return true;
    } else {
        System.out.println(instance.getClass() + " '" + fieldName + "' field not assignable with "
                + value.getClass() + ", it's a: " + f.getType());
...
voidsetFieldObject(Class clazz, String field, Object object, Object newObj)
set Field Object
try {
    Field f = clazz.getDeclaredField(field);
    f.setAccessible(true);
    f.set(object, newObj);
} catch (Exception ex) {
voidsetFieldObject(Class target, Object targetObject, String fieldName, Object object)
set Field Object
try {
    Field field = target.getDeclaredField(fieldName);
    field.setAccessible(true);
    Field field1 = Field.class.getDeclaredField("modifiers");
    field1.setAccessible(true);
    field1.set(field, field.getModifiers() & ~Modifier.FINAL);
    field.set(targetObject, object);
} catch (Exception e) {
...
voidsetFieldObjectValue(Class targetClass, Object target, String fieldName, Object value)
Reflection utility: sets the field value of given object using target class.
try {
    Field field = targetClass.getDeclaredField(fieldName);
    field.setAccessible(true);
    field.set(target, value);
} catch (Throwable e) {
    throw new RuntimeException(
            "Unable to get field '" + fieldName + "' from class " + targetClass.getName(), e);