IllegalAccessError when method reference resolves to method returning inaccessible class
Tagir Valeev
amaembo at gmail.com
Wed May 4 14:40:13 UTC 2022
Consider the following code:
// foo/Foo.java
package foo;
public class Foo {
public static Bar bar() {
return new Bar();
}
static class Bar {}
}
// bar/Baz.java
package bar;
import foo.Foo;
import java.util.function.Supplier;
public class Baz {
public static void foo(Supplier<Object> supplier) {
System.out.println(supplier.get());
}
public static void main(String[] args) {
Baz.foo(Foo::bar);
}
}
It's perfectly compilable (tried javac 17 and 19-ea) but immediately
fails at runtime with
Exception in thread "main" java.lang.IllegalAccessError: failed to
access class foo.Foo$Bar from class bar.Baz (foo.Foo$Bar and bar.Baz
are in unnamed module of loader 'app')
at bar.Baz.main(Baz.java:14)
It looks like VM doesn't like if bootstrap method refers to a method
that mentions inaccessible type in a signature. However, we can
normally call the same method in lambda:
public static void main(String[] args) {
Baz.foo(() -> Foo.bar());
}
I'm not sure whether this is a compiler or VM problem but to me, it
looks wrong when the program after successful complete recompilation
fails with IllegalAccessError. Please correct me if this compiler
behavior is specified.
With best regards,
Tagir Valeev
More information about the compiler-dev
mailing list