public static void setPrivateField ( final Object obj , final String name , final Object value ) { try { final Field field = obj . getClass ( ) . getDeclaredField ( name ) ; field . setAccessible ( true ) ; field . set ( obj , value ) ; } catch ( final Exception ex ) { throw new RuntimeException ( "Couldn't set field '" + name + "' in class '" + obj . getClass ( ) + "'" , ex ) ; } }
Sets a private field in an object by using reflection .