Here you can find the source of setField(Class clazz, Object src, String fieldName, Object value)
public static void setField(Class clazz, Object src, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException
//package com.java2s; //License from project: Open Source License import java.lang.reflect.Field; import java.lang.reflect.Modifier; public class Main { public static void setField(Object src, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException { setField(src.getClass(), src, fieldName, value); }/*from w w w .j a v a 2s . c om*/ public static void setField(Class clazz, Object src, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException { Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField .setInt(field, field.getModifiers() & ~Modifier.FINAL); field.set(src, value); } }