1

在链接中: 如何使用 LambdaMetaFactory 实例化对象?提到了如何使用 LambdaMetafactory 实例化一个单参数的构造器。

我正在尝试对默认构造函数执行相同的操作,但由于以下错误而失败:

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;
  }

}

测试类

import java.util.Collections;
import java.util.Map;

public class TestClass {

  public Map<String, String> getContextMap() {
    return Collections.emptyMap();
  }
}

为了使用 LambdaMetafactory 调用默认构造函数,我做错了什么?

4

1 回答 1

3

您的默认构造函数不带任何参数。更改FunctionSupplier

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;
}
于 2019-02-27T22:42:00.967 回答