gpt4 book ai didi

java - 如何为具有变体参数的方法构造 MethodType

转载 作者:太空宇宙 更新时间:2023-11-04 06:14:59 25 4
gpt4 key购买 nike

我无法为 Java 中的方法查找创建 MethodType。下面是我的代码。在这段代码中,我想为sample::gwd方法创建一个MethodType,然后通过lookup().findStatic检索对此函数的引用。很明显,我无法获取方法引用,因为 MethodType 构造错误。

//I want to construct MethodType for Sample:gwd method, but do not know how to handle array parameters for 'gwd' method
MethodType mt = MethodType.methodType(Object.class, MethodHandle.class, MethodHandle.class, MethodHandle.class);

MethodHandle myMH = MethodHandles.lookup().findStatic(Sample.Class, "gwd", mt);

public class Sample
{
public static Object gwd(MethodHandle methodhandle, MethodHandle methodhandle1, MethodHandle methodhandle2, Object aobj[])
throws Throwable
{ .......... }
}

谁能帮忙吗?谢谢

最佳答案

您非常接近,您传递给 MethodHandles#lookupMethodType 缺少最后一个参数,即 Objects 数组。这就是您所需要的:

MethodType mt = MethodType.methodType(Object.class, MethodHandle.class, MethodHandle.class, MethodHandle.class, Object[].class);

顺便说一句,如果 gwd 使用可变参数而不是最终数组,这也是您所需要的。

关于java - 如何为具有变体参数的方法构造 MethodType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28205590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com