JDK9 encapsulation problem

Stephen Felts stephen.felts at oracle.com
Fri Sep 9 14:30:41 UTC 2016

We have an application that is running into a problem with a utility program.  Below is a standalone reproducer.


The program does not import the SPI package sun.nio.ch - it isn't aware of

it, and SocketChannel.isConnected() is a public method of a public type. In

short, it does not break any law of encapsulation, so call

setAccessible(true) should be OK.


import java.lang.reflect.Method;



public class JDK9Nio {

  public static void main(String args[]) throws Exception {




  public static void call() throws Exception {

    Class clzz = Class.forName("java.nio.channels.SocketChannel");

    Method open = clzz.getMethod("open");

    Object obj = open.invoke(null);

    Method isConn = obj.getClass().getMethod("isConnected");

    isConn.setAccessible(true); // OK with JDK8, fail with JDK9






java JDK9Nio

Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make member of class sun.nio.ch.SocketChannelImpl accessible:  module java.base does not export sun.nio.ch to unnamed module @3857f613

        at jdk.internal.reflect.Reflection.throwInaccessibleObjectException(java.base at 9-ea/Reflection.java:414)

        at java.lang.reflect.AccessibleObject.checkCanSetAccessible(java.base at 9-ea/AccessibleObject.java:174)

        at java.lang.reflect.Method.checkCanSetAccessible(java.base at 9-ea/Method.java:192)

        at java.lang.reflect.Method.setAccessible(java.base at 9-ea/Method.java:186)

        at JDK9Nio.call(JDK9Nio.java:14)

        at JDK9Nio.main(JDK9Nio.java:6)




It's easy to say that the program should be re-written and the setAccessible is not necessary but this is a utility program over which the application has no control (a jython script).

What's ugly is that the internal implementation is showing through to the application.


Many people especially tool makers have this problem:








More information about the jigsaw-dev mailing list