Here you can find the source of setFieldValue(Object bean, Field field, Object value)
public static void setFieldValue(Object bean, Field field, Object value) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException
//package com.java2s; //License from project: Apache License import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main { public static void setFieldValue(Object bean, Field field, Object value) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Method method = null;// w w w .j a v a2s . c o m method = getSetMethod(bean.getClass(), field); if (method == null) { throw new NoSuchMethodException("can not find the set method"); } else { method.invoke(bean, new Object[] { value }); } } public static Method getSetMethod(Class<?> bean, Field field) { String isMethodName = "set" + toUpperCaseFirstChar(field.getName()); try { return bean.getMethod(isMethodName, new Class[] { field.getType() }); } catch (NoSuchMethodException var4) { System.out.println(isMethodName + " not found"); return null; } } public static String toUpperCaseFirstChar(String string) { return string == null ? null : string.toUpperCase().charAt(0) + string.substring(1); } }