Map <String, Object> Tools turn Entity class object

static public <T> T map2Object (the Map < String, Object > Map, Class <T> clazz) {

the SimpleDateFormat the SimpleDateFormat SDF new new = ( "the MM-dd-YYYY HH: mm: SS");

IF (Map == null) {
return null;
}
T obj = null;
the try {
// newInstance used to create objects
obj = clazz.newInstance ();
// get all the fields in the class
field [] fields = obj.getClass () getDeclaredFields ().;
for (Field, Field: Fields) {
int field.getModifiers = MOD ();
// a modifier has judged
iF (Modifier.isStatic (MOD) || Modifier.isFinal (MOD)) {
Continue;
}
// if when using a private field modifications need to add
field.setAccessible (to true);
// get the name of the parameter type
Field.getType filedTypeName = String () getName ();.
// determines whether the time type, using equalsIgnoreCase string comparison, a case-insensitive
// to obj property assignment
if (filedTypeName.equalsIgnoreCase ( "java.util.date ")) {
String DateTimeStamp = (String) as map.get (field.getName ());
IF (datetimestamp.equalsIgnoreCase (" null ")) {
field.set (obj, null);
} the else {
field.set (obj , sdf.parse (DateTimeStamp));
}
} the else {
field.set (obj, as map.get (field.getName ()));
}
}
} the catch (Exception E) {
e.printStackTrace ();
}
return obj;
}

Guess you like

Origin www.cnblogs.com/zxy-come-on/p/12594693.html