Here you can find the source of setFieldValue(Object object, String name, Object value)
static void setFieldValue(Object object, String name, Object value)
//package com.java2s; //License from project: Open Source License import java.lang.reflect.Field; public class Main { static void setFieldValue(Object object, String name, Object value) { Field field = null;//from w ww. j av a 2s . co m boolean isAccessible = true; try { field = object.getClass().getDeclaredField(name); //getField() for public fields isAccessible = field.isAccessible(); field.setAccessible(true); field.set(object, value); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } finally { if (field != null && !isAccessible) { field.setAccessible(false); } } } }