本文整理汇总了Java中java.lang.module.ModuleReference.open方法的典型用法代码示例。如果您正苦于以下问题:Java ModuleReference.open方法的具体用法?Java ModuleReference.open怎么用?Java ModuleReference.open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.module.ModuleReference
的用法示例。
在下文中一共展示了ModuleReference.open方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: isAutomaticModuleNameAttributeAvailable
import java.lang.module.ModuleReference; //导入方法依赖的package包/类
boolean isAutomaticModuleNameAttributeAvailable(ModuleReference moduleReference) {
try (var moduleReader = moduleReference.open()) {
var manifestString =
moduleReader
.read("META-INF/MANIFEST.MF")
.map(StandardCharsets.UTF_8::decode)
.map(Object::toString)
.orElse("");
if (manifestString.contains("Automatic-Module-Name")) {
return true;
}
} catch (Exception e) {
debug("reading manifest failed: {0}", e);
}
return false;
}
示例2: moduleReaderFor
import java.lang.module.ModuleReference; //导入方法依赖的package包/类
/**
* Returns the ModuleReader for the given module, creating it if needed
*/
private ModuleReader moduleReaderFor(ModuleReference mref) {
ModuleReader reader = moduleToReader.get(mref);
if (reader == null) {
// avoid method reference during startup
Function<ModuleReference, ModuleReader> create = new Function<>() {
public ModuleReader apply(ModuleReference moduleReference) {
try {
return mref.open();
} catch (IOException e) {
// Return a null module reader to avoid a future class
// load attempting to open the module again.
return new NullModuleReader();
}
}
};
reader = moduleToReader.computeIfAbsent(mref, create);
}
return reader;
}
示例3: main
import java.lang.module.ModuleReference; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
String mn = args[0];
ModuleReference mref = ModuleLayer.boot()
.configuration()
.findModule(mn)
.map(ResolvedModule::reference)
.orElseThrow(() -> new RuntimeException(mn + " not resolved!!"));
try (ModuleReader reader = mref.open()) {
reader.list().forEach(name -> {
testFindUnchecked(name);
// if the resource is a directory then find without trailing slash
if (name.endsWith("/")) {
testFindUnchecked(name.substring(0, name.length() - 1));
}
});
}
}
示例4: createModuleReader
import java.lang.module.ModuleReference; //导入方法依赖的package包/类
/**
* Creates a ModuleReader for the given module.
*/
private ModuleReader createModuleReader(ModuleReference mref) {
try {
return mref.open();
} catch (IOException e) {
// Return a null module reader to avoid a future class load
// attempting to open the module again.
return new NullModuleReader();
}
}
示例5: listAllModules
import java.lang.module.ModuleReference; //导入方法依赖的package包/类
/**
* List all modules that the finder finds and the resources in the module.
*/
private void listAllModules(ModuleFinder finder) throws Exception {
for (ModuleReference mref : finder.findAll()) {
System.out.println(mref.descriptor());
try (ModuleReader reader = mref.open()) {
reader.list().forEach(name -> System.out.format(" %s%n", name));
}
}
}
示例6: read
import java.lang.module.ModuleReference; //导入方法依赖的package包/类
public static ModuleTarget read(ModuleReference modRef) throws IOException {
ModuleReader reader = modRef.open();
try (InputStream in = reader.open("module-info.class").get()) {
return read(in);
} finally {
reader.close();
}
}
示例7: testModuleReader
import java.lang.module.ModuleReference; //导入方法依赖的package包/类
/**
* Exercise ModuleReader on a multi-release JAR
*/
public void testModuleReader() throws Exception {
String name = "m1";
ModuleDescriptor descriptor1 = ModuleDescriptor.newModule(name)
.requires("java.base")
.build();
// module descriptor for versioned section
ModuleDescriptor descriptor2 = ModuleDescriptor.newModule(name)
.requires("java.base")
.requires("jdk.unsupported")
.build();
Path jar = new JarBuilder(name)
.moduleInfo(MODULE_INFO, descriptor1)
.moduleInfo("META-INF/versions/" + VERSION + "/" + MODULE_INFO, descriptor2)
.build();
// find the module
ModuleFinder finder = ModuleFinder.of(jar);
Optional<ModuleReference> omref = finder.find(name);
assertTrue((omref.isPresent()));
ModuleReference mref = omref.get();
ModuleDescriptor expected;
if (MULTI_RELEASE) {
expected = descriptor2;
} else {
expected = descriptor1;
}
// test ModuleReader by reading module-info.class resource
try (ModuleReader reader = mref.open()) {
// open resource
Optional<InputStream> oin = reader.open(MODULE_INFO);
assertTrue(oin.isPresent());
try (InputStream in = oin.get()) {
checkRequires(ModuleDescriptor.read(in), expected);
}
// read resource
Optional<ByteBuffer> obb = reader.read(MODULE_INFO);
assertTrue(obb.isPresent());
ByteBuffer bb = obb.get();
try {
checkRequires(ModuleDescriptor.read(bb), expected);
} finally {
reader.release(bb);
}
// find resource
Optional<URI> ouri = reader.find(MODULE_INFO);
assertTrue(ouri.isPresent());
URI uri = ouri.get();
String expectedTail = "!/";
if (MULTI_RELEASE)
expectedTail += "META-INF/versions/" + VERSION + "/";
expectedTail += MODULE_INFO;
assertTrue(uri.toString().endsWith(expectedTail));
URLConnection uc = uri.toURL().openConnection();
uc.setUseCaches(false);
try (InputStream in = uc.getInputStream()) {
checkRequires(ModuleDescriptor.read(in), expected);
}
}
}