Here you can find the source of setFieldValue(Object target, String fieldName, Object value)
public static boolean setFieldValue(Object target, String fieldName, Object value) throws IllegalAccessException
//package com.java2s; //License from project: Apache License import java.lang.reflect.Field; public class Main { public static boolean setFieldValue(Object target, String fieldName, Object value) throws IllegalAccessException { return setFieldValue(target, fieldName, value, false); }/* ww w . ja va 2 s . c o m*/ public static boolean setFieldValue(Object target, String fieldName, Object value, boolean traceable) throws IllegalAccessException { Field field = searchField(target.getClass(), fieldName, traceable); if (field != null) return setValue(field, target, value); return false; } private static Field searchField(Class c, String targetField, boolean traceable) { do { Field[] fields = c.getDeclaredFields(); for (Field f : fields) { if (f.getName().equals(targetField)) { return f; } } c = c.getSuperclass(); traceable = traceable && c != Object.class; } while (traceable); return null; } private static boolean setValue(Field field, Object target, Object value) throws IllegalAccessException { if (!field.isAccessible()) field.setAccessible(true); field.set(target, value); return true; } }