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, Class fieldOwner, String fieldName, Object value)
set Field
final Field field;
try {
    field = fieldOwner.getDeclaredField(fieldName);
    field.setAccessible(true);
    field.set(obj, value);
} catch (NoSuchFieldException e) {
    throw new IllegalStateException("no field '" + fieldName + "' declared in " + fieldOwner + "", e);
} catch (IllegalAccessException e) {
...
voidsetField(Object obj, Field field, Object value)
set Field
field.setAccessible(true);
try {
    field.set(obj, value);
} catch (IllegalAccessException e) {
    logger.error("set field failure", e);
    throw new RuntimeException(e);
voidsetField(Object obj, Object value, String fieldName)
Sets the value of a field
try {
    getField(obj.getClass(), fieldName).set(obj, value);
} catch (IllegalArgumentException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
voidsetField(Object obj, Object value, String fieldName)
set Field
try {
    getField(obj.getClass(), fieldName).set(obj, value);
} catch (IllegalArgumentException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
voidsetField(Object obj, String fieldName, Object fieldValue)
Utility to set field in object if there is no public setter for it.
final Field field = obj.getClass().getDeclaredField(fieldName);
final boolean isAccessible = field.isAccessible();
field.setAccessible(true);
field.set(obj, fieldValue);
field.setAccessible(isAccessible);
voidsetField(Object obj, String fieldName, Object val)
set Field
getFieldDef(obj.getClass(), fieldName).set(obj, val);
voidsetField(Object obj, String fieldname, Object value)
set Field
try {
    Field field = obj.getClass().getField(fieldname);
    field.setAccessible(true);
    field.set(obj, value);
} catch (Exception e) {
voidsetField(Object obj, String fieldName, Object value)
set Field
Class<?> clazz = obj.getClass();
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
try {
    field.set(obj, value);
} catch (IllegalAccessException e) {
    e.printStackTrace();
voidsetField(Object obj, String fieldName, Object value)
set Field
try {
    Class c = obj.getClass();
    Field f = c.getField(fieldName);
    f.set(obj, value);
} catch (Exception e) {
;
voidsetField(Object obj, String fieldName, Object value, Class valueClass)
set Field
if (obj == null || fieldName == null || fieldName.length() < 1) {
    return;
String methodName = "set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
Method m = obj.getClass().getMethod(methodName, new Class[] { valueClass });
m.invoke(obj, new Object[] { value });