MethodHandle.bindTo chain fails with "IllegalArgumentException: no leading reference parameter"

Vladimir Ivanov vladimir.x.ivanov at oracle.com
Tue Jul 25 15:38:45 UTC 2017


> DVTs were deliberately forbidden in MH.bindTo() (8184920 [1]).
> The method contract explicitly states that only reference types can be 
> bound:
>       * @throws IllegalArgumentException if the target does not have a
>       *         leading parameter type that is a reference type

MethodHandles.insertArguments() should be used instead.

Best regards,
Vladimir Ivanov

> [1] https://jbs.oracle.com/browse/JDK-8184920
> 
> On 7/25/17 6:26 PM, Tobias Hartmann wrote:
>> Hi,
>>
>> below test fails with:
>> Exception in thread "main" java.lang.IllegalArgumentException: no 
>> leading reference parameter
>>     at 
>> java.base/java.lang.invoke.MethodHandleStatics.newIllegalArgumentException(MethodHandleStatics.java:134) 
>>
>>     at 
>> java.base/java.lang.invoke.MethodType.leadingReferenceParameter(MethodType.java:531) 
>>
>>     at 
>> java.base/java.lang.invoke.MethodHandle.bindTo(MethodHandle.java:1419)
>>     at Test.test(Test.java:35)
>>     at Test.main(Test.java:41)
>>
>> Am I doing something wrong or is this case not yet supported?
>>
>> Thanks,
>> Tobias
>>
>>
>> import java.lang.invoke.*;
>>
>> __ByValue final class MyValue1 {
>>    final int x;
>>
>>    private MyValue1() {
>>      this.x = 0;
>>    }
>>    __ValueFactory static MyValue1 createDefaultInline() {
>>      return __MakeDefault MyValue1();
>>    }
>> }
>>
>> public class Test {
>>
>>    static final MethodHandle mh;
>>    static {
>>      try {
>>        mh = 
>> MethodHandles.lookup().unreflect(Test.class.getMethod("test_target", 
>> MyValue1.class));
>>      } catch (NoSuchMethodException|IllegalAccessException e) {
>>        e.printStackTrace();
>>        throw new RuntimeException("method handle lookup fails");
>>      }
>>    }
>>
>>    MyValue1 test_vt = MyValue1.createDefaultInline();
>>
>>    public int test_target(MyValue1 vt) {
>>      return vt.x;
>>    }
>>
>>    long test() throws Throwable {
>>      MethodHandle mh1 = mh.bindTo(this);
>>      MethodHandle mh2 = mh1.bindTo(test_vt);
>>      return (long)mh2.invoke();
>>    }
>>
>>    public static void main(String[] args) throws Throwable {
>>      Test t = new Test();
>>      t.test();
>>    }
>> }
>>



More information about the valhalla-dev mailing list