Module.addOpens should log if the package has been opened for illegal access for the caller

Johannes Kuhn info at j-kuhn.de
Wed Dec 9 22:45:51 UTC 2020


Hope I got the right mailing list, otherwise please direct me there.

Module.addOpens currently doesn't produce an illegal access warning if 
the target package has only be opened for illegal access.

Reproducer:

     import java.lang.reflect.Method;
     import java.net.URL;
     import java.net.URLClassLoader;

     public class NoIllegalWarning {
         public static void main(String[] args) throws Throwable {
             Class<?> clazz = URLClassLoader.class;
             addOpens(clazz);
             // Use
             Method m = URLClassLoader.class.getDeclaredMethod("addURL", 
URL.class);
             m.setAccessible(true);
             // invoke it here.
             System.out.println("done");
         }

         public static void addOpens(Class<?> target) {
             target.getModule().addOpens(target.getPackageName(), 
NoIllegalWarning.class.getModule());
         }
     }

In the current maser branch, it has to be executed with 
--illegal-access=permit.
Other than that, the code above doesn't display any warnings.
Removing the "addOpens" call will display a warning.
No subsequent illegal access warning is produced as the package is 
reflectively open now.

Could someone create a bug on JBS? I could not find an existing one there.
I did prepare a patch to fix this: 
https://github.com/DasBrain/jdk/compare/addOpens-illegalAccess

This is the kind of code I don't like to see in production.

- Johannes



More information about the jigsaw-dev mailing list