JDK Dynalink only works with unconditionally exported packages

Thiago Henrique Hupner thihup at gmail.com
Mon May 10 12:13:55 UTC 2021


Hi all.

I've been testing the JDK Dynalink recently and
I think I've found a bug.

The class jdk.dynalink.beans.CheckRestrictedPackage checks
if a package is restricted.

So, I have a class that has some static methods. But to Dynalink find the
class,
the class needs to be public. OK,
I've changed my class to be public. However, it needs that
the package of the class is exported in the module too.

I think this is a little too restrictive. I don't want to expose
to my users some internal implementation.

The main problem is that the method
CheckRestrictedPackage::isRestrictedClass
does the following check:

// ...
final String pkgName = name.substring(0, i);
final Module module = clazz.getModule();
if (module != null && !module.isExported(pkgName)) {
    return true;
 }
// ...

I have a feeling that this check should be changed to something like

// ...
final String pkgName = name.substring(0, i);
final Module module = clazz.getModule();
if (module != null && !module.isOpen(pkgName,
CheckRestrictedPackage.class.getModule())) {
    return true;
 }
// ...

In this way, my code can only "opens" a single package to the jdk.dynalink
module,
not having to unconditionally export a package.

>From what I could understand, in Nashorn, the generated classes
were defined in the unnamed module, that always exports all the packages,
so the access always worked.

The code can be found here:
https://github.com/openjdk/jdk/blob/master/src/jdk.dynalink/share/classes/jdk/dynalink/beans/CheckRestrictedPackage.java#L94

Best regards.

Thiago


More information about the core-libs-dev mailing list