Here you can find the source of setField(Object o, String fieldName, Object inject)
public static void setField(Object o, String fieldName, Object inject)
//package com.java2s; //License from project: Apache License import java.lang.reflect.Field; public class Main { public static void setField(Object o, String fieldName, Object inject) { Field field = getAccessibleField(fieldName, o.getClass()); try {/*ww w . jav a 2s .com*/ field.set(o, inject); } catch (IllegalAccessException e) { e.printStackTrace(); } } private static Field getAccessibleField(String fieldName, Class<?> c) { Field field = null; while (c != null) { try { field = c.getDeclaredField(fieldName); field.setAccessible(true); break; } catch (NoSuchFieldException e) { c = c.getSuperclass(); } } return field; } }