From 45fabc8ca077964109cf6275e84d77f2c80fdc5f Mon Sep 17 00:00:00 2001 From: thinkgem Date: Sun, 22 Jul 2018 12:16:31 +0800 Subject: [PATCH] =?UTF-8?q?=E9=99=8D=E4=BD=8EReflectUtils=E7=9A=84?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E7=BA=A7=E5=88=AB=EF=BC=8C=E5=A6=82=E6=9E=9C?= =?UTF-8?q?null=E4=B8=8D=E6=8A=9B=E9=94=99=EF=BC=8C=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=AD=A6=E5=91=8A=E5=8D=B3=E5=8F=AF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jeesite/common/reflect/ReflectUtils.java | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 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 abc8954a..62204337 100644 --- a/common/src/main/java/com/jeesite/common/reflect/ReflectUtils.java +++ b/common/src/main/java/com/jeesite/common/reflect/ReflectUtils.java @@ -76,7 +76,9 @@ public class ReflectUtils { public static Object getFieldValue(final Object obj, final String fieldName) { Field field = getAccessibleField(obj, fieldName); if (field == null) { - throw new IllegalArgumentException("在 [" + obj.getClass() + "] 中,没有找到 [" + fieldName + "] 字段 "); + //throw new IllegalArgumentException("在 [" + obj.getClass() + "] 中,没有找到 [" + fieldName + "] 字段 "); + logger.warn("在 [" + obj.getClass() + "] 中,没有找到 [" + fieldName + "] 字段 "); + return null; } Object result = null; try { @@ -93,7 +95,9 @@ public class ReflectUtils { public static void setFieldValue(final Object obj, final String fieldName, final Object value) { Field field = getAccessibleField(obj, fieldName); if (field == null) { - throw new IllegalArgumentException("在 [" + obj.getClass() + "] 中,没有找到 [" + fieldName + "] 字段 "); + //throw new IllegalArgumentException("在 [" + obj.getClass() + "] 中,没有找到 [" + fieldName + "] 字段 "); + logger.warn("在 [" + obj.getClass() + "] 中,没有找到 [" + fieldName + "] 字段 "); + return; } try { field.set(obj, value); @@ -114,7 +118,9 @@ public class ReflectUtils { } Method method = getAccessibleMethod(obj, methodName, parameterTypes); if (method == null) { - throw new IllegalArgumentException("在 [" + obj.getClass() + "] 中,没有找到 [" + methodName + "] 方法 "); + //throw new IllegalArgumentException("在 [" + obj.getClass() + "] 中,没有找到 [" + methodName + "] 方法 "); + logger.warn("在 [" + obj.getClass() + "] 中,没有找到 [" + methodName + "] 方法 "); + return null; } try { return method.invoke(obj, args); @@ -132,7 +138,10 @@ public class ReflectUtils { public static Object invokeMethodByName(final Object obj, final String methodName, final Object[] args) { Method method = getAccessibleMethodByName(obj, methodName, args.length); if (method == null) { - throw new IllegalArgumentException("在 [" + obj.getClass() + "] 中,没有找到 [" + methodName + "] 方法 "); + // 如果为空不报错,直接返回空。 +// throw new IllegalArgumentException("在 [" + obj.getClass() + "] 中,没有找到 [" + methodName + "] 方法 "); + logger.warn("在 [" + obj.getClass() + "] 中,没有找到 [" + methodName + "] 方法 "); + return null; } try { // 类型转换(将参数数据类型转换为目标方法参数类型) @@ -174,7 +183,11 @@ public class ReflectUtils { * 如向上转型到Object仍无法找到, 返回null. */ public static Field getAccessibleField(final Object obj, final String fieldName) { - Validate.notNull(obj, "object can't be null"); + // 为空不报错。直接返回 null + // Validate.notNull(obj, "object can't be null"); + if (obj == null){ + return null; + } Validate.notBlank(fieldName, "fieldName can't be blank"); for (Class superClass = obj.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) { try { @@ -197,7 +210,11 @@ public class ReflectUtils { */ public static Method getAccessibleMethod(final Object obj, final String methodName, final Class... parameterTypes) { - Validate.notNull(obj, "object can't be null"); + // 为空不报错。直接返回 null + // Validate.notNull(obj, "object can't be null"); + if (obj == null){ + return null; + } Validate.notBlank(methodName, "methodName can't be blank"); for (Class searchType = obj.getClass(); searchType != Object.class; searchType = searchType.getSuperclass()) { try { @@ -219,7 +236,11 @@ public class ReflectUtils { * 用于方法需要被多次调用的情况. 先使用本函数先取得Method,然后调用Method.invoke(Object obj, Object... args) */ public static Method getAccessibleMethodByName(final Object obj, final String methodName, int argsNum) { - Validate.notNull(obj, "object can't be null"); + // 为空不报错。直接返回 null + // Validate.notNull(obj, "object can't be null"); + if (obj == null){ + return null; + } Validate.notBlank(methodName, "methodName can't be blank"); for (Class searchType = obj.getClass(); searchType != Object.class; searchType = searchType.getSuperclass()) { Method[] methods = searchType.getDeclaredMethods();