mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
新增对instantiatedType数组的支持
This commit is contained in:
@@ -2,7 +2,6 @@ package cn.hutool.core.lang.func;
|
|||||||
|
|
||||||
import cn.hutool.core.classloader.ClassLoaderUtil;
|
import cn.hutool.core.classloader.ClassLoaderUtil;
|
||||||
import cn.hutool.core.reflect.FieldUtil;
|
import cn.hutool.core.reflect.FieldUtil;
|
||||||
import cn.hutool.core.text.StrUtil;
|
|
||||||
|
|
||||||
import java.lang.invoke.SerializedLambda;
|
import java.lang.invoke.SerializedLambda;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
@@ -41,13 +40,22 @@ public class LambdaInfo {
|
|||||||
}
|
}
|
||||||
int index = lambda.getInstantiatedMethodType().indexOf(";)");
|
int index = lambda.getInstantiatedMethodType().indexOf(";)");
|
||||||
if (index > -1) {
|
if (index > -1) {
|
||||||
String[] instantiatedTypeNames = StrUtil.sub(lambda.getInstantiatedMethodType(), 2, index).split(";L");
|
boolean isArray = lambda.getInstantiatedMethodType().startsWith("([");
|
||||||
this.instantiatedTypes = new Type[instantiatedTypeNames.length];
|
if (isArray) {
|
||||||
for (int i = 0; i < instantiatedTypeNames.length; i++) {
|
try {
|
||||||
this.instantiatedTypes[i] = ClassLoaderUtil.loadClass(instantiatedTypeNames[i]);
|
this.instantiatedTypes = new Type[]{Class.forName(lambda.getInstantiatedMethodType().replace("/", ".").substring(0, index).substring(1) + ";")};
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new IllegalStateException(e);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
String[] instantiatedTypeNames = lambda.getInstantiatedMethodType().substring(2, index).split(";L");
|
||||||
|
this.instantiatedTypes = new Type[instantiatedTypeNames.length];
|
||||||
|
for (int i = 0; i < instantiatedTypeNames.length; i++) {
|
||||||
|
this.instantiatedTypes[i] = ClassLoaderUtil.loadClass(instantiatedTypeNames[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
instantiatedTypes = new Type[0];
|
this.instantiatedTypes = new Type[0];
|
||||||
}
|
}
|
||||||
this.clazz = (Class<?>) FieldUtil.getFieldValue(executable, "clazz");
|
this.clazz = (Class<?>) FieldUtil.getFieldValue(executable, "clazz");
|
||||||
this.executable = executable;
|
this.executable = executable;
|
||||||
|
@@ -128,6 +128,10 @@ public class LambdaUtilTest {
|
|||||||
// 引用父类静态无参方法,能够获取到正确的参数类型
|
// 引用父类静态无参方法,能够获取到正确的参数类型
|
||||||
Func1<MyTeacher, ?> lambda = MyTeacher::takeIdBy;
|
Func1<MyTeacher, ?> lambda = MyTeacher::takeIdBy;
|
||||||
Assert.assertEquals(MyTeacher.class, LambdaUtil.getRealClass(lambda));
|
Assert.assertEquals(MyTeacher.class, LambdaUtil.getRealClass(lambda));
|
||||||
|
}, () -> {
|
||||||
|
// 数组测试
|
||||||
|
VoidFunc1<String[]> lambda = (String[] stringList) -> {};
|
||||||
|
Assert.assertEquals(String[].class, LambdaUtil.getRealClass(lambda));
|
||||||
}).forEach(Runnable::run);
|
}).forEach(Runnable::run);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user