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.)