Skip to content

Commit

Permalink
Merge pull request #192 from jnr/better_module_handling
Browse files Browse the repository at this point in the history
Use tricks to improve module visibility issues
  • Loading branch information
headius committed Jun 6, 2024
2 parents 73a004b + 7e6ac00 commit 50b3be0
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/main/java/jnr/posix/JavaLibCHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,27 @@ private static class ReflectiveAccess {
private static final Field FILE_DESCRIPTOR_HANDLE;

static {
try {
Method getModule = Class.class.getMethod("getModule");
Class<?> Module = Class.forName("java.lang.Module");
Method isOpen = Module.getMethod("isOpen", String.class, Module);
Method isNamed = Module.getMethod("isNamed");
Method addOpens = Module.getMethod("addOpens", String.class, Module);
Object JNRPosixModule = getModule.invoke(ReflectiveAccess.class);
Object JavaBaseModule = getModule.invoke(FileDescriptor.class);

if (!((Boolean) isOpen.invoke(JavaBaseModule, "java.io", JNRPosixModule))) {
// warn that many APIs will be broken without module access
System.err.println("Some JDK modules may not be open to jnr-posix, which will break file descriptor and process APIs. See https://github.com/jnr/jnr-posix/wiki/Using-POSIX-with-Java-Modules");
} else if (!((Boolean) isNamed.invoke(JNRPosixModule))) {
// explicitly open them to avoid the implicitly open warning
addOpens.invoke(JavaBaseModule, "java.io", JNRPosixModule);
addOpens.invoke(JavaBaseModule, "sun.nio.ch", JNRPosixModule);
}
} catch (Exception e) {
// ignore, we're not on Java 9+
}

Method getFD;
Class selChImpl;
try {
Expand Down

0 comments on commit 50b3be0

Please sign in to comment.