Java tutorial
//package com.java2s; //License from project: Apache License import java.lang.reflect.Field; import java.lang.reflect.Modifier; public class Main { public static void setField(Field field, Object target, Object value) { if (!Modifier.isPublic(field.getModifiers())) { field.setAccessible(true); } try { field.set(target, value); } catch (IllegalAccessException iae) { throw new IllegalArgumentException("Could not set field " + field, iae); } } }