Here you can find the source of setFieldValue(Object input, Object value, String fieldName)
public static Object setFieldValue(Object input, Object value, String fieldName) throws Exception
//package com.java2s; /*/*from w w w. j ava2s . c o m*/ * Copyright: (c) Mayo Foundation for Medical Education and * Research (MFMER). All rights reserved. MAYO, MAYO CLINIC, and the * triple-shield Mayo logo are trademarks and service marks of MFMER. * * Distributed under the OSI-approved BSD 3-Clause License. * See http://ncip.github.com/lexevs-remote/LICENSE.txt for details. */ import java.lang.reflect.Field; public class Main { public static Object setFieldValue(Object input, Object value, String fieldName) throws Exception { Class searchClass = input.getClass(); while (searchClass != null) { Field[] fields = searchClass.getDeclaredFields(); for (Field field : fields) { if (field.getName().equals(fieldName)) { field.setAccessible(true); //Check to see if we're trying to set a int, long, etc with a String if (field.getType().getName().equals("java.lang.Long")) { if (value instanceof String) { field.set(input, Long.getLong((String) value)); } else { field.set(input, value); } } else if (field.getType().getName().equals("java.lang.Integer")) { if (value instanceof String) { field.set(input, Integer.getInteger((String) value)); } else { field.set(input, value); } } else { field.set(input, value); } } } searchClass = searchClass.getSuperclass(); } return input; } }