From 130f3abfb6049562875498f510dfaf681c400702 Mon Sep 17 00:00:00 2001 From: thinkgem Date: Mon, 27 May 2019 22:12:39 +0800 Subject: [PATCH] =?UTF-8?q?ReflectUtils=20Getter=20Setter=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20Map?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jeesite/common/reflect/ReflectUtils.java | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/common/src/main/java/com/jeesite/common/reflect/ReflectUtils.java b/common/src/main/java/com/jeesite/common/reflect/ReflectUtils.java index 064d4939..40666582 100644 --- a/common/src/main/java/com/jeesite/common/reflect/ReflectUtils.java +++ b/common/src/main/java/com/jeesite/common/reflect/ReflectUtils.java @@ -12,6 +12,7 @@ import java.lang.reflect.Modifier; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.Date; +import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; @@ -42,14 +43,19 @@ public class ReflectUtils { /** * 调用Getter方法, * 支持多级,如:对象名.对象名.方法, - * 支持静态类及方法调用 + * 支持静态类及方法调用, + * 支持Map */ @SuppressWarnings("unchecked") public static E invokeGetter(Object obj, String propertyName) { Object object = obj; for (String name : StringUtils.split(propertyName, ".")){ - String getterMethodName = GETTER_PREFIX + StringUtils.capitalize(name); - object = invokeMethod(object, getterMethodName, new Class[] {}, new Object[] {}); + if (obj instanceof Map){ + object = ((Map)obj).get(name); + }else{ + String methodName = GETTER_PREFIX + StringUtils.capitalize(name); + object = invokeMethod(object, methodName, new Class[] {}, new Object[] {}); + } } return (E)object; } @@ -57,18 +63,28 @@ public class ReflectUtils { /** * 调用Setter方法,仅匹配方法名, * 支持多级,如:对象名.对象名.方法, - * 支持静态类及方法调用 + * 支持静态类及方法调用, + * 支持Map */ + @SuppressWarnings("unchecked") public static void invokeSetter(Object obj, String propertyName, E value) { Object object = obj; String[] names = StringUtils.split(propertyName, "."); for (int i=0; i