From cb0252a492ac2383cfce9aa2a3a757c5b590cee3 Mon Sep 17 00:00:00 2001 From: Romesum Date: Fri, 8 May 2020 14:36:07 +0800 Subject: [PATCH] =?UTF-8?q?[feat][M]:=E6=96=B0=E5=A2=9EMap=E8=BD=ACObject?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bookingsystem/converter/Map2Object.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 java/src/main/java/com/hzu/bookingsystem/converter/Map2Object.java diff --git a/java/src/main/java/com/hzu/bookingsystem/converter/Map2Object.java b/java/src/main/java/com/hzu/bookingsystem/converter/Map2Object.java new file mode 100644 index 0000000..1b64bf5 --- /dev/null +++ b/java/src/main/java/com/hzu/bookingsystem/converter/Map2Object.java @@ -0,0 +1,40 @@ +package com.hzu.bookingsystem.converter; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.Map; +import java.util.Objects; + +public class Map2Object { + /** + * @param map + * @param clazz + * @return + */ + public static Object map2Object(Map map, Class clazz) { + if (map == null) { + return null; + } + Object ob = null; + try { + ob = clazz.newInstance(); + Field[] fis = ob.getClass().getDeclaredFields(); + for (Field fi : fis) { + int mod = fi.getModifiers(); + if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) { + continue; + } + fi.setAccessible(true); + if (Objects.equals("entranceId", fi.getName())) { + String str = String.valueOf(map.get(fi.getName())); + fi.set(ob, Long.parseLong(str)); + } else { + fi.set(ob, map.get(fi.getName())); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + return ob; + } +} \ No newline at end of file