The general questions are:
object::aMethod
, can it be converted to a type such as MethodHandle
as a functional interface?As an example, imagine we'd like to have a MethodRefRunner
as:
class MethodRefRunner {
static void execute(Object target, WHATTYPE mref, Object... args) {
mref.apply(args);
}
}
And it can be used as: MethodRefRunner.execute(o, o::someMethod, someParam)
In the above snippet, one option for WHATTYPE
is java.util.function.Function
but very restrictive. As this answer mentions, prior to version b75, there was a java.util.function.Block
was available and might be handy.
On the other side, any chance that WHATTYPE
could be in some way converted to a java.lang.invoke.MethodHandle
?
Note to Java experts: Please refine the title of the questions as needed.
Types of Method ReferencesStatic Method Reference. Instance Method Reference of a particular object. Instance Method Reference of an arbitrary object of a particular type. Constructor Reference.
I don't think there is any way to do what you want. WHATTYPE is going to have to be a functional interface—not necessarily Function
, but one whose single abstract method matches somemethod
. It's an ordinary interface type, subject to the usual rules governing Java types. java.util.function.Block
was an ordinary interface type like this, and not special in the way that you seem to think. (It's still around, by the way, now called Consumer
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With