On Tue, Nov 29, 2011 at 12:59 PM, Jochen Theodorou <[email protected]> wrote:
> I should add, that the problems I have now are still very basic ones... more
> about how to use the API correctly. I have for example to replace an
> argument with a new value, but no real idea how to do that.

I sympathize. The method handle API is very powerful, but very
low-level. It takes some time to get used to it.

If you want to replace an incoming argument, you probably want to use
filterArguments like this:

// I always import static MethodHandles.* and MethodType.* to clean up code

MethodHandle handle = <get handle to System.setProperty>

// this line wraps a constant handle with a "drop", so it ignores the
String it is passed
MethodHandle replaceWithFoo = dropArguments(constant(String.class,
"foo"), 0, String.class);

// this line replaces the incoming property key with "foo", unconditionally
handle = filterArguments(handle, 0, replaceWithFoo);

Of course you can use a handle that actually processes the argument
and changes it into something else, like this version that upcases the
both strings on the way through:

MethodHandle upcase = lookup.findVirtual(String.class, "toUpperCase",
methodType(String.class);
handle = filterArguments(handle, 0, upcase, upcase);

It definitely takes a while to grok all the combinations of method
handle adapters, but once you get the hang of it you can do some
amazing things.

- Charlie

-- 
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.

Reply via email to