I am trying to get a MethodHandle
for any of the constructors of the com.sun.tools.javac.code.TypeMetadata.Annotations
record on JDK 21. Here is the source code of TypeMetadata
(from OpenJDK):
Here is my code:
import com.sun.tools.javac.code.TypeMetadata;import java.lang.invoke.*;import java.util.*;class TestMH { public static void main(String[] args) throws Throwable { MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodType mt = MethodType.methodType(void.class); MethodHandle mh = lookup.findConstructor(TypeMetadata.Annotations.class, mt); System.out.println(mh); }}
I compile by doing javac --add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED TestMH.java
. Here is the output of java --add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED TestMH
:
Exception in thread "main" java.lang.IllegalAccessException: no such constructor: com.sun.tools.javac.code.TypeMetadata$Annotations.<init>()void/newInvokeSpecial at java.base/java.lang.invoke.MemberName.makeAccessException(MemberName.java:911) at java.base/java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:994) at java.base/java.lang.invoke.MethodHandles$Lookup.resolveOrFail(MethodHandles.java:3750) at java.base/java.lang.invoke.MethodHandles$Lookup.findConstructor(MethodHandles.java:2837) at TestMH.main(TestMH.java:9)Caused by: java.lang.IllegalAccessError: class TestMH tried to access method 'void com.sun.tools.javac.code.TypeMetadata$Annotations.<init>()' (TestMH is in unnamed module of loader 'app'; com.sun.tools.javac.code.TypeMetadata$Annotations is in module jdk.compiler of loader 'app') at java.base/java.lang.invoke.MethodHandleNatives.resolve(Native Method) at java.base/java.lang.invoke.MemberName$Factory.resolve(MemberName.java:962) at java.base/java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:991) ... 3 more
Is there some way to get around this IllegalAccessError
, beyond the --add-opens
flag? Or is this a fundamental limitation due to the modules?