00:00

Strong Encapsulation of JDK Internals in Java 17

Last updated: · Standard (Java 17)

Since Java 17 (JEP 403), the internal classes and private members of the JDK are strongly encapsulated. Code can no longer use deep reflection, such as setAccessible(true), on JDK internals, and the --illegal-access option that relaxed this rule has no effect any more.

This tutorial shows the error you will see, why the change was made, and how to fix or temporarily work around it.

1. Why Was This Change Made?

The module system in Java 9 separated the supported APIs of the JDK from its internals. For compatibility, Java 9 to 15 still allowed deep reflection into internals with a warning. Libraries that rely on internals break when the JDK changes, and internals can expose security risks. Java 16 (JEP 396) denied such access by default, and Java 17 removed the switch that could re-enable it.

Critical internal APIs such as sun.misc.Unsafe remain available through the jdk.unsupported module, because there were no public replacements yet.

2. The Options That Matter

OptionEffect in Java 17
--illegal-access=permitignored with a warning; support removed
--add-opens module/package=targetallows deep reflection into one package
--add-exports module/package=targetallows compile-time and run-time access to public types of an internal package
Add-Opens: in a JAR manifestsame as --add-opens for executable JARs

3. Practical Examples

Example 1: Deep reflection is blocked

DeepReflection.java
import java.lang.reflect.Field;
import java.lang.reflect.InaccessibleObjectException;
import java.util.ArrayList;

public class DeepReflection {
    public static void main(String[] args) throws Exception {
        ArrayList<String> list = new ArrayList<>();
        list.add("a");

        // Reading a private field of a JDK class with reflection
        Field elementData = ArrayList.class.getDeclaredField("elementData");
        try {
            elementData.setAccessible(true);
            Object[] array = (Object[]) elementData.get(list);
            System.out.println("Internal capacity: " + array.length);
        } catch (InaccessibleObjectException e) {
            String msg = e.getMessage();
            System.out.println("Blocked: " + msg.substring(0, msg.indexOf(" to unnamed module")));
        }
    }
}
Output
Blocked: Unable to make field transient java.lang.Object[] java.util.ArrayList.elementData accessible: module java.base does not "opens java.util"

Compile and run: java DeepReflection

Example 2: The old --illegal-access option is ignored

Command: java --illegal-access=permit DeepReflection

Output
Blocked: Unable to make field transient java.lang.Object[] java.util.ArrayList.elementData accessible: module java.base does not "opens java.util"
Java HotSpot(TM) 64-Bit Server VM warning: Ignoring option --illegal-access=permit; support was removed in 17.0

The JVM prints a warning that the option was removed in 17.0, and the access is still denied. This output came from an Oracle JDK 21 runtime, which prints the same message.

Example 3: Opening one package explicitly

Command: java --add-opens java.base/java.util=ALL-UNNAMED DeepReflection

Output
Internal capacity: 10

4. How Access Is Decided

Access check for deep reflection setAccessible on a JDK member succeeds only if the module opens that package to the caller. Without an --add-opens option, java.base does not open java.util, so an InaccessibleObjectException is thrown. DEFAULT setAccessible(true) on ArrayList field java.base opens java.util? no InaccessibleObjectException access denied --ADD-OPENS setAccessible(true) on ArrayList field java.util opened to ALL-UNNAMED access granted prints capacity 10
Figure 1: Deep reflection works only for packages that are explicitly opened.

5. Important Points and Best Practices

  • Upgrade libraries first; most popular frameworks stopped using JDK internals long ago.
  • Use jdeps --jdk-internals your.jar to find dependencies on internal APIs.
  • Treat --add-opens as a temporary workaround and open only the packages you need.
  • Replace internal APIs with supported ones, for example VarHandle instead of many Unsafe uses.

6. Summary

  • Java 17 (JEP 403) strongly encapsulates JDK internals.
  • Deep reflection into JDK packages throws InaccessibleObjectException.
  • --illegal-access no longer has any effect.
  • --add-opens opens specific packages as a temporary workaround.

Related Tutorials

7. References