void return type check in method reference to PolymorphicSignature method

Tagir Valeev amaembo at gmail.com
Thu Oct 1 11:29:52 UTC 2020


I've found that this code is not compilable in Java 9 and Java 10, but
compilable in Java 11 or later (yet fails at runtime):

import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.Arrays;

class Test {
  interface VH {
    int set(int[] arr, int idx, int val);

  public static void main(String[] args) {
    VarHandle vh = MethodHandles.arrayElementVarHandle(int[].class);
    //VH mr = (arr, idx, val) -> vh.set(arr, idx, val); // not compilable
    VH mr = vh::set; // compilable but runtime error
    int[] data = {0};
    mr.set(data, 0, 2);

Fails with

Exception in thread "main" java.lang.NoSuchMethodError:
at java.base/java.lang.invoke.MethodHandleNatives.newNoSuchMethodErrorOnVarHandle(MethodHandleNatives.java:589)
at java.base/java.lang.invoke.MethodHandleNatives.varHandleOperationLinkerMethod(MethodHandleNatives.java:542)
at java.base/java.lang.invoke.MethodHandleNatives.linkMethodImpl(MethodHandleNatives.java:475)
at java.base/java.lang.invoke.MethodHandleNatives.linkMethod(MethodHandleNatives.java:463)
at Test.main(Test.java:15)

However, very similar lambda is expectedly not compilable. Is it
expected behavior? Looks like a regression to me. I don't see any
changes in JLS 15.13 between Java 10 and Java 11. Am I missing

With best regards,
Tagir Valeev

More information about the compiler-dev mailing list