Symbolic argument support in InvokeBinder

Charles Oliver Nutter headius at headius.com
Sat Feb 2 03:50:38 PST 2013


Sitting here at FOSDEM today I was showing Remi my new addition to
InvokeBinder: named arguments.

Background: InvokeBinder is my little Java DSL/fluent API for building
method handle chains. Short example:

MethodHandle mh = Binder
   .from(String.class, String.class, String.class) // String w(String, String)
   .drop(1, String.class) // String x(String)
   .insert(0, 'hello') // String y(String, String)
   .cast(String.class, CharSequence.class, Object.class) // String
z(CharSequence, Object)
   .invoke(someTargetHandle);

The new stuff I added is a Signature class for managing a MethodType
along with an array of argument names, and SmartBinder to take
advantage of that. How is this useful? The above example might be
reworked as follows:

Signature sig = Signature
    .returning(String.class)
    .appendArg("arg1", String.class)
    .appendArg("arg2", String.class);

MethodHandle mh = SmartBinder
   .from(sig)
   .drop("arg2") // String x(String)
   .prepend("argX", 'hello') // String y(String, String)
   .cast(String.class, CharSequence.class, Object.class) // String
z(CharSequence, Object)
   .invoke(someTargetHandle);

So we can always use the argument names rather than error-prone
indices. This is especially useful for permutes, which I consistently
get completely wrong:

MethodHandle incoming = handle with signature below;

Signature sig = Signature
    .returning(String.class)
    .appendArg("arg1", String.class)
    .appendArg("arg2", String.class)
    .appendArg("arg3", String.class);

// permute without indices!
MethodHandle permuted = sig.permuteWith(incoming, "arg1", "arg3");

This is not in an InvokeBinder release yet because I want to add all
Binder operations to SmartBinder, but I'm looking for feedback and
other use cases for named arguments in the signature. Thanks!

- Charlie


More information about the mlvm-dev mailing list