Search code examples
javajava-modulejava-17

Why Reflection Calls Do Not Work Anymore in Java 17?


We migrated from Java 8 to Java 17 and the following error started to pop up due to a reflection call in the code:

java.lang.IllegalAccessException: class com.twitter.chill.Instantiators$ cannot access a member of class org.apache.avro.Schema$StringSchema with modifiers "public"
    at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:392)
    at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:674)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:489)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
    at com.twitter.chill.Instantiators$.$anonfun$normalJava$1(KryoBase.scala:169)
    at com.twitter.chill.Instantiators$$anon$1.newInstance(KryoBase.scala:137)
    ... 46 common frames omitted

If I understand correctly, the problem is in the new Java modular system and I need to open some modules to make it work. So how do I know what modules I should open?

I looked at the error message and thought it required me to add --add-opens=org.apache/org.apache.avro=com.twitter.chill to allow the package com.twitter.chill to access org.apache.avro.Schema, but it does not work. I also tried --add-opens=org.apache/org.apache.avro=java.base/jdk.internal.reflect with the same success (or rather its absence).


Solution

  • I think the real issue is that org.apache.avro.Schema$StringSchema is a private inner class, so you don't have access to its members (public or otherwise) from outside its declaring class