p example (Re: Reflection: how can one access public fields (members) in a superclass ?
Rony G. Flatscher
Rony.Flatscher at wu.ac.at
Wed Jan 24 15:42:11 UTC 2018
Changed the subject to hint at the "p" package example.
On 24.01.2018 15:28, Alan Bateman wrote:
> On 23/01/2018 14:52, Rony G. Flatscher wrote:
... cut ...
> I don't think the questions and observations in this thread are strictly modules related. One
> suggestion is to start with a simpler scenario like this:
>
> package p;
> class C1 {
> public static final int K = 99;
> public static int k() { return K; }
> public final int F = -1;
> public int m() { return F; }
> }
>
> package p;
> public class C2 extends C1 { }
>
> No modules or qualified exports in the picture for now. The important part is that C1 is not
> public but it has public members. You can try tests to see if references to C2.K, C2.k(), new
> C2().F, and new C2().m() will compile and run. You can try the equivalent with core reflection to
> see how it differs to static references (you may have to change method m to be final to prevent
> javac generating a bridge method in C2).
OK, now add to this the following class that uses p.C2 objects to access e.g. m() via it:
G:\xfer\java9modules\03-20180124-AlanBatmanP\p>type UseC2.java
public class UseC2
{
public static void main (String args[]) {
p.C2 o=new p.C2();
System.out.println("o="+o);
System.out.println("o.m()="+o.m());
}
}
Compiling all three classes works.
Running "UseC2" works and outputs:
G:\xfer\java9modules\03-20180124-AlanBatmanP\p>java -cp ".;.." UseC2
o=p.C2 at 66048bfd
o.m()=-1
So it is possible to access m() via the p.C2 object from UseC2.
---
The modules seem to come into play when reflection or unreflect try to determine whether
accessibility should be granted or not. The current implementations (mistakingly, I think) assume
that access is only to be grantable if the package of the object to reflect is exported to the
reflector.
Rather, it should check whether the reflected member is in a class with a package that gets exported
to the reflector *or* is a superclass of a class which package got exported to the reflector. Or
with other words, once a class is determined that gets exported to the reflector all public members
in all superclasses should be accessible in order to avoid "crippled Java objects CJO" ;) .
java.lang.reflect.* could do that check. In the case of unreflection probably one should be able to
supply an exported class to check accessibility and then only accept object that are instances of at
least that class.
---rony
More information about the jigsaw-dev
mailing list