Java Reflection Field Value Set setFieldValue(Object instanceContainingField, String fieldName, Object fieldValue)

Here you can find the source of setFieldValue(Object instanceContainingField, String fieldName, Object fieldValue)

Description

Sets a value to a field using reflection even if the field is private.

License

Open Source License

Parameter

Parameter Description
instanceContainingField the object containing the field
fieldName the name of the field in the object
fieldValue the value to set for the provided field

Declaration

public static void setFieldValue(Object instanceContainingField, String fieldName, Object fieldValue) 

Method Source Code

//package com.java2s;
/*/*from www .  j  a v a2s  .c  om*/
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import java.lang.reflect.Field;

public class Main {
    /**
     * Sets a value to a field using reflection even if the field is private.
     *
     * @param instanceContainingField the object containing the field
     * @param fieldName the name of the field in the object
     * @param fieldValue the value to set for the provided field
     */
    public static void setFieldValue(Object instanceContainingField, String fieldName, Object fieldValue) {
        // Find the class containing the field to set
        Class<?> targetClass = instanceContainingField.getClass();
        while (targetClass != null) {
            for (Field field : targetClass.getDeclaredFields()) {
                if (field.getName().equalsIgnoreCase(fieldName)) {
                    try {
                        boolean isAccessible = field.isAccessible();
                        try {
                            field.setAccessible(true);
                            field.set(instanceContainingField, fieldValue);
                        } finally {
                            field.setAccessible(isAccessible);
                        }
                    } catch (Exception e) {
                        // This shouldn't happen but if it does then the Component manager will not function properly
                        // and we need to abort. It probably means the Java security manager has been configured to
                        // prevent accessing private fields.
                        throw new RuntimeException("Failed to set field [" + fieldName + "] in instance of ["
                                + instanceContainingField.getClass().getName() + "]. The Java Security Manager has "
                                + "probably been configured to prevent settting private field values. XWiki requires "
                                + "this ability to work.", e);
                    }
                    return;
                }
            }
            targetClass = targetClass.getSuperclass();
        }
    }
}

Related

  1. setFieldValue(Object bean, Field field, Object value)
  2. setFieldValue(Object bean, String field, Object value)
  3. setFieldValue(Object host, Field f, Object value)
  4. setFieldValue(Object input, Object value, String fieldName)
  5. setFieldValue(Object instance, Field field, Object value)
  6. setFieldValue(Object o, Class baseClass, Class fieldType, T value)
  7. SetFieldValue(Object o, String field, Object object)
  8. setFieldValue(Object obj, Field f, Object value)
  9. setFieldValue(Object obj, Field field, Object value)