@Test public void testDefaultToMap() { A a = new A(); a.setName("name"); a.setAge(25); Map<String, Object> params = MapUtil.toMap(a); assertEquals(params.get("name"), "name"); assertEquals(params.get("age"), 25); }
public static Map<String, Object> toMap(Object o, Map<String, String> map, String... ignoreProperties) { PropertyDescriptor[] ts = ReflectUtil.getPropertyDescriptors(o); List<String> ignoreList = (ignoreProperties != null && ignoreProperties.length > 0) ? Arrays.asList(ignoreProperties) : null; Map<String, Object> m = new HashMap<>(ts.length); for (PropertyDescriptor t : ts) { Method r = t.getReadMethod(); String name = t.getName(); if (r != null && (ignoreList == null || !ignoreList.contains(name))) { try { if (!Modifier.isPublic(r.getDeclaringClass().getModifiers())) { r.setAccessible(true); } String value; if (null != map && (value = map.get(name)) != null) { m.put(value, r.invoke(o)); } else { m.put(name, r.invoke(o)); } } catch (Throwable e) { throw new RuntimeException("Could not copy property '" + name + "' from source to target", e); } } } return m; }
MapUtil { public static Map<String, Object> toMap(Object o, Map<String, String> map, String... ignoreProperties) { PropertyDescriptor[] ts = ReflectUtil.getPropertyDescriptors(o); List<String> ignoreList = (ignoreProperties != null && ignoreProperties.length > 0) ? Arrays.asList(ignoreProperties) : null; Map<String, Object> m = new HashMap<>(ts.length); for (PropertyDescriptor t : ts) { Method r = t.getReadMethod(); String name = t.getName(); if (r != null && (ignoreList == null || !ignoreList.contains(name))) { try { if (!Modifier.isPublic(r.getDeclaringClass().getModifiers())) { r.setAccessible(true); } String value; if (null != map && (value = map.get(name)) != null) { m.put(value, r.invoke(o)); } else { m.put(name, r.invoke(o)); } } catch (Throwable e) { throw new RuntimeException("Could not copy property '" + name + "' from source to target", e); } } } return m; } }
MapUtil { public static Map<String, Object> toMap(Object o, Map<String, String> map, String... ignoreProperties) { PropertyDescriptor[] ts = ReflectUtil.getPropertyDescriptors(o); List<String> ignoreList = (ignoreProperties != null && ignoreProperties.length > 0) ? Arrays.asList(ignoreProperties) : null; Map<String, Object> m = new HashMap<>(ts.length); for (PropertyDescriptor t : ts) { Method r = t.getReadMethod(); String name = t.getName(); if (r != null && (ignoreList == null || !ignoreList.contains(name))) { try { if (!Modifier.isPublic(r.getDeclaringClass().getModifiers())) { r.setAccessible(true); } String value; if (null != map && (value = map.get(name)) != null) { m.put(value, r.invoke(o)); } else { m.put(name, r.invoke(o)); } } catch (Throwable e) { throw new RuntimeException("Could not copy property '" + name + "' from source to target", e); } } } return m; }  }
MapUtil { public static Map<String, Object> toMap(Object o, Map<String, String> map, String... ignoreProperties) { PropertyDescriptor[] ts = ReflectUtil.getPropertyDescriptors(o); List<String> ignoreList = (ignoreProperties != null && ignoreProperties.length > 0) ? Arrays.asList(ignoreProperties) : null; Map<String, Object> m = new HashMap<>(ts.length); for (PropertyDescriptor t : ts) { Method r = t.getReadMethod(); String name = t.getName(); if (r != null && (ignoreList == null || !ignoreList.contains(name))) { try { if (!Modifier.isPublic(r.getDeclaringClass().getModifiers())) { r.setAccessible(true); } String value; if (null != map && (value = map.get(name)) != null) { m.put(value, r.invoke(o)); } else { m.put(name, r.invoke(o)); } } catch (Throwable e) { throw new RuntimeException("Could not copy property '" + name + "' from source to target", e); } } } return m; }  static MapPlain<K, V> build(); static MapPlain<K, V> build(int cap); static Map<String, Object> toMap(Object o, Map<String, String> map, String... ignoreProperties); static Map<String, Object> toMap(Object o, String... ignoreProperties); static void toObject(Map<String, Object> source, Object target, String... ignoreProperties); static List<Map<String, Object>> mapConvertToList(Map<K, V> map); static List<Map<String, Object>> mapConvertToList(Map<K, V> map, Object defaultValue); }
MapUtil { public static Map<String, Object> toMap(Object o, Map<String, String> map, String... ignoreProperties) { PropertyDescriptor[] ts = ReflectUtil.getPropertyDescriptors(o); List<String> ignoreList = (ignoreProperties != null && ignoreProperties.length > 0) ? Arrays.asList(ignoreProperties) : null; Map<String, Object> m = new HashMap<>(ts.length); for (PropertyDescriptor t : ts) { Method r = t.getReadMethod(); String name = t.getName(); if (r != null && (ignoreList == null || !ignoreList.contains(name))) { try { if (!Modifier.isPublic(r.getDeclaringClass().getModifiers())) { r.setAccessible(true); } String value; if (null != map && (value = map.get(name)) != null) { m.put(value, r.invoke(o)); } else { m.put(name, r.invoke(o)); } } catch (Throwable e) { throw new RuntimeException("Could not copy property '" + name + "' from source to target", e); } } } return m; }  static MapPlain<K, V> build(); static MapPlain<K, V> build(int cap); static Map<String, Object> toMap(Object o, Map<String, String> map, String... ignoreProperties); static Map<String, Object> toMap(Object o, String... ignoreProperties); static void toObject(Map<String, Object> source, Object target, String... ignoreProperties); static List<Map<String, Object>> mapConvertToList(Map<K, V> map); static List<Map<String, Object>> mapConvertToList(Map<K, V> map, Object defaultValue);  }