[英]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.