如何从ModuleReference获取Module对象 [英] How to get a Module object from a ModuleReference
问题描述
使用代码:
ModuleFinder.of(Paths.get(path)).findAll()
我能够检索所有的 Set< ModuleReference>
路径
文件夹中的 .jars 。
I am able to retrieve a Set<ModuleReference>
of all the .jars in the path
folder.
我的下一步是从模块 .net / java / jdk9 / docs / api / java / lang / module / ModuleReference.htmlrel =nofollow noreferrer> ModuleReference
但是没有方法返回,我可以得到一个 ModuleDescriptor
但即使是那个也无济于事。有没有办法做到这一点?
My next step would be getting a Module
from a ModuleReference
but there's no method that returns that, I can get a ModuleDescriptor
but even that one doesn't help. Is there a way to do this?
推荐答案
如果你想访问模块内容你应该 打开
您获得的 ModuleReference
。
这将使您可以访问 ModuleReader
If you desire to access the module content you should open
the ModuleReference
that you've attained.
This would provide you access to the ModuleReader
which
适用于访问模块中的资源是
必需
is intended for cases where access to the resources in a module is required
模块中的资源由
'的抽象名称标识/'
- 分隔的路径字符串。例如,模块 java.base
可能有资源java / lang / Object.class
按照惯例,
是 java.lang.Object
的类文件。模块阅读器可以将模块内容中的
目录视为资源(无论它是否为
模块阅读器特定)。如果模块内容包含可以作为资源定位的目录
,则其名称以斜杠结尾('/'
)。该目录也可以使用一个名称来删除尾部斜杠。
A resource in a module is identified by an abstract name that is a
'/'
-separated path string. For example, module java.base
may have a resource "java/lang/Object.class"
that, by convention,
is the class file for java.lang.Object
. A module reader may treat
directories in the module content as resources (whether it does or not is
module reader specific). Where the module content contains a directory
that can be located as a resource then its name ends with a slash ('/'
). The directory can also be located with a name that drops the trailing slash.
请记住,文档还指定:
Do keep in mind though, that the docs also specify :
ModuleReader在创建时打开,并通过调用
关闭
方法。未能关闭模块阅读器可能会导致资源
泄漏。try-with-resources
语句为
提供了一个有用的结构,确保关闭模块阅读器。
A ModuleReader is open upon creation and is closed by invoking the
close
method. Failure to close a module reader may result in a resource leak. Thetry-with-resources
statement provides a useful construct to ensure that module readers are closed.
从资源获取模块
的一种方法是使用 Class#getModule
as :
One way to get the Module
from the resources would be to access it using the Class#getModule
as:
Module module = com.foo.bar.YourClass.class.getModule();
编辑 :我已经及时了解了使用 ModuleFinder
访问模块
的更好方法 @Alan 建议也可能是:
Edit: I've learned with time a better way to use the ModuleFinder
to access a Module
as suggested by @Alan as well could possibly be :
ModuleFinder finder = ModuleFinder.of(path);
ModuleLayer parent = ModuleLayer.boot();
Configuration configuration = parent.configuration().resolve(finder, ModuleFinder.of(), Set.of("curious")); // 'curious' being the name of the module
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
ModuleLayer layer = parent.defineModulesWithOneLoader(configuration, systemClassLoader);
Module m = layer.findModule("curious").orElse(null);
这篇关于如何从ModuleReference获取Module对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!