Why does calling `Blah.class.getModule()` give me the Unnamed Module instead of the BlahModule?

12 hours ago 1
ARTICLE AD BOX

In short, I have a BlahModule which I made a module-info.java for, and one of my classes within that module is called Blah. So, when another class within that same module calls Blah.class.getModule(), I am expecting the BlahModule, but instead, I get the Unnamed Module.

Here is what I have done research-wise.

Looking at the documentation for Class.getModule(), it says this about modules.

If this class is in an unnamed module then the unnamed Module of the class loader for this class is returned.

Ok, when clicking the link to read about Unnamed Modules, it brings me to the Java Language Specification on Unnamed Modules (7.7.5), which says the following.

An observable ordinary compilation unit that the host system does not associate with a named module (§7.3) is associated with an unnamed module.

Ok, it appears that the association must be explicit. Following the link to 7.3 brings me to the following.

Each observable compilation unit may be associated with a module, as follows:

The host system may determine that an observable ordinary compilation unit is associated with a module chosen by the host system, except for (i) the ordinary compilation units in the predefined package java and its subpackages lang and io, which are all associated with the java.base module, and (ii) any ordinary compilation unit in an unnamed package, which is associated with a module as specified in §7.4.2.

The host system must determine that an observable modular compilation unit is associated with the module declared by the modular compilation unit.

Ok, those are the rules that permit or deny associating a module with a compilation unit, but nothing about how to associate a module with a compilation unit.

Ok, browsing around a bit, I landed on 7.2, which says the following.

The host system is free to determine that a compilation unit which contains a module declaration is not, in fact, observable, and thus is not associated with the module declared therein. This enables a compiler to choose which directory on a modulesourcepath is "really" the embodiment of a given module. However, if the host system determines that a compilation unit which contains a module declaration is observable, then §7.4.3 mandates that the compilation unit must be associated with the module declared therein, and not with any other module.

Ok, so the act of association for modules with compilation units is STRICTLY determined by the observability of a module declaration to the compilation unit.

Ok, what does it mean to be observable?

Well, 7.3 (which I looked at part of above) lists some stuff, but nothing that explicitly explains HOW to know when something is or is not observable. It sounds like the host system can determine that, but how is not clear to me.

So this is more or less where I am lost. I don't really see where else to look from here. I could probably do a Ctrl+F search of the whole JLS, but I figured I would make this post before doing so.


And to reiterate, all of this research is merely to answer the question -- what is required in order for Blah.class.getModule() to return my BlahModule instead of the Unnamed Module?

Read Entire Article