Here you can find the source of setFieldValue(Field field, Object value, Object target)
public static void setFieldValue(Field field, Object value, Object target) throws IllegalArgumentException, IllegalAccessException
//package com.java2s; /******************************************************************************* * Copyright (c) 2010 Robert "Unlogic" Olofsson (unlogic@unlogic.se). * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser Public License v3 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/lgpl-3.0-standalone.html ******************************************************************************/ import java.lang.reflect.Field; public class Main { public static void setFieldValue(Field field, Object value, Object target) throws IllegalArgumentException, IllegalAccessException { boolean declaredAccessible = field.isAccessible(); if (!declaredAccessible) { field.setAccessible(true);//from ww w . j av a 2s .c o m } field.set(target, value); if (!declaredAccessible) { field.setAccessible(false); } } }