Here you can find the source of setFieldValue(final Object object, final String fieldName, final Object value)
public static void setFieldValue(final Object object, final String fieldName, final Object value) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException
//package com.java2s; //License from project: Open Source License import java.lang.reflect.Field; import java.lang.reflect.Modifier; public class Main { public static void setFieldValue(final Object object, final String fieldName, final Object value) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { Field field = getDeclaredField(object.getClass(), fieldName); makeAccessible(field);// ww w. ja v a 2s . c om field.set(object, value); } protected static Field getDeclaredField(final Object object, final String fieldName) throws NoSuchFieldException, SecurityException { return getDeclaredField(object.getClass(), fieldName); } @SuppressWarnings({ "rawtypes", "unused" }) protected static Field getDeclaredField(final Class clazz, final String fieldName) throws NoSuchFieldException, SecurityException { for (Class superClazz = clazz; superClazz != Object.class; superClazz = superClazz.getSuperclass()) { return superClazz.getDeclaredField(fieldName); } return null; } public static void makeAccessible(final Field field) { if (!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) { field.setAccessible(true); } } }