Ich möchte ein Methoden-Handle erstellen, das es mir erlaubt, als Argument einen Wert zu übergeben, der an einen Platzhalter in der Methoden-Handle-Struktur gebunden ist.Java-Methodenhandles: ungebundene Argumente über Funktionen hinweg propagieren
Figur, etwa wie folgt: f(x) = plus(minus(x, 2), 3)
wo x bei invoke übergeben wird, und 2 und 3 sind einige Konstanten MethodHandles, die immer 2 oder 3
Ich laufe in ein Problem zurück, die ich nicht verstehe, :
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
public class TTest {
public static double plus(double a, double b) { return a + b; }
public static double minus(double a, double b) { return a - b; }
public static void main(String[] args) throws Throwable {
MethodHandle mh_minus = MethodHandles.lookup().findStatic(TTest.class, "minus", MethodType.methodType(double.class, double.class, double.class));
MethodHandle mh_plus = MethodHandles.lookup().findStatic(TTest.class, "plus", MethodType.methodType(double.class, double.class, double.class));
// f(x) = plus(minus(x, 2), 3)
MethodHandle doubleInvoker = MethodHandles.exactInvoker(MethodType.methodType(double.class));
// mh_minus takes 2 doubles as input. The second one needs to take a MethodHandle that returns a constant:
MethodHandle minus_2 = MethodHandles.filterArguments(mh_minus, 1, doubleInvoker);
minus_2 = MethodHandles.insertArguments(minus_2, 1, MethodHandles.constant(double.class, 2));
// mh_plus takes 2 doubles as input. The second one needs to take a MethodHandle that returns a constant:
MethodHandle plus_3 = MethodHandles.filterArguments(mh_plus, 1, doubleInvoker);
plus_3 = MethodHandles.insertArguments(plus_3, 1, MethodHandles.constant(double.class, 3));
// the first arg of plus_3 is minus_2, but minus_2 is a MethodHandle that takes a double and returns a double, so we need to filter the first arg of plus_3 by an exact invoker
// EXCEPTION HERE:
plus_3 = MethodHandles.filterArguments(plus_3, 0, MethodHandles.exactInvoker(MethodType.methodType(double.class, double.class)));
MethodHandle comp = plus_3.bindTo(minus_2);
double res = (double)comp.invokeExact(3.0); // performs (3 - 2) + 3
}
}
Das gibt eine Ausnahme, wenn sie versuchen zu filtern plus_3: mit minus
Exception in thread "main" java.lang.IllegalArgumentException: target and filter types do not match: (double)double, (MethodHandle,double)double
at java.lang.invoke.MethodHandleStatics.newIllegalArgumentException(MethodHandleStatics.java:145)
at java.lang.invoke.MethodHandles.filterArgumentChecks(MethodHandles.java:2631)
at java.lang.invoke.MethodHandles.filterArgument(MethodHandles.java:2608)
at java.lang.invoke.MethodHandles.filterArguments(MethodHandles.java:2601)
at TTest.main(TTest.java:31)
Was ich nicht verstehe, ist, wie ich komponieren kann plus, w Hier hat Minus ein Argument, das noch nicht erfüllt ist.
Können Sie mir helfen?