Here you can find the source of setField(Object o, String name, Object value)
public static void setField(Object o, String name, Object value)
//package com.java2s; //License from project: Apache License import java.lang.reflect.Field; public class Main { public static void setField(Object o, String name, Object value) { try {//from w w w . j av a 2 s .com field(o, name).set(o, value); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } private static Field field(Object o, String name) throws NoSuchFieldException { return field(name, o.getClass()); } private static Field field(String name, Class klass) throws NoSuchFieldException { if (klass == null) { return null; } Field[] fields = klass.getDeclaredFields(); for (Field field : fields) { if (field.getName().equals(name)) { field.setAccessible(true); return field; } } return field(name, klass.getSuperclass()); } }