Java tutorial
//package com.java2s; //License from project: Apache License import java.lang.reflect.Field; public class Main { public static void setFieldValue(Object object, Field field, Object value) { try { setAccessibleIfNeeded(field); field.set(object, value); } catch (final Exception e) { throw new RuntimeException(e); } } public static void setAccessibleIfNeeded(Field field) { if (!field.isAccessible()) { field.setAccessible(true); } } }