Here you can find the source of setField(Class cls, Object obj, String fieldName, Object value)
public static void setField(Class cls, Object obj, String fieldName, Object value) throws Exception
//package com.java2s; /******************************************************************************* * Copyright ? 2012-2015 eBay Software Foundation * This program is dual licensed under the MIT and Apache 2.0 licenses. * Please see LICENSE for more information. *******************************************************************************/ import java.lang.reflect.Field; public class Main { public static void setField(Object obj, String fieldName, Object value) throws Exception { Class cls = obj.getClass(); Field field = cls.getDeclaredField(fieldName); field.setAccessible(true);/*from w w w . j a v a 2 s .c o m*/ field.set(obj, value); } public static void setField(Class cls, Object obj, String fieldName, Object value) throws Exception { Field field = cls.getDeclaredField(fieldName); field.setAccessible(true); field.set(obj, value); } }