Here you can find the source of setFieldValue(Object instanceContainingField, String fieldName, Object fieldValue)
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 |
public static void setFieldValue(Object instanceContainingField, String fieldName, Object fieldValue)
//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(); } } }