From e0faad31cb5cadfc000277d3f2052a57cb441b2d Mon Sep 17 00:00:00 2001 From: Jonathan Cobb Date: Tue, 8 Dec 2020 09:06:01 -0500 Subject: [PATCH] allow copy to map with a prefix --- .../java/org/cobbzilla/util/reflect/ReflectionUtil.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/cobbzilla/util/reflect/ReflectionUtil.java b/src/main/java/org/cobbzilla/util/reflect/ReflectionUtil.java index 67649e7..6930e04 100644 --- a/src/main/java/org/cobbzilla/util/reflect/ReflectionUtil.java +++ b/src/main/java/org/cobbzilla/util/reflect/ReflectionUtil.java @@ -518,7 +518,7 @@ public class ReflectionUtil { * Same as copy(dest, src) but only named fields are copied * @param dest destination object, or a Map * @param src source object - * @param prefix prefix map keys with this String + * @param prefix prefix map keys with this String. This only works if `dest` is a Map * @param fields only fields with these names will be considered for copying * @param exclude fields with these names will NOT be considered for copying * @param objects must share a type @@ -586,7 +586,7 @@ public class ReflectionUtil { // copy the value from src to dest, if it's different if (!srcValue.equals(destValue)) { if (isMap) { - ((Map) dest).put(fieldName, srcValue); + ((Map) dest).put((empty(prefix)?"":prefix) + fieldName, srcValue); } else { setter.invoke(dest, srcValue); } @@ -727,7 +727,7 @@ public class ReflectionUtil { public static Map toMap(Object thing, String prefix, String[] fields, String[] exclude) { final Map map = new HashMap<>(); - copy(map, thing, fields, exclude); + copy(map, thing, prefix, fields, exclude); return map; }