Re: trySetAccessible
Russell Gold
russell.gold at oracle.com
Wed Aug 16 01:09:49 UTC 2017
I am clearly missing something, here. Using build 180, I have variables:
Method method, Class callingClass.
I evaluate:
callingClass.getModule() -
-> java.lang.Module at 2940 “unnamed module @f973499”
method.getDeclaringClass()
-> java.lang.Class “class java.io.ObjectInputStream”
method.getDeclaringClass().getModule()
-> java.lang.Module @2949 “module java.base"
method.getDeclaringClass().getModule().isOpen("java.io”)
-> false // which I read as saying that package “java.io” in module java.base is NOT open to the unnamed module
but:
method.getDeclaringClass().getModule().isOpen("java.io", callingClass.getModule())
-> true // which seems to say that it IS open to that particular unnamed module ?!
of course, method.setAccessible(true) gives me the warning.
What am I missing?
Thanks,
Russ
> On Jul 11, 2017, at 6:11 AM, Alan Bateman <Alan.Bateman at oracle.com> wrote:
>
> 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