简体   繁体   English

如何链接MethodHandle调用?

[英]How to chain MethodHandle invocations?

I have a problem where I have multiple methods with identical signatures (all returning void). 我有一个问题,其中有多个具有相同签名的方法(所有方法都返回void)。 I want to be able to combine the methodHandles for those methods to obtain a methodHandle that will invoke each method in turn. 我希望能够将这些方法的methodHandles组合在一起,从而获得将依次调用每个方法的methodHandle。

The only way I have worked out to do this is with guardWithTest and I have an example of how I can make it work at: https://gist.github.com/gregw/b6c926fb44fd9a45b2c5afccaf7dcbf4 我解决此问题的唯一方法是使用guardWithTest,我在以下网址提供了一个示例说明如何使用它: https ://gist.github.com/gregw/b6c926fb44fd9a45b2c5afccaf7dcbf4

but the essence of the code is: ```java 但是代码的实质是:Java

    TestMethodHandle test = new TestMethodHandle();

    MethodHandle callA = MethodHandles.lookup().findVirtual(TestMethodHandle.class,"callA", methodType(Void.TYPE, String.class, Integer.TYPE)).bindTo(test);
    MethodHandle callB = MethodHandles.lookup().findVirtual(TestMethodHandle.class,"callB", methodType(Void.TYPE, String.class, Integer.TYPE)).bindTo(test);
    MethodHandle callC = MethodHandles.lookup().findVirtual(TestMethodHandle.class,"callC", methodType(Void.TYPE, String.class, Integer.TYPE)).bindTo(test);

    MethodHandle asGuard = MethodHandles.lookup().findStatic(TestMethodHandle.class,"alwaysTrue", MethodType.methodType(Boolean.TYPE));
    MethodHandle guardA = MethodHandles.filterReturnValue(callA,asGuard);
    MethodHandle guardB = MethodHandles.filterReturnValue(callB,asGuard);
    MethodHandle guardC = MethodHandles.filterReturnValue(callC,asGuard);

    MethodHandle empty = empty(methodType(Void.TYPE, String.class, Integer.TYPE));

    MethodHandle invokeC = MethodHandles.guardWithTest(guardC,empty,empty);
    MethodHandle invokeBC = MethodHandles.guardWithTest(guardB,invokeC,empty);
    MethodHandle invokeABC = MethodHandles.guardWithTest(guardA,invokeBC,empty);

    invokeABC.invoke("chained", 2);

``` ```

Is there an easier way to do this? 有没有更简单的方法可以做到这一点?

Supplemental question... should I want to do this or will it be just as fast to call the methodHandles in loop over a collection of method handles? 补充问题...我应该执行此操作,还是在方法句柄集合中循环调用methodHandles一样快?

I think this might be better: 我认为这可能更好:

    MethodHandle callA = MethodHandles.lookup().findVirtual(TestMethodHandle.class,"callA", methodType(Void.TYPE, String.class, Integer.TYPE)).bindTo(test);
    MethodHandle callB = MethodHandles.lookup().findVirtual(TestMethodHandle.class,"callB", methodType(Void.TYPE, String.class, Integer.TYPE)).bindTo(test);
    MethodHandle callC = MethodHandles.lookup().findVirtual(TestMethodHandle.class,"callC", methodType(Void.TYPE, String.class, Integer.TYPE)).bindTo(test);

    MethodHandle foldedABC = MethodHandles.foldArguments(MethodHandles.foldArguments(callC,callB),callA);
    foldedABC.invoke("folded", 42);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM