Skip to content

Commit

Permalink
Use tricks to improve module visibility issues
Browse files Browse the repository at this point in the history
When open access to java.io is not granted to jnr-posix, display
a warning about the APIs that will be broken.

When open access is granted but jnr-posix is in the unnamed
module, explicitly open the packages to eliminate the warnings.

See https://github.com/jnr/jnr-posix/wiki/Using-jnr%E2%80%90posix-with-Java-Modules

Eliminates warnings from unnamed module open access requests on
releases prior to Java 17 and provides better information (with
link to the above wiki page) on Java 17+ for unnamed modules or
when open access is denied to the jnr-posix module.

See discussion in #190
  • Loading branch information
headius committed Jun 6, 2024
1 parent 73a004b commit 3a4e801
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-jnr%E2%80%90posix-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 3a4e801

Please sign in to comment.