我对方法句柄的方法参数感到困惑。我先构建一个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");
}
}
对我来说,问题是如何将参数传递给三个方法句柄:test、trueTarget 和 faliover。1、invokeExact的第一个参数“result”作为receiver传递给test guard,第二个参数“data”传递给startWith:
String.startsWith(String)
"result" "data"
但是这三个参数被传递给 falseTarget 为:
falseTarget(String str, String own, String t)
"result" "data" "sijie"
那么,参数传递的规则是什么,它们如何与方法句柄引用的方法匹配?