Here you can find the source of setField(Object object, String name, Object value)
public static void setField(Object object, String name, Object value) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException
//package com.java2s; //License from project: Apache License import java.lang.reflect.Field; public class Main { public static void setField(Object object, String name, Object value) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Class<?> clazz = object.getClass(); Field field = clazz.getDeclaredField(name); boolean accessable = field.isAccessible(); if (!accessable) field.setAccessible(true);//from w w w. j a v a 2s. c o m field.set(object, value); if (!accessable) field.setAccessible(false); } }