4

I've run into code which compiles fine but throws IllegalAccessError during runtime if a method reference is used instead of a lambda.

The code involves using a non-visible package-private class as the parameter type of a public method in a public class.

Are such designs something that should be avoided in future Java versions? (Or is it just a bug in the current version? (8u201))

Minimal code follows:

package1/Test.java:

package package1;

import java.util.function.Predicate;

import package2.Child;

public class Test {

    public static void main(String[] args) {

        Predicate<Child> p1 = x -> Child.check(x);
        p1.test(new Child()); // Passes

        Predicate<Child> p2 = Child::check; // <-- This line throws IAE
        p2.test(new Child());
    }
}

package2/Child.java:

package package2;

class Base {
}

public class Child extends Base {

    public static boolean check(Base base) {
        return true;
    }
}

(This code is minimal and doesn't make semantics sense.)

antak
  • 19,481
  • 9
  • 72
  • 80
  • I reopened, as this seems to be a different issue as the linked Q&A, as it is not about the visibility of the method itself (or its receiver type), but rather about the parameter type. While the former are fixed in recent Java versions, this one is still reproducible under Java 11. – Holger Jan 22 '19 at 14:07

0 Answers0