简体   繁体   English

MethodHandle InvokeExact参数

[英]MethodHandle InvokeExact parameter

I am confused by method parameters for method handles. 我对方法句柄的方法参数感到困惑。 I first build a guardwithtest method handle as shown below: 我首先构建了一个guardwithtest方法句柄,如下所示:

public class App 
{
    public static void trueTarget(String str, String own, String t){
        System.out.println("This is true target "+str+" "+own + " "+t);
    }

    public static void falseTarget(String str, String own, String t){
        System.out.println("This is false target " + str+"  "+own +" "+t);
    }

    public static void main( String[] args ) throws Throwable
    {
        MethodHandle test = MethodHandles.publicLookup().findVirtual(String.class, "startsWith", 
                MethodType.methodType(boolean.class, String.class));

        System.out.println((boolean)test.invokeExact("result", "res"));

        MethodHandle target = MethodHandles.lookup().findStatic(App.class, "trueTarget", MethodType.methodType(void.class, String.class, String.class, String.class));
        MethodHandle fallback = MethodHandles.lookup().findStatic(App.class, "falseTarget", MethodType.methodType(void.class, String.class, String.class, String.class));

        MethodHandle gwd = MethodHandles.guardWithTest(test, target, fallback);

        gwd.invokeExact("result", "data", "sijie");

    }
}

The problem for me is that how parameters are passed to three method handles: test, trueTarget and faliover. 对我来说,问题在于如何将参数传递给三个方法句柄:test,trueTarget和faliover。 1, The first parameter for invokeExact "result" is passed to the test guard as receiver, and the 2nd parameter "data" is passed to startWith: 1,invokeExact“结果”的第一个参数作为接收器传递给测试防护,而第二个参数“数据”传递给startWith:

      String.startsWith(String)
      "result"          "data"  

But the these three parameters are passed to the falseTarget as: 但是,这三个参数通过以下方式传递给falseTarget:

      falseTarget(String str, String own, String t)
                        "result"      "data"     "sijie"

So, what's the rule for parameters passing and how do they match to the methods referenced by a method handle? 那么,传递参数的规则是什么,它们与方法句柄引用的方法如何匹配?

This appears in the Javadoc of findVirtual 这出现在findVirtual的Javadoc中

When called, the handle will treat the first argument as a receiver and dispatch on the receiver's type to determine which method implementation to enter. 调用时,句柄会将第一个参数视为接收方,并根据接收方的类型进行分派,以确定要输入的方法实现。

It's exactly as you described it. 正是您所描述的。 A static method does not have a receiver and therefore all arguments to invokeExact are considered arguments to the method. static方法没有接收器,因此所有invokeExact参数都被视为该方法的参数。

暂无
暂无

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

相关问题 MethodHandle invokeExact使用return和parameter调用静态方法 - MethodHandle invokeExact a static method with return and parameter Java 8 | 在字段上动态使用 MethodHandle#invokeExact - Java 8 | Using MethodHandle#invokeExact on fields dynamically 如何使用Object []数组调用MethodHandle.invokeExact()? - How to call MethodHandle.invokeExact() with an array of Object[]? MethodHandle示例在invokeExact调用时抛出WrongMethodTypeException - MethodHandle example throws WrongMethodTypeException on invokeExact call Gradle Android Error:MethodHandle.invoke和MethodHandle.invokeExact - Gradle Android Error: MethodHandle.invoke and MethodHandle.invokeExact 由使用MethodHandle :: invokeExact作为方法引用导致的LambdaConversionException引起的BootstrapMethodError - BootstrapMethodError caused by LambdaConversionException caused by using MethodHandle::invokeExact as a method reference 为什么(int)MethodHandle.invokeExact缺少checkcast-instruction? - Why checkcast-instruction is absent for (int)MethodHandle.invokeExact? 为什么即使 MethodHandle 没问题,在 invokeExact 上也会得到 WrongMethodTypeException - Why do I get a WrongMethodTypeException on invokeExact even though MethodHandle is OK MethodHandle.invoke 和 MethodHandle.invokeExact 仅从 Android O (--min-api 26) 开始支持 - MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26) 尝试在 Android 中运行 Spark:仅从 Android O 开始支持 MethodHandle.invoke 和 MethodHandle.invokeExact - Trying to run Spark in Android: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM