From 20c41088d227756062d4c5afb76cd6f6f3b3f600 Mon Sep 17 00:00:00 2001 From: thinkgem Date: Mon, 3 May 2021 11:55:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=8D=E5=B0=84=E6=96=B9=E6=B3=95=E8=B5=8B?= =?UTF-8?q?=E5=80=BC=EF=BC=8C=E6=94=AF=E6=8C=81=E7=BA=A7=E8=81=94=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E4=B8=BA=E7=A9=BA=E7=9A=84=E6=96=B9=E6=B3=95=E8=B5=8B?= =?UTF-8?q?=E5=80=BC=EF=BC=8C=E6=94=B9=E8=BF=9BExcel=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E6=97=B6=EF=BC=8C=E4=B8=BA=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E5=80=99=EF=BC=8C=E4=B8=8D=E7=94=A8=E5=86=8D?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=20=E5=88=A4=E6=96=AD=E4=B8=BA=E7=A9=BA?= =?UTF-8?q?=E5=B9=B6new=E5=AF=B9=E8=B1=A1=E4=BA=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jeesite/common/reflect/ReflectUtils.java | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 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 03b1c968..9eec6b15 100644 --- a/common/src/main/java/com/jeesite/common/reflect/ReflectUtils.java +++ b/common/src/main/java/com/jeesite/common/reflect/ReflectUtils.java @@ -39,6 +39,8 @@ public class ReflectUtils { private static final String CGLIB_CLASS_SEPARATOR = "$$"; private static Logger logger = LoggerFactory.getLogger(ReflectUtils.class); + + private static Class baseEntityClass = null; /** * 调用Getter方法, @@ -75,8 +77,28 @@ public class ReflectUtils { if (obj instanceof Map){ object = ((Map)obj).get(names[i]); }else{ - String methodName = GETTER_PREFIX + StringUtils.capitalize(names[i]); - object = invokeMethod(object, methodName, new Class[] {}, new Object[] {}); + String methodName = GETTER_PREFIX + StringUtils.capitalize(names[i]); + Object childObj = invokeMethod(object, methodName, new Class[] {}, new Object[] {}); + // 如果 get 获取对象为空,并且返回值类型继承自 BaseEntity,则 new 对象,并通过 set 赋予它 + if (childObj == null && object != null){ + Method method = getAccessibleMethod(object, methodName, new Class[] {}); + if (method != null) { + Class returnType = method.getReturnType(); + try { + if (baseEntityClass == null) { + baseEntityClass = Class.forName("com.jeesite.common.entity.BaseEntity"); + } + if (baseEntityClass.isAssignableFrom(returnType)) { + childObj = returnType.getDeclaredConstructor().newInstance(); + methodName = SETTER_PREFIX + StringUtils.capitalize(names[i]); + invokeMethodByName(object, methodName, new Object[] { childObj }); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } + object = childObj; } }else{ if (obj instanceof Map){