Update: I answered my own question here:
Scanning classpath/modulepath in runtime in Java 9
--
[Old question -- obsolete:]
What is the correct way to get a ModuleReference given only a Module object in Java 9?
Consider these two methods of referring to java.base:
Module mod = ModuleLayer.boot().findModule("java.base").orElse(null);
ModuleReference modRef = ModuleFinder.ofSystem().find("java.base").orElse(null);
mod has a method Set<String> getPackages(), but you only get the names of the packages, you cannot list the resources in each package.
modRef has a method ModuleReader open(), and ModuleReader has a method Stream<String> list() that lists the resources in the module, which is what I need to do.
However, for automatic (and therefore unnamed) modules, produced by adding non-module jarfiles to the classpath, you cannot get a ModuleReference from ModuleFinder.ofSystem().find(String name) or ModuleFinder.ofSystem().findAll() -- you can only get the Module reference from getClass().getModule().
I cannot find any way to get a ModuleReference for automatic modules. I also cannot find a way to get a ModuleReference from a Module object, which means I am not able to list the resources in a Module if the module is automatic and/or unnamed.
Surely there must be a way to get a ModuleReference for a given (already-loaded) Module?