Here you can find the source of setField(String name, Object target, Object value)
public static void setField(String name, Object target, Object value)
//package com.java2s; //License from project: Apache License import java.lang.reflect.Field; public class Main { public static void setField(String name, Object target, Object value) { try {/*from w w w. java 2 s . co m*/ Class<?> targetClass = target.getClass(); Field field = targetClass.getDeclaredField(name); boolean unlocked = false; if (!field.isAccessible()) { field.setAccessible(true); unlocked = true; } field.set(target, value); if (unlocked) { field.setAccessible(false); } } catch (Exception e) { throw new RuntimeException(e); } } }