在链接中: How to instantiate an object using LambdaMetaFactory?提到了如何使用LambdaMetafactory实例化一个单参数contrcutor。
我正在尝试对default-constructor进行相同的操作,但是由于以下错误而失败:
Exception in thread "main" java.lang.AbstractMethodError: Method com/test/Main$$Lambda$1.apply(Ljava/lang/Object;)Ljava/lang/Object; is abstract
at com.test.Main$$Lambda$1/186370029.apply(Unknown Source)
at com.test.Main.test2(Main.java:29)
at com.test.Main.main(Main.java:14)
我要运行的代码:
import java.lang.invoke.LambdaMetafactory;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.function.Function;
public class Main {
public static void main(String[] args) throws Throwable {
long t2= System.nanoTime();
for(int i=0;i<10000;i++){
test2(TestClass.class.getName());
}
long t3= System.nanoTime();
System.out.println((t3-t2)*1e-9);
}
private static TestClass test2(String objclass)
throws Throwable {
Class clazz = Class.forName(objclass);
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle mh = lookup.findConstructor(clazz, MethodType.methodType(void.class));
Function<String, TestClass> constructor = (Function<String, TestClass>) LambdaMetafactory
.metafactory(lookup, "apply",MethodType.methodType(Function.class),
mh.type().generic(), mh, mh.type()).getTarget().invokeExact();
TestClass testClass = constructor.apply(objclass);
return testClass;
}
}
TestClass
import java.util.Collections;
import java.util.Map;
public class TestClass {
public Map<String, String> getContextMap() {
return Collections.emptyMap();
}
}
使用LambdaMetafactory调用默认构造函数时,我在做什么错了?
答案 0 :(得分:2)
您的默认构造函数不带参数。将Function
更改为Supplier
:
private static TestClass test2(String objclass)
throws Throwable {
Class clazz = Class.forName(objclass);
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle mh = lookup.findConstructor(clazz, MethodType.methodType(void.class));
Supplier<TestClass> constructor = (Supplier<TestClass>) LambdaMetafactory.metafactory(
lookup, "get", MethodType.methodType(Supplier.class), mh.type().generic(), mh, mh.type()
).getTarget().invokeExact();
TestClass testClass = constructor.get();
return testClass;
}