Java tutorial
//package com.java2s; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Map; public class Main { public static <T> T converMap2Object(Map<String, String> map, Class<T> cls) { Field[] fields = cls.getDeclaredFields(); T t = null; try { t = (T) cls.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } for (Field f : fields) { f.setAccessible(true); invokeSet(t, f.getName(), map.get(f.getName())); } return t; } public static void invokeSet(Object o, String fieldName, Object value) { Method method = getSetMethod(o.getClass(), fieldName); try { method.invoke(o, new Object[] { value }); } catch (Exception e) { e.printStackTrace(); } } @SuppressWarnings("unchecked") public static Method getSetMethod(Class objectClass, String fieldName) { try { Class[] parameterTypes = new Class[1]; Field field = objectClass.getDeclaredField(fieldName); parameterTypes[0] = field.getType(); StringBuffer sb = new StringBuffer(); sb.append("set"); sb.append(fieldName.substring(0, 1).toUpperCase()); sb.append(fieldName.substring(1)); Method method = objectClass.getMethod(sb.toString(), parameterTypes); return method; } catch (Exception e) { e.printStackTrace(); } return null; } }