Java Reflection Field Set setField(Object target, Field field, Object value)

Here you can find the source of setField(Object target, Field field, Object value)

Description

Set target Object field to a certain value

License

LGPL

Parameter

Parameter Description
target Object whose field is being set
field Object field to set
value the new value for the given field

Declaration

public static void setField(Object target, Field field, Object value) 

Method Source Code

//package com.java2s;
/*/*ww w .  ja v a2s .  c om*/
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
 */

import java.lang.reflect.Field;

public class Main {
    /**
     * Set target Object field to a certain value
     *
     * @param target Object whose field is being set
     * @param field Object field to set
     * @param value the new value for the given field
     */
    public static void setField(Object target, Field field, Object value) {
        try {
            field.set(target, value);
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException("Field " + field + " could not be set", e);
        }
    }

    /**
     * Set target Object field to a certain value
     *
     * @param target Object whose field is being set
     * @param fieldName Object field naem to set
     * @param value the new value for the given field
     */
    public static void setField(Object target, String fieldName, Object value) {
        try {
            Field field = getField(target.getClass(), fieldName);
            field.set(target, value);
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException("Field " + fieldName + " could not be set", e);
        }
    }

    /**
     * Get a field from a given class
     *
     * @param clazz clazz
     * @param name field name
     *
     * @return field object
     */
    public static Field getField(Class clazz, String name) {
        try {
            Field field = clazz.getDeclaredField(name);
            field.setAccessible(true);
            return field;
        } catch (NoSuchFieldException e) {
            Class superClass = clazz.getSuperclass();
            if (!clazz.equals(superClass)) {
                return getField(superClass, name);
            }
            throw new IllegalArgumentException("Class " + clazz + " does not contain a " + name + " field", e);
        }
    }
}

Related

  1. setField(Object owner, String name, Object newValue, Class definedIn)
  2. setField(Object owner, String targetClass, String fieldName, Object value)
  3. setField(Object service, String name, Object obj)
  4. setField(Object source, String fieldName, Object value)
  5. setField(Object sourceObj, Object targetObj, Field valueField, List targetFields)
  6. setField(Object target, Field field, Object value)
  7. setField(Object target, String fieldName, Class fieldType, T value)
  8. setField(Object target, String fieldname, Object value)
  9. setField(Object target, String name, Object value)