T invoke(final Object obj, final Method method, final Object... args) throws HutoolException {
- return MethodHandleUtil.invoke(obj, method, args);
+ try{
+ return MethodHandleUtil.invoke(obj, method, args);
+ } catch (final Exception e){
+ // 传统反射方式执行方法
+ try {
+ return (T) method.invoke(ModifierUtil.isStatic(method) ? null : obj, actualArgs(method, args));
+ } catch (final IllegalAccessException | InvocationTargetException ex) {
+ throw new HutoolException(ex);
+ }
+ }
}
/**
@@ -742,6 +754,50 @@ public class MethodUtil {
}
}
+ /**
+ * 检查用户传入参数:
+ *
+ * - 1、忽略多余的参数
+ * - 2、参数不够补齐默认值
+ * - 3、通过NullWrapperBean传递的参数,会直接赋值null
+ * - 4、传入参数为null,但是目标参数类型为原始类型,做转换
+ * - 5、传入参数类型不对应,尝试转换类型
+ *
+ *
+ * @param method 方法
+ * @param args 参数
+ * @return 实际的参数数组
+ */
+ public static Object[] actualArgs(final Method method, final Object[] args) {
+ final Class>[] parameterTypes = method.getParameterTypes();
+ if(1 == parameterTypes.length && parameterTypes[0].isArray()){
+ // 可变长参数,不做转换
+ return args;
+ }
+ 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], args[i]);
+ if (null != targetValue) {
+ actualArgs[i] = targetValue;
+ }
+ } else {
+ actualArgs[i] = args[i];
+ }
+ }
+ }
+
+ return actualArgs;
+ }
+
/**
* 获取方法的唯一键,结构为:
*
diff --git a/hutool-json/src/main/java/org/dromara/hutool/json/convert/JSONConverter.java b/hutool-json/src/main/java/org/dromara/hutool/json/convert/JSONConverter.java
index 40e99b865..aaf723107 100644
--- a/hutool-json/src/main/java/org/dromara/hutool/json/convert/JSONConverter.java
+++ b/hutool-json/src/main/java/org/dromara/hutool/json/convert/JSONConverter.java
@@ -14,13 +14,13 @@ package org.dromara.hutool.json.convert;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.bean.BeanUtil;
+import org.dromara.hutool.core.bean.RecordUtil;
import org.dromara.hutool.core.bean.copier.BeanCopier;
import org.dromara.hutool.core.convert.Convert;
import org.dromara.hutool.core.convert.ConvertException;
import org.dromara.hutool.core.convert.Converter;
import org.dromara.hutool.core.convert.RegisterConverter;
import org.dromara.hutool.core.convert.impl.*;
-import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.map.MapWrapper;
import org.dromara.hutool.core.reflect.ConstructorUtil;
import org.dromara.hutool.core.reflect.TypeReference;
@@ -228,7 +228,7 @@ public class JSONConverter implements Converter {
}
// 无法转换
- throw new JSONException("Can not convert from {}: [{}] to [{}]",
+ throw new JSONException("Can not convert from '{}': {} to '{}'",
json.getClass().getName(), json, targetType.getTypeName());
}
@@ -280,6 +280,11 @@ public class JSONConverter implements Converter {
return (T) ArrayConverter.INSTANCE.convert(type, value);
}
+ // Record
+ if(RecordUtil.isRecord(rowType)){
+ return (T) RecordConverter.INSTANCE.convert(type, value);
+ }
+
// 表示非需要特殊转换的对象
return null;
}
diff --git a/hutool-json/src/main/java/org/dromara/hutool/json/mapper/JSONObjectMapper.java b/hutool-json/src/main/java/org/dromara/hutool/json/mapper/JSONObjectMapper.java
index 8463939d4..357fb62c4 100644
--- a/hutool-json/src/main/java/org/dromara/hutool/json/mapper/JSONObjectMapper.java
+++ b/hutool-json/src/main/java/org/dromara/hutool/json/mapper/JSONObjectMapper.java
@@ -13,10 +13,12 @@
package org.dromara.hutool.json.mapper;
import org.dromara.hutool.core.bean.BeanUtil;
+import org.dromara.hutool.core.bean.RecordUtil;
import org.dromara.hutool.core.bean.copier.CopyOptions;
import org.dromara.hutool.core.convert.Convert;
import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.lang.mutable.MutableEntry;
+import org.dromara.hutool.core.reflect.MethodUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.json.InternalJSONUtil;
import org.dromara.hutool.json.JSONArray;
@@ -30,6 +32,7 @@ import org.dromara.hutool.json.serialize.JSONSerializer;
import java.io.InputStream;
import java.io.Reader;
+import java.lang.reflect.Type;
import java.util.Enumeration;
import java.util.Map;
import java.util.ResourceBundle;
@@ -105,7 +108,7 @@ public class JSONObjectMapper {
if (source instanceof JSONTokener) {
// JSONTokener
mapFromTokener((JSONTokener) source, jsonObject);
- }else if (source instanceof Map) {
+ } else if (source instanceof Map) {
// Map
for (final Map.Entry, ?> e : ((Map, ?>) source).entrySet()) {
jsonObject.set(Convert.toStr(e.getKey()), e.getValue(), predicate, false);
@@ -125,11 +128,14 @@ public class JSONObjectMapper {
} else if (source instanceof ResourceBundle) {
// ResourceBundle
mapFromResourceBundle((ResourceBundle) source, jsonObject);
+ } else if (RecordUtil.isRecord(source.getClass())) {
+ // since 6.0.0
+ mapFromRecord(source, jsonObject);
} else if (BeanUtil.isReadableBean(source.getClass())) {
// 普通Bean
mapFromBean(source, jsonObject);
} else {
- if(!jsonObject.config().isIgnoreError()){
+ if (!jsonObject.config().isIgnoreError()) {
// 不支持对象类型转换为JSONObject
throw new JSONException("Unsupported type [{}] to JSONObject!", source.getClass());
}
@@ -180,6 +186,22 @@ public class JSONObjectMapper {
JSONParser.of(x).parseTo(jsonObject, this.predicate);
}
+ /**
+ * 从Record转换
+ *
+ * @param record Record对象
+ * @param jsonObject {@link JSONObject}
+ */
+ private void mapFromRecord(final Object record, final JSONObject jsonObject) {
+ final Map.Entry[] components = RecordUtil.getRecordComponents(record.getClass());
+
+ String key;
+ for (final Map.Entry entry : components) {
+ key = entry.getKey();
+ jsonObject.set(key, MethodUtil.invoke(record, key));
+ }
+ }
+
/**
* 从Bean转换
*
diff --git a/pom.xml b/pom.xml
index f49d49693..20c902ded 100755
--- a/pom.xml
+++ b/pom.xml
@@ -179,6 +179,9 @@
org.apache.maven.plugins
maven-surefire-plugin
3.0.0
+
+ --add-opens java.base/java.lang=ALL-UNNAMED
+
org.apache.maven.plugins