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)
//package com.java2s; //License from project: Apache 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) { Field field = getDeclaredField(object, fieldName); if (field == null) throw new IllegalArgumentException( "Could not find field [" + fieldName + "] on target [" + object + "]"); makeAccessible(field);/* ww w . j ava 2s . c om*/ try { field.set(object, value); } catch (IllegalAccessException e) { } } protected static Field getDeclaredField(final Object object, final String fieldName) { for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass .getSuperclass()) { try { return superClass.getDeclaredField(fieldName); } catch (NoSuchFieldException e) { } } return null; } protected static void makeAccessible(final Field field) { if (!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) { field.setAccessible(true); } } }