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(Object obj, String name, Object value)
set Field
try {
    getFieldAndEnsureItsAccessible(obj, name).set(obj, value);
} catch (IllegalAccessException e) {
    throw new RuntimeException(e);
booleansetField(Object obj, String name, Object value)
Set the value of this parameter.
String setterName = "set" + Character.toUpperCase(name.charAt(0)) + name.substring(1);
Method[] methods = obj.getClass().getMethods();
for (int i = 0; i < methods.length; i++) {
    if (methods[i].getName().equals(setterName)) {
        try {
            methods[i].invoke(obj, new Object[] { value });
            return true; 
        } catch (Exception e) {
...
voidsetField(Object object, Class clazz, String fieldName, Object value)
set Field
try {
    Field field = clazz.getDeclaredField(fieldName);
    field.setAccessible(true);
    field.set(object, value);
} catch (Exception e) {
    throw new RuntimeException(e);
booleansetField(Object object, Class clazz, String fieldName, Object value)
Sets the value of a field in an object
if (!clazz.isAssignableFrom(object.getClass()))
    return false;
Field field = Arrays.stream(clazz.getDeclaredFields()).filter(f -> f.getName().equals(fieldName))
        .findFirst().orElse(null);
return setField(object, field, value);
voidsetField(Object object, Field field, Object value)
Sets the value of a field for a given object, regardless if the field is accessible.
boolean accessible = field.isAccessible();
if (!accessible) {
    field.setAccessible(true);
try {
    field.set(object, value);
} catch (IllegalAccessException shouldNeverHappen) {
    shouldNeverHappen.printStackTrace();
...
voidsetField(Object object, Field field, Object value)
Sets the given value for the field inside the given object Throws all exceptions that can be thrown by Field.set Changes the accessibility of the field if it is false
boolean changedAccessible = false;
boolean isAccessible = field.isAccessible();
if (!isAccessible) {
    changedAccessible = true;
    field.setAccessible(true);
field.set(object, value);
if (changedAccessible) {
...
voidsetField(Object object, Object value, String name)
Set the value of the field 'name' to be 'value' on object 'object'.
ReflectiveOperationException firstError = null;
Class<? extends Object> clazz = object.getClass();
while (clazz != null) {
    try {
        Field field = clazz.getDeclaredField(name);
        field.setAccessible(true);
        field.set(object, value);
        clazz = null;
...
voidsetField(Object object, String field, Object value)
set Field
if (object == null || field == null)
    return;
try {
    Field target = object.getClass().getDeclaredField(field);
    target.setAccessible(true);
    target.set(object, value);
} catch (Exception ignored) {
voidsetField(Object object, String fieldName, int value)
Sets a field value for the given object.
setField(object, fieldName, Integer.valueOf(value));
voidsetField(Object object, String fieldName, Object newValue)
set Field
try {
    Field f = object.getClass().getDeclaredField(fieldName);
    if (f != null) {
        f.setAccessible(true);
        f.set(object, newValue);
} catch (NoSuchFieldException e) {
} catch (IllegalAccessException e) {
...