RE: trySetAccessible
Uwe Schindler
uschindler at apache.org
Tue Jul 11 09:16:15 UTC 2017
Moin,
> > we understand all this. But what you say is also not true. I started a second
> approach to fix the issue by using canAccess() and also checking the module
> stuff. For that I executed the following code on the Groovy unnamed
> module:
> >
> > import org.codehaus.groovy.reflection.CachedClass;
> >
> String.getClass().getModule().isOpen(CachedClass.class.getPackage().getNam
> e(), CachedClass.class.getModule());
> >
> > This returned false, so the java.base module is not open to the package and
> code hosting Groovy's CachedClass! But setAccessible still works with a
> warning.
> This code fragment tests if java.base opens a package to
> CachedClass.class.getModule(). Can you say which package CachedClass is
> in? I ask because I would have expected to see:
>
> String.class.getModule().isOpen("java.lang",
> CachedClass.class.getModule());
>
> to test if java.base opens java.lang to the Groovy module.
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.
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
But now it is impossible for us to check, if something is not open by default. So, to come back to the previous discussion, it is now impossible to make everything accessible (as if illegal-access=deny). This makes migration to Java 10 hard for some projects, because you have to live with warnings that are printed by default, but you can't check if something is allowed without printing a warning.
That's not good. IMHO, this should be improved by:
- adding an API to do the checks, ignoring illegal-access settings
- make the runtime not open by default and instead just allow setAccessible on the java.* modules with printing a warning.
Uwe
More information about the jigsaw-dev
mailing list