Here you can find the source of setField(Class> ownerClass, Object owner, String fieldName, Object newValue)
static void setField(Class<?> ownerClass, Object owner, String fieldName, Object newValue)
//package com.java2s; /*// w ww .ja v a 2 s.com * Copyright (c) 2006-2011 Rog?rio Liesenfeld * This file is subject to the terms of the MIT license (see LICENSE.txt). */ import java.lang.reflect.*; public class Main { static void setField(Object owner, String fieldName, Object newValue) { setField(owner.getClass(), owner, fieldName, newValue); } static void setField(Class<?> ownerClass, Object owner, String fieldName, Object newValue) { Field f; try { f = ownerClass.getDeclaredField(fieldName); } catch (NoSuchFieldException e) { throw new RuntimeException(e); } f.setAccessible(true); try { //noinspection unchecked f.set(owner, newValue); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } }