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(final Object bean, final Field field, final Object value)
set Field Value
if (!field.isAccessible()) {
    field.setAccessible(true);
try {
    field.set(bean, value);
} catch (final IllegalAccessException e) {
    throw new IOException("Cannot set " + field.getName() + ": " + e.getMessage(), e);
} catch (final IllegalArgumentException e) {
...
voidsetFieldValue(final Object obj, final String fieldName, final Object value)
set Field Value
getSetField(obj, fieldName, false, value);
voidsetFieldValue(final Object object, final String field, final Object value)
Applies specified value to object field.
final Field actualField = getField(object.getClass(), field);
actualField.setAccessible(true);
actualField.set(object, value);
voidsetFieldValue(final Object object, final String fieldName, final Object value)
set Field Value
Field field = getDeclaredField(object.getClass(), fieldName);
makeAccessible(field);
field.set(object, value);
voidsetFieldValue(final Object object, final String fieldName, final Object value)
set Field Value
@SuppressWarnings("rawtypes")
Class clazz = object.getClass();
try {
    getAccessibleField(clazz, fieldName).set(object, value);
} catch (Exception e) {
    throw new RuntimeException(e);
voidsetFieldValue(final Object object, final String fieldName, final Object value)
set Field Value
Field field = getDeclaredField(object, fieldName);
if (field == null)
    throw new IllegalArgumentException(
            "Could not find field [" + fieldName + "] on target [" + object + "]");
makeAccessible(field);
try {
    field.set(object, value);
} catch (IllegalAccessException e) {
...
ObjectsetFieldValue(Map map, Class cls)
set Field Value
Field[] fields = cls.getDeclaredFields();
Object obj = cls.newInstance();
for (Field field : fields) {
    Class<?> clsType = field.getType();
    String name = field.getName();
    String strSet = "set" + name.substring(0, 1).toUpperCase() + name.substring(1, name.length());
    Method methodSet = cls.getDeclaredMethod(strSet, clsType);
    if (map.containsKey(name)) {
...
voidsetFieldValue(Object bean, Field field, Object value)
This method is used to set the value of a field of a model object to the specified value.
Class<?> targetType = field.getType();
Class<?> sourceType = value.getClass();
if (targetType.isAssignableFrom(sourceType)) {
    field.setAccessible(true);
    field.set(bean, value);
} else {
    throw new IllegalArgumentException(String.format(
            "Attempt to set property %s of type %s from a " + "value of type %s, types are not assignable.",
...
voidsetFieldValue(Object bean, Field field, Object value)
set Field Value
Method method = null;
method = getSetMethod(bean.getClass(), field);
if (method == null) {
    throw new NoSuchMethodException("can not find the set method");
} else {
    method.invoke(bean, new Object[] { value });
voidsetFieldValue(Object bean, String field, Object value)
Used to set value of specified field irrespective of the field modifier
try {
    Field fieldObj = bean.getClass().getDeclaredField(field);
    fieldObj.setAccessible(true);
    fieldObj.set(bean, value);
} catch (Exception ex) {
    throw new IllegalStateException("An error occurred while seting field value - " + field, ex);