ad 'module' modifier idea (Re: p example (Re: Reflection: how can one access public fields (members) in a superclass ?

Rony G. Flatscher Rony.Flatscher at wu.ac.at
Wed Mar 14 14:20:40 UTC 2018


Just changed the subject to indicate better its content.

On 13.03.2018 14:17, Remi Forax wrote:
... cut ...
> ---
>
> The only grief I have at the moment is with the meaning of "public": "public"
> has become a homonym
> in jigsaw, introducing a *lot* of confusion into Java (one time "public" is
> public, another time
> "public" is not public).
>
> This could be probably eased considerably IMHO, if there was a class Modifier
> introduced named
> "module" that would be set for reflective access to classes from non exported
> modules.
> It already exists, but not at class level, 
yes, something like (e.g. from
<http://mail.openjdk.java.net/pipermail/jigsaw-dev/2016-September/009357.html>):

    if (Modifier.isPublic(clazz.getModifiers()) && clazz.getModule().isExported(clazz.getPackageName(), caller.getModule()) ...


> you can open a module, or each package individually, in that case, setAccessible is allowed.
yes, however this adds the need to learn the new concept "open" (which is fine for advanced Java
programmers)

>> This would have at least the following benefits:
>>
>> - communicating (and teaching!) about the publicness of a class can immediately
>> be clarified ("is
>> the class truly public or does it reside in a non-exported module?"),
> Teaching is not a big deal because you can teach package and module at the same time and say that it works like onions or russian dolls,
nice metapher!
> you have module access, class access and member access, by example, a public field is not really public if the class is not public and the package not exported.
> If you take a look from the point of view of accessing a member (and not a class), things are regular at module level and class level.  
in my case, if thinking of teaching it is thinking of  "beginner"/"end-user"/"casual" programmer
type of people (business administration students who are interested in learning programming, however
not much time available in the curriculum to teach it)

>
>> - also, programmatically it would be simple to learn whether a class object
>> fetched reflectively is
>> truly public or not by testing against the presence of such a "Module" modifier
>> bit at runtime in
>> Java 9 or higher.
> you can use Lookup.accessClass() [1].
yes, but this a quite advanced concept, it takes quite some time before arriving and being able to
take advantage of it

> [1] https://docs.oracle.com/javase/9/docs/api/java/lang/invoke/MethodHandles.Lookup.html#accessClass-java.lang.Class-
>
---

There are at least two aspects in the context of the "public" homonym:

  * one is the aspect of conceptually understanding it ("why are 'public' classes sometimes not
    'public'?"), and

  * one is to be able to personally test it (maximum learning effect) with minimal Java skills,
    which is why I would prefer the info that a class is from a non-exported module be given with a 
    hypothetical "module" identfier with the class object (if absent, the class is indeed public to
    the world).

    This way, if a student (or any - expert or casual - Java programmer) uses
    "o.getClass().toString()" would see e.g. "module class xyz.SomeClass" and immediately know that
    this class is from a non-exported module. This would simplify this issue considerably IMHO.

---rony



More information about the jigsaw-dev mailing list