javac doesn't generate the correct bytecode for lambda if the target type is an intersection type

Remi Forax forax at
Thu Nov 5 09:37:27 UTC 2015

About this bug,

javac has trouble with intersection type that are target type of a lambda and method reference,
Usually when there is an intersection type, javac substitute it by the first type of the intersection type and add cast when necessary.

Let suppose we have this code,

public class Intersection {
  interface I {
  interface J {
    void foo();

  static <T extends I & J> void bar(T t) {
      Runnable r = t::foo;
  public static void main(String[] args) {
    class A implements I, J { public void foo() {} }
    bar(new A());

Currently, javac generates a method reference on J::foo with an invokedynamic that takes an I as parameter, hence it fails at runtime.
javac should de-sugar t::foo into a lambda that take an I and then add a cast to J like for a call to a method of an intersection type. 

So the workaround is to use a lambda instead,
  Runnable r = t ->;

I've already seen this bug somewhere but was not able to find a corresponding bug report in the database :(



More information about the compiler-dev mailing list