Here you can find the source of setField(final Object object, final String name, final Object value)
public static void setField(final Object object, final String name, final Object value)
//package com.java2s; //License from project: Apache License import java.lang.reflect.Field; public class Main { public static void setField(final Object object, final String name, final Object value) { try {// w w w. j a v a 2 s. c o m final Field field = findField(object.getClass(), name); field.setAccessible(true); field.set(object, value); } catch (ReflectiveOperationException e) { throw new IllegalStateException(e); } } private static Field findField(final Class<?> clazz, final String name) throws ReflectiveOperationException { Class<?> theClazz = clazz; while (theClazz != null) { try { return theClazz.getDeclaredField(name); } catch (NoSuchFieldException e) { theClazz = theClazz.getSuperclass(); } } throw new NoSuchFieldException(name); } }