Reflection: how does one access a protected member in a superclass reflectively?
Alan Bateman
Alan.Bateman at oracle.com
Tue Jan 16 09:18:20 UTC 2018
On 15/01/2018 20:56, Rony G. Flatscher wrote:
> :
>
>
> So the question is, how can I reflectively access "mtest1.Class01A" static protected field
> "myClassName" from "mtest3.Class03a" in Java 9?
>
The scenario in your mail is complicated but if I read it correctly then
I would expect this code in mtest3.Class03A (mod_C) to work:
Class.forName("mtest1.Class01A").getDeclaredField("myClassName").get(null);
I can't tell if the code in the unnamed module extends mtest1.Class01A
or not. If it does then the same code should work, if it's not a
sub-class then you should see IllegalAccessException being thrown.
Your traces show InaccessibleObjectException being thrown so I think you
are attempting to call setAccessible(true) to suppress the access check.
You can't do on that protected members when the package is not open for
deep reflection. In the example, mod_A would need to `opens mtest1` to
allow this.
-Alan.
More information about the jigsaw-dev
mailing list