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 {

    call();

  }

 

  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

    System.out.println(isConn.invoke(obj));

  }

}

 

 

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:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=482318 

https://netbeans.org/bugzilla/show_bug.cgi?id=258952 

https://netbeans.org/bugzilla/show_bug.cgi?id=262765 

http://mail.openjdk.java.net/pipermail/quality-discuss/2015-November/000468.html 

https://community.oracle.com/thread/3937249 

 

 


More information about the jigsaw-dev mailing list