从问题中清除的第一件事是句子中的推断是不正确的 –
for automatic (and therefore unnamed) modules,produced by adding non-module jar files to the classpath
Automatic modules是在模块路径上找到的命名模块.另一方面,the unnamed module是模块系统的成员,该系统支持从类路径加载类型,该类路径的包未在任何已知(命名)模块中定义.
Every class loader,it turns out,has its own unique unnamed module,
which is returned by the new 07004 method….
ClassLoader cl = getClass().getClassLoader();// returns the class loader for the class
Module yourClassLoaderUnnamedModule = cl.getUnnamedModule();
进一步移动文档增加了获取一个类型的模块加载形式未命名的模块::
.. type is considered to be in that loader’s unnamed module,i.e.,
the 07005 method of the type’s Class object will return its
loader’s unnamed module.
最终相当于:
Module yourClassUnnamedModule = getClass().getModule(); // from the type itself
主要是在访问未命名模块的资源方面,虽然我同意目前没有API可以访问未命名模块的资源等.但是,由于通过此模块加载的Class对象的类型总是来自类路径,因此可以要么获取正在使用的类路径的所有资源,要么相反,您可以检查是否从类路径或模块路径访问正在使用的资源.我能想到的一种方法是使用上述两个功能进行验证:
yourClassLoaderUnnamedModule.equals(yourClassUnnamedModule)//true if resource is loaded via classpath