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