Java Reflection Field Value Set setFieldValue(Object object, String name, Object value)

Here you can find the source of setFieldValue(Object object, String name, Object value)

Description

Utility method to set a field to a value.

License

Open Source License

Parameter

Parameter Description
object Instance in which the value should be set
name Name of the field who's value should be set
value The value to be set

Declaration

public static void setFieldValue(Object object, String name, Object value) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 EM-SOFTWARE and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * <p>//from   ww  w.  java2  s. c om
 * Contributors:
 * Christoph Keimel <c.keimel@emsw.de> - initial API and implementation
 *******************************************************************************/

import java.lang.reflect.Field;

public class Main {
    /**
     * Utility method to set a field to a value. If the field is not accessible, it will be set to be accessible.
     * @param object Instance in which the value should be set
     * @param name Name of the field who's value should be set
     * @param value The value to be set
     */
    public static void setFieldValue(Object object, String name, Object value) {
        try {
            Field field = getField(object.getClass(), name);
            if (!field.isAccessible()) {
                field.setAccessible(true);
            }
            field.set(object, value);
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
            throw new RuntimeException(
                    "Could not set field value: " + object.getClass().getSimpleName() + "." + name, e); //$NON-NLS-1$ //$NON-NLS-2$
        }

    }

    /**
     * Searches for a field in the given class and all of its super classes.
     * @param clazz Class to start the search for the field
     * @param name Name of the field
     * @return The field that was found
     * @throws NoSuchFieldException
     */
    public static Field getField(Class<?> clazz, String name) throws NoSuchFieldException {
        Class<?> searchClass = clazz;
        Field field = null;
        while (field == null && searchClass != null) {
            try {
                field = searchClass.getDeclaredField(name);
            } catch (NoSuchFieldException e) {
                searchClass = searchClass.getSuperclass();
            }
        }
        if (field == null) {
            throw new NoSuchFieldException(clazz.getSimpleName() + "." + name); //$NON-NLS-1$
        }
        return field;
    }
}

Related

  1. setFieldValue(Object object, String fieldName, Object fieldValue)
  2. setFieldValue(Object object, String fieldName, Object value)
  3. setFieldValue(Object object, String fieldName, Object value)
  4. setFieldValue(Object object, String fieldName, Object value)
  5. setFieldValue(Object object, String fieldName, Object value)
  6. setFieldValue(Object object, String name, Object value)
  7. setFieldValue(Object objectInstance, String fieldName, Object valueToSet)
  8. setFieldValue(Object pojo, Field field, Object value)
  9. setFieldValue(Object receiver, String fieldName, Object value)