public static Object getObjectFromMap(Map<String,Object> map,String clazzpath) {
map = formatMap(map);
try {
Class c = Class.forName(clazzpath);
Constructor constructor = c.getConstructor();
Object object = constructor.newInstance();
Field[] declaredFields = c.getDeclaredFields();
for (Field declaredField : declaredFields) {
String name = declaredField.getName();
String nameFormat = name.replaceAll("_","").toUpperCase();
if (map.containsKey(nameFormat)) {
declaredField.setAccessible(true);
Object o = map.get(nameFormat);
boolean numeric = isNumeric(o);
if (numeric) {
declaredField.set(object, String.valueOf(o));
} else {
declaredField.set(object, o);
}
}
}
return object;
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
public static boolean isNumeric (Object obj){
if(obj instanceof BigDecimal) {
return true;
}
String str = String.valueOf(obj);
for (int i = 0;i < str.length();i++) {
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
|