Re: trySetAccessible​

Alan Bateman Alan.Bateman at oracle.com
Tue Jul 11 10:11:42 UTC 2017


On 11/07/2017 10:16, Uwe Schindler wrote:
> :
> Sorry, I mixed up the parameters. So basically the "correct" code to check if something like java.lang.String is open to Groovy would be:
>
> Module groovyModule = CachedClass.class.getModule(); // org.codehaus.groovy.reflection.CachedClass;
> Class clazz = String.class; // to test if open
> return clazz.getModule().isOpen(String.class.getPackage().getName(), groovyModule);
>
> If I do this, it returns "true" for String, Object or any other class. So the behaviour is consistent.
Yes, it has to be consistent.

As an aside, you can replace getPackage().getName() with 
getPackageName() - it's more efficient and avoids the NPE when the class 
is an array or primitive.


>
> I implemented this check as a single MethodHandle to a precompiled method that takes a Class<?> and returns true/false if the Class is open to Groovy's module: https://goo.gl/XvdCQK
> By this it's possible to execute this without a Java 9 at compile time. Unfortunately, it doesn't help, because java.base is open to unnamed module
Right, although isOpen("java.lang") will return false because the 
package is not open to all modules.


>
> But now it is impossible for us to check, if something is not open by default.
Module::getDescriptor will return the module descriptor for named 
modules. So in this case, Object.class.getModule().getDescriptor() 
returns the ModuleDescriptor for java.base so you can inspect the 
packages and which are exported and/or opened before being changed by 
runtime. So there is enough in the API to determine which packages have 
been opened at runtime (either via CLI options or APIs).

-Alan


More information about the jigsaw-dev mailing list