Here you can find the source of setField(final Object obj, final String fieldName, final Object value)
public static void setField(final Object obj, final String fieldName, final Object value)
//package com.java2s; //License from project: Open Source License import java.lang.reflect.Field; public class Main { public static void setField(final Object obj, final String fieldName, final Object value) { Field field = null;// w w w.j a v a 2s . co m try { field = obj.getClass().getDeclaredField(fieldName); } catch (NoSuchFieldException ex) { throw new IllegalArgumentException( "Unexpected reflection exception - " + ex.getClass().getName() + ": " + ex.getMessage()); } field.setAccessible(true); try { field.set(obj, value); } catch (IllegalAccessException ex) { throw new IllegalStateException( "Unexpected reflection exception - " + ex.getClass().getName() + ": " + ex.getMessage()); } } }