|
|
@@ -149,7 +149,7 @@ public class ReflectUtil {
|
|
|
*/
|
|
|
public static Field getField(Class<?> beanClass, String name) throws SecurityException {
|
|
|
final Field[] fields = getFields(beanClass);
|
|
|
- return ArrayUtil.firstMatch((field)->name.equals(getFieldName(field)), fields);
|
|
|
+ return ArrayUtil.firstMatch((field) -> name.equals(getFieldName(field)), fields);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -191,7 +191,7 @@ public class ReflectUtil {
|
|
|
* 获得一个类中所有字段列表,直接反射获取,无缓存<br>
|
|
|
* 如果子类与父类中存在同名字段,则这两个字段同时存在,子类字段在前,父类字段在后。
|
|
|
*
|
|
|
- * @param beanClass 类
|
|
|
+ * @param beanClass 类
|
|
|
* @param withSuperClassFields 是否包括父类的字段列表
|
|
|
* @return 字段列表
|
|
|
* @throws SecurityException 安全检查异常
|
|
|
@@ -865,84 +865,84 @@ public class ReflectUtil {
|
|
|
return invoke(obj, method, args);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 执行方法
|
|
|
- *
|
|
|
- * <p>
|
|
|
- * 对于用户传入参数会做必要检查,包括:
|
|
|
- *
|
|
|
- * <pre>
|
|
|
- * 1、忽略多余的参数
|
|
|
- * 2、参数不够补齐默认值
|
|
|
- * 3、传入参数为null,但是目标参数类型为原始类型,做转换
|
|
|
- * </pre>
|
|
|
- *
|
|
|
- * @param <T> 返回对象类型
|
|
|
- * @param obj 对象,如果执行静态方法,此值为<code>null</code>
|
|
|
- * @param method 方法(对象方法或static方法都可)
|
|
|
- * @param args 参数对象
|
|
|
- * @return 结果
|
|
|
- * @throws UtilException 一些列异常的包装
|
|
|
- */
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- public static <T> T invoke(Object obj, Method method, Object... args) throws UtilException {
|
|
|
- setAccessible(method);
|
|
|
-
|
|
|
- // 检查用户传入参数:
|
|
|
- // 1、忽略多余的参数
|
|
|
- // 2、参数不够补齐默认值
|
|
|
- // 3、通过NullWrapperBean传递的参数,会直接赋值null
|
|
|
- // 4、传入参数为null,但是目标参数类型为原始类型,做转换
|
|
|
- // 5、传入参数类型不对应,尝试转换类型
|
|
|
- final Class<?>[] parameterTypes = method.getParameterTypes();
|
|
|
- final Object[] actualArgs = new Object[parameterTypes.length];
|
|
|
- if (null != args) {
|
|
|
- for (int i = 0; i < actualArgs.length; i++) {
|
|
|
- if (i >= args.length || null == args[i]) {
|
|
|
- // 越界或者空值
|
|
|
- actualArgs[i] = ClassUtil.getDefaultValue(parameterTypes[i]);
|
|
|
- } else if (args[i] instanceof NullWrapperBean) {
|
|
|
- //如果是通过NullWrapperBean传递的null参数,直接赋值null
|
|
|
- actualArgs[i] = null;
|
|
|
- } else if (!parameterTypes[i].isAssignableFrom(args[i].getClass())) {
|
|
|
- //对于类型不同的字段,尝试转换,转换失败则使用原对象类型
|
|
|
- final Object targetValue = Convert.convert(parameterTypes[i], args[i]);
|
|
|
- if (null != targetValue) {
|
|
|
- actualArgs[i] = targetValue;
|
|
|
- }
|
|
|
- } else {
|
|
|
- actualArgs[i] = args[i];
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- return (T) method.invoke(ClassUtil.isStatic(method) ? null : obj, actualArgs);
|
|
|
- } catch (Exception e) {
|
|
|
- throw new UtilException(e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 执行对象中指定方法
|
|
|
- * 如果需要传递的参数为null,请使用NullWrapperBean来传递,不然会丢失类型信息
|
|
|
- *
|
|
|
- * @param <T> 返回对象类型
|
|
|
- * @param obj 方法所在对象
|
|
|
- * @param methodName 方法名
|
|
|
- * @param args 参数列表
|
|
|
- * @return 执行结果
|
|
|
- * @throws UtilException IllegalAccessException包装
|
|
|
- * @see NullWrapperBean
|
|
|
- * @since 3.1.2
|
|
|
- */
|
|
|
- public static <T> T invoke(Object obj, String methodName, Object... args) throws UtilException {
|
|
|
- final Method method = getMethodOfObj(obj, methodName, args);
|
|
|
- if (null == method) {
|
|
|
- throw new UtilException(StrUtil.format("No such method: [{}]", methodName));
|
|
|
- }
|
|
|
- return invoke(obj, method, args);
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 执行方法
|
|
|
+ *
|
|
|
+ * <p>
|
|
|
+ * 对于用户传入参数会做必要检查,包括:
|
|
|
+ *
|
|
|
+ * <pre>
|
|
|
+ * 1、忽略多余的参数
|
|
|
+ * 2、参数不够补齐默认值
|
|
|
+ * 3、传入参数为null,但是目标参数类型为原始类型,做转换
|
|
|
+ * </pre>
|
|
|
+ *
|
|
|
+ * @param <T> 返回对象类型
|
|
|
+ * @param obj 对象,如果执行静态方法,此值为<code>null</code>
|
|
|
+ * @param method 方法(对象方法或static方法都可)
|
|
|
+ * @param args 参数对象
|
|
|
+ * @return 结果
|
|
|
+ * @throws UtilException 一些列异常的包装
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public static <T> T invoke(Object obj, Method method, Object... args) throws UtilException {
|
|
|
+ setAccessible(method);
|
|
|
+
|
|
|
+ // 检查用户传入参数:
|
|
|
+ // 1、忽略多余的参数
|
|
|
+ // 2、参数不够补齐默认值
|
|
|
+ // 3、通过NullWrapperBean传递的参数,会直接赋值null
|
|
|
+ // 4、传入参数为null,但是目标参数类型为原始类型,做转换
|
|
|
+ // 5、传入参数类型不对应,尝试转换类型
|
|
|
+ final Class<?>[] parameterTypes = method.getParameterTypes();
|
|
|
+ final Object[] actualArgs = new Object[parameterTypes.length];
|
|
|
+ if (null != args) {
|
|
|
+ for (int i = 0; i < actualArgs.length; i++) {
|
|
|
+ if (i >= args.length || null == args[i]) {
|
|
|
+ // 越界或者空值
|
|
|
+ actualArgs[i] = ClassUtil.getDefaultValue(parameterTypes[i]);
|
|
|
+ } else if (args[i] instanceof NullWrapperBean) {
|
|
|
+ //如果是通过NullWrapperBean传递的null参数,直接赋值null
|
|
|
+ actualArgs[i] = null;
|
|
|
+ } else if (false == parameterTypes[i].isAssignableFrom(args[i].getClass())) {
|
|
|
+ //对于类型不同的字段,尝试转换,转换失败则使用原对象类型
|
|
|
+ final Object targetValue = Convert.convert(parameterTypes[i], args[i]);
|
|
|
+ if (null != targetValue) {
|
|
|
+ actualArgs[i] = targetValue;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ actualArgs[i] = args[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ return (T) method.invoke(ClassUtil.isStatic(method) ? null : obj, actualArgs);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new UtilException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 执行对象中指定方法
|
|
|
+ * 如果需要传递的参数为null,请使用NullWrapperBean来传递,不然会丢失类型信息
|
|
|
+ *
|
|
|
+ * @param <T> 返回对象类型
|
|
|
+ * @param obj 方法所在对象
|
|
|
+ * @param methodName 方法名
|
|
|
+ * @param args 参数列表
|
|
|
+ * @return 执行结果
|
|
|
+ * @throws UtilException IllegalAccessException包装
|
|
|
+ * @see NullWrapperBean
|
|
|
+ * @since 3.1.2
|
|
|
+ */
|
|
|
+ public static <T> T invoke(Object obj, String methodName, Object... args) throws UtilException {
|
|
|
+ final Method method = getMethodOfObj(obj, methodName, args);
|
|
|
+ if (null == method) {
|
|
|
+ throw new UtilException(StrUtil.format("No such method: [{}]", methodName));
|
|
|
+ }
|
|
|
+ return invoke(obj, method, args);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 设置方法为可访问(私有方法可以被外部调用)
|