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(String fieldName, int fieldValue, Object board)
set Field Value
Field field;
try {
    field = board.getClass().getDeclaredField(fieldName);
    field.setAccessible(true);
    field.set(board, fieldValue);
} catch (NoSuchFieldException e) {
    e.printStackTrace();
} catch (SecurityException e) {
...
voidsetFieldValue(String inField, Object inObject, Object inValue)
set Field Value
final Class<?> theClass = (Class<?>) (inObject instanceof Class ? inObject : inObject.getClass());
final Field theField = theClass.getDeclaredField(inField);
setFieldValue(theField, Modifier.isStatic(theField.getModifiers()) ? null : inObject, inValue);
voidsetFieldValue(String name, Object instance, int value, Class cl)
set Field Value
try {
    getReflectField(name, instance, cl).setInt(instance, value);
} catch (Exception e) {
    e.printStackTrace();
    if (isThrowable) {
        throw new RuntimeException(
                "setFieldValue int error " + name + "   target   " + instance + "  value  " + value);
voidsetFieldValue(String name, String obfuscatedName, Class clazz, Object object, Object value)
set Field Value
Field field = getField(name, obfuscatedName, clazz);
try {
    field.setAccessible(true);
    field.set(object, value);
} catch (Exception e) {
    throw new RuntimeException(e);
voidsetFieldvalue(T instance, E value, Field field)
set Fieldvalue
try {
    field.setAccessible(true);
    field.set(instance, value);
} catch (IllegalAccessException e) {
    throw new RuntimeException(
            String.format("Error while setting value (%s) into field (%s)", value, field.getName()), e);
voidsetFieldValue_internal(Object bean, Field field, Object value)
set Field Valuinternal
try {
    field.set(bean, value);
} catch (IllegalAccessException e) {
    field.setAccessible(true);
    try {
        field.set(bean, value);
    } catch (IllegalAccessException e1) {
        throw new IllegalAccessError(e1.getMessage());
...
voidsetFieldValueForObject(Object object, String fieldName, Object value)
set Field Value For Object
Field field = object.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(object, value);
voidsetFieldValues(Object bean, Map valMap)
Set all fields' value.
Class<?> cls = bean.getClass();
Map<String, Field> fieldMap = getFields(cls);
Iterator<Entry<String, Object>> iterator = valMap.entrySet().iterator();
while (iterator.hasNext()) {
    try {
        Field field = fieldMap.get(iterator.next().getKey());
        if (field == null) {
            continue;
...
voidsetFieldValueWithPath(Object object, String path, Object value)
Returns the value of a field identified using a path from the parent.
int lastDot = path.lastIndexOf('.');
if (lastDot > -1) {
    String parentPath = path.substring(0, lastDot);
    String field = path.substring(lastDot + 1);
    Object parentObject = getFieldValueWithPath(object, parentPath);
    if (parentObject == null) {
        throw new IllegalStateException(String.format("Null value for %s while accessing %s on object %s",
                parentPath, path, object));
...
ObjectsetFieldValueWithSetterMethod(Object target, Object value, Class clazz, Field field)
set Field Value With Setter Method
Method method = getSetterMethod(clazz, field);
try {
    return method.invoke(target, value);
} catch (IllegalAccessException e) {
    throw new RuntimeException(e);
} catch (InvocationTargetException e) {
    throw new RuntimeException(e);