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; public class Main { public static void setFieldValue(final Object object, final String fieldName, final Object value) { @SuppressWarnings("rawtypes") Class clazz = object.getClass(); try {/* w ww . j a v a2s . co m*/ getAccessibleField(clazz, fieldName).set(object, value); } catch (Exception e) { throw new RuntimeException(e); } } private static Field getAccessibleField(@SuppressWarnings("rawtypes") final Class clazz, final String fieldName) throws NoSuchFieldException { Class<?> tmpClass = clazz; do { for (final Field field : tmpClass.getDeclaredFields()) { String candidateName = field.getName(); if (!candidateName.equals(fieldName)) { continue; } field.setAccessible(true); return field; } tmpClass = tmpClass.getSuperclass(); } while (clazz != null); throw new RuntimeException("Field '" + fieldName + "' not found on class " + clazz); } }