Java Reflection Field Value Set setFieldValue(Object bean, Field field, Object value)

Here you can find the source of setFieldValue(Object bean, Field field, Object value)

Description

This method is used to set the value of a field of a model object to the specified value.

License

Open Source License

Parameter

Parameter Description
bean The bean object to be manipulated
field The field definition in the model object to be set
value The value to be stored in that field.

Exception

Parameter Description
IllegalArgumentException if the method is an instance method and the specified object argument is not an instance of the classor interface declaring the underlying method (or of a subclass or implementor thereof); if the numberof actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; orif, after possible unwrapping, a parameter value cannot be converted to the corresponding formalparameter type by a method invocation conversion.
IllegalAccessException if this Method object is enforcing Java language access control and the underlying method isinaccessible.

Declaration

public static void setFieldValue(Object bean, Field field, Object value)
        throws IllegalArgumentException, IllegalAccessException 

Method Source Code


//package com.java2s;

import java.lang.reflect.Field;

public class Main {
    /**//from  w w w  . j  a  v  a  2  s . c  o  m
     * This method is used to set the value of a field of a model object to the specified value.
     * 
     * @param bean
     *            The bean object to be manipulated
     * @param field
     *            The field definition in the model object to be set
     * @param value
     *            The value to be stored in that field.
     * @throws IllegalArgumentException
     *             if the method is an instance method and the specified object argument is not an instance of the class
     *             or interface declaring the underlying method (or of a subclass or implementor thereof); if the number
     *             of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or
     *             if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal
     *             parameter type by a method invocation conversion.
     * @throws IllegalAccessException
     *             if this Method object is enforcing Java language access control and the underlying method is
     *             inaccessible.
     */
    public static void setFieldValue(Object bean, Field field, Object value)
            throws IllegalArgumentException, IllegalAccessException {
        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.",
                    field.getName(), targetType.getName(), sourceType.getName()));
        }
    }
}

Related

  1. setFieldValue(final Object object, final String field, final Object value)
  2. setFieldValue(final Object object, final String fieldName, final Object value)
  3. setFieldValue(final Object object, final String fieldName, final Object value)
  4. setFieldValue(final Object object, final String fieldName, final Object value)
  5. setFieldValue(Map map, Class cls)
  6. setFieldValue(Object bean, Field field, Object value)
  7. setFieldValue(Object bean, String field, Object value)
  8. setFieldValue(Object host, Field f, Object value)
  9. setFieldValue(Object input, Object value, String fieldName)