Reflection: how does one access a protected member in a superclass reflectively?
Alan Bateman
Alan.Bateman at oracle.com
Thu Jan 18 09:58:38 UTC 2018
On 17/01/2018 18:53, Rony G. Flatscher wrote:
>
> :
>
> Would you have concrete suggestions for this use-case, i.e. a
> framework that is not part of a module, but having a need to access
> public types from exported packages and get reflective access to
> objects supertype's protected members?
I think it would be better to start with public members as protected is
complicated (and hasn't changed with modules once you establish the
class declaring the member is accessible).
For your example, you've got a reference to a java.awt.Graphics2D
object, the actual implementation type is sun.java2d.SunGraphics2D. The
user is attempting to invoke one of the public setRenderingHint methods
that Graphics2D defines. You said in one of your mails that the bridge
"iterates over all its superclasses" which I take to mean that it
recursively looks at the superclass and interfaces to find a public
class or interface that defines the target setRenderingHint method. In
the example, I expect it would skip sun.java2d.SunGraphics2D if it were
non public.
Can you extend this check to test if the class is in a package exported
by its module. For the example, sun.java2d.SunGraphics2D is in the
java.desktop module and this module does not export sun.java2d to
everyone. Here is a code fragment to test this:
Class<?> clazz = graphicsObj.getClass();
boolean isExportedToAll =
clazz.getModule().isExported(clazz.getPackageName());
(I'm deliberately avoiding the 2-arg isExported to keep things simple
for this discussion).
If you can incorporate this check into the bridge then I suspect you'll
find most of the examples will work.
-Alan
More information about the jigsaw-dev
mailing list