Le 24/05/2011 16:41, Ola Bini a écrit :
Hi,
Maybe this is old news to everyone else, but I found a neat trick with
methodhandles that came quite in handy. Basically, my problem was that I
have lots of different specialized arities in order to not wrap and
unwrap stuff. The basic requirement is to be able to create a callsite
such that it will pass on any arguments it gets sent at that specific
call site. You _can_ do this with asCollector and asSpreader, but I'm
not sure if that will actually create a new array on the path.
(In order to do this trick, the main activation sequence for my objects
has one method that returns a methodhandle bound to the specific arity).
It turns out it's unnecessary though, because you can always pass on any
arguments you get to another method handle, by doing something like this
in your CallSite:
slowPath() {
setTarget(filterArguments(findVirtual(MethodHandle.class, "invoke",
type()), 0, insertArguments(findVirtual(SephObject.class,
"invocationFor", SephObject.class, int.class), 1, arity()));
}
you mean:
setTarget(filterArguments(findVirtual(MethodHandle.class, "invoke",
type()), 0, insertArguments(findVirtual(SephObject.class,
"invocationFor", MethodHandle.class, int.class), 1, arity()));
^^^^^^^^^^^^
Obviously this is a bit simplified, but the key insight is that you can
use findVirtual to lookup a method handle to MethodHandle.invoke of any
arity you want.
It's what we call an invoker.
http://download.java.net/jdk7/docs/api/java/lang/invoke/MethodHandles.html#invoker%28java.lang.invoke.MethodType%29
And yes, this is powerfull :)
Just a question, why invocationFor is called on the receiver
and not on the receiver class (or prototype).
Cheers
Rémi
--
You received this message because you are subscribed to the Google Groups "JVM
Languages" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/jvm-languages?hl=en.