Java tutorial
//package com.java2s; //License from project: Apache License import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Map; public class Main { public static Object setFieldValue(Map<String, String> map, Class<?> cls) throws Exception { Field[] fields = cls.getDeclaredFields(); Object obj = cls.newInstance(); for (Field field : fields) { Class<?> clsType = field.getType(); String name = field.getName(); String strSet = "set" + name.substring(0, 1).toUpperCase() + name.substring(1, name.length()); Method methodSet = cls.getDeclaredMethod(strSet, clsType); if (map.containsKey(name)) { Object objValue = typeConversion(clsType, map.get(name)); methodSet.invoke(obj, objValue); } } return obj; } public static Object typeConversion(Class<?> cls, String str) { Object obj = null; String nameType = cls.getSimpleName(); if ("Integer".equals(nameType)) { obj = Integer.valueOf(str); } if ("String".equals(nameType)) { obj = str; } if ("Float".equals(nameType)) { obj = Float.valueOf(str); } if ("Double".equals(nameType)) { obj = Double.valueOf(str); } if ("Boolean".equals(nameType)) { obj = Boolean.valueOf(str); } if ("Long".equals(nameType)) { obj = Long.valueOf(str); } if ("Short".equals(nameType)) { obj = Short.valueOf(str); } if ("Character".equals(nameType)) { obj = str.charAt(1); } return obj; } }