Java Reflection Field Value Set setFieldValue(Object obj, String fieldName, Object value)

Here you can find the source of setFieldValue(Object obj, String fieldName, Object value)

Description

set Field Value

License

Apache License

Declaration

public static boolean setFieldValue(Object obj, String fieldName, Object value) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Field;

public class Main {
    public static boolean setFieldValue(Object obj, String fieldName, Object value) {
        boolean result = true;
        try {/* w  w  w .j  a v a 2  s  .  c  om*/
            Field field = obj.getClass().getDeclaredField(fieldName);
            boolean access = field.isAccessible();
            field.setAccessible(true);
            field.set(obj, value);
            field.setAccessible(access);
        } catch (ReflectiveOperationException e) {
            result = false;
        }

        return result;
    }
}

Related

  1. setFieldValue(Object obj, String field, Object value)
  2. setFieldValue(Object obj, String fieldName, Object value)
  3. setFieldValue(Object obj, String fieldname, Object value)
  4. setFieldValue(Object obj, String fieldName, Object value)
  5. setFieldValue(Object obj, String fieldName, Object value)
  6. setFieldValue(Object obj, String fieldName, String fieldValue)
  7. setFieldValue(Object obj, String name, Object value)
  8. setFieldValue(Object object, Field field, Object value)
  9. setFieldValue(Object object, String fieldName, Object fieldValue)