MethodHandle 示例在调用 invokeExact 时抛出 WrongMethodTypeException
MethodHandle
类描述中显示的示例在调用语句 mh.invokeExact("daddy",'d','n 时抛出
并具有以下描述:WrongMethodTypeException
')(CC)Ljava/lang/String;不能使用与 ([Ljava/lang/Object;)Ljava/lang/Object;
不同的参数来调用。
MethodHandle
对象 mh
具有对应于:(CC)Ljava/lang/String
的符号类型描述符。但是当我们调用 mh.invokeExact("daddy",'d','n')
时,参数:d
和 n
是作为 Object
数组传递,然后它们与 char
类型的参数不匹配。
我知道我可以使用 invokeWithArguments
而不是 invokeExcat
或 invoke
解决上述问题,但此示例应该按所示方式工作在Java 7 API的MethodHandle
的描述中。除此之外,invokeWithArguments
的性能开销与 invoke
/invokeExact
相关。
The example shown in the description of the MethodHandle
class throws a WrongMethodTypeException
in the invocation of the statement mh.invokeExact("daddy",'d','n')
with the following description: (CC)Ljava/lang/String; cannot be called with a different arity as ([Ljava/lang/Object;)Ljava/lang/Object;
.
The MethodHandle
object mh
has a symbolic type descriptor corresponding to: (CC)Ljava/lang/String
. But when we are invoking mh.invokeExact("daddy",'d','n')
, the arguments: d
and n
are passed as an Object
array and then they are not matching with the arguments of the type char
.
I know that I can resolve the above problem using the invokeWithArguments
instead of the invokeExcat
or the invoke
, but this example was supposed to work as presented in the description of the MethodHandle
of Java 7 API. Besides that, the invokeWithArguments
has a performance overhead in relation to invoke
/invokeExact
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你如何编译这个?
对我来说,这听起来很像一个已知的 Eclipse bug。
我刚刚检查了 javac 和这段代码:
似乎工作正常:
javap 输出的相关部分似乎也很正常:
How are you compiling this?
It sounds suspiciously like a known Eclipse bug to me.
I've just checked with javac and this code:
seems to work OK:
The relevant portion of output from javap seems sane as well:
invokeExact
要求 MH 的方法类型描述和参数类型之间完全匹配。由于 MH 的方法类型是(cc)string
,所以要执行 MH,第一个和第二个参数都应该是 char。于是,事情就这样了invokeExact
requires exact match between the MH's method type description, and the types of arguments. Since the MH's method type is(cc)string
, so what you want to execute the MH, both the first and second argument should be char. Thus, it is like that