- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
得到了一个仍然支持 Java 6 的项目。下面的代码位于一个 jar 文件中,该文件使用 Compiler compliance level 1.6
该 jar 文件应该从为 java 6 或更新版本构建的 java 应用程序中调用。它在 Java 8 中也运行良好。
现在使用 Java9,我遇到了 nio.DirectByteBuffer 的问题,我尝试使用反射以这种方式解决它:
@SuppressWarnings("unchecked")
static void cleanDirectBuffer(sun.nio.ch.DirectBuffer buffer) {
if (JAVA_VERSION < 1.9) {
sun.misc.Cleaner cleaner = buffer.cleaner();
if (cleaner != null) cleaner.clean();
} else {
// For java9 do it the reflection way
@SuppressWarnings("rawtypes")
Class B = buffer.getClass();
// will be a java.nio.DirectBuffer, which is unknown if compiled in 1.6 compliance mode
try {
java.lang.reflect.Method CleanerMethod = B.getMethod("cleaner");
CleanerMethod.setAccessible(true); // fails here !
Object cleaner = CleanerMethod.invoke(buffer);
if (cleaner == null) return;
@SuppressWarnings("rawtypes")
Class C = cleaner.getClass();
java.lang.reflect.Method CleanMethod = C.getMethod("clean");
CleanMethod.invoke(cleaner);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (Exception other) {
other.printStackTrace();
}
}
}
JAVA_VERSION 检测很好并且可以很好地切换,具体取决于我的调用代码使用的版本。jre6 到 jre8 环境很好地使用了 sun.misc.Cleaner 路径,但这在 java9 中不起作用
您可能会注意到我不是 java.reflection 方面的专家。通过猜测,我找到了.setAccessible(true);
lance-java 的回答(到目前为止感谢)有点帮助:
版本 2:
Class B = buffer.getClass();
try {
java.lang.reflect.Method CleanerMethod = B.getDeclaredMethod("cleaner");
CleanerMethod.setAccessible(true);
Object cleaner = CleanerMethod.invoke(buffer);
if (cleaner == null) return;
@SuppressWarnings("rawtypes")
Class C = cleaner.getClass();
java.lang.reflect.Method CleanMethod = C.getDeclaredMethod("clean");
CleanMethod.setAccessible(true); // Now it fails here !
CleanMethod.invoke(cleaner);
} catch (InaccessibleObjectException e) {
// ** causes: Unable to make public void jdk.internal.ref.Cleaner.clean() accessible: module java.base does not "exports jdk.internal.ref" to unnamed module **
}
另外还有第一个 CleanerMethod.setAccessible(true)
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by my.package.MyClass (file:/xyz.jar) to method java.nio.DirectByteBuffer.cleaner()
...
WARNING: All illegal access operations will be denied in a future release
...听起来不太健康?但是,唉,这只是一个警告:)
我还缺少什么,或者是否有不同/更好的方法来解决我的问题?
最佳答案
在 Java 9 中引入了模块的概念(https://blog.codefx.org/java/java-module-system-tutorial/)。
A module lists the packages it exports. For code in one module (say org.codefx.demo.jpms) to access types in another (say String in java.base), the following accessibility rules must be fulfilled:
the accessed type ( String) must be public
the package containing the type ( java.lang) must be exported by its module (java.base)
the accessing module (org.codefx.demo.jpms) must read the accessed one (java.base), which is typically achieved by requiring it
If any of these rules are violated at compile or run time, the module systems throws an error. This means that public is no longer really public. A public type in a non-exported package is as inaccessible to the outside world as a non-public type in an exported package. Also note that reflection lost its superpowers.
现在可以定义可在包外访问的类/方法。所有其他未指定的类/方法将无法被其他模块访问,即使它们是公共(public)的。这是 java.base
的 module-info
的链接 http://people.redhat.com/mbalaoal/webrevs/jdk_8029661_tls_12_sunpkcs11/2018_02_02/8029661.webrev.02/src/java.base/share/classes/module-info.java.html .如您所见,jdk.internal.ref
仅导出到这些模块:java.desktop
和 jdk.unsupported
关于java - 无法使用修饰符 "public"访问类 java.nio.DirectByteBuffer(在模块 java.base 中)的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47891295/
我花了相当多的时间尝试优化文件哈希算法,以尽可能地提高性能。 查看我之前的 SO 主题: Get File Hash Performance/Optimization FileChannel Byte
我不太明白它们之间有什么不同,所以我对这两个包有一些疑问。 在 Google 上浏览了一下之后,似乎 Oracle 决定使用更新和增强的 NIO.2 包来更新 NIO 包,作为 JDK7 版本的一部分
在 Java 1.4 之前,通过在不同的输入流/输出流之间移动字节来处理文件是常见的做法。 自 Java 1.4 起,其中 NIO已添加,建议使用 Channels 执行相同操作。 与 NIO2在 J
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我需要重写一些应该在 Java 6 VM 上运行的 Java 7 文件 IO 代码。 该实现使用了方便的 Java 7 功能,例如自动关闭、Paths 和 Files。 更具体地说,我需要处理像 /t
当我查看java中Scanner的源代码时,我发现一些让我困惑的事情 import java.nio.file.Path; import java.nio.*; 它们之间有什么区别,为什么它们不直接导
我的 Java 代码中几乎所有文件 I/O 操作都使用 java.nio.*。然而,在今天调试一段代码时,我注意到调试器 (Intellij IDEA 14) 显示了以下关于 java.nio.fil
奇怪的是,我无法在 Google 中找到 NIO.2 异步 IO 性能与通过 java.nio.channels.Selector 使用 NIO 的多路复用 IO 的明确答案。 所以,我的问题是:NI
我是初级 Java 程序员。 今天,我练习了如何在 java 中复制文件并尝试按照本教程进行操作 http://www.journaldev.com/861/4-ways-to-copy-file-i
我有一个指向绝对路径的 java.nio.Path: /home/user/project/resources/configuration.xml 我有第二个 java.nio.Path 指向项目的根
我开始使用java.nio.*,现在我想知道:为什么java.nio.Paths.get(..)不使用java.nio.Path 对象作为输入? 现在我总是做这样的事情: final Path bas
我是新手,正在学习 Java。我尝试在 Netbeans 7 中运行以下应用程序。 import java.io.*; import java.nio.file.*; import java.nio.
我的 Java 程序(见下文)有时会在 java.nio.File.move() 方法执行中因 java.nio.file.AccessDeniedException 崩溃。 我不明白为什么会抛出这个
所以我在这里阅读我最喜欢的软件模式书籍之一(面向模式的软件架构 - 并发和网络对象的模式),特别是关于 Proactor/Reactor 异步 IO 模式的部分。我可以看到通过使用可选 channel
我有一个方法如下,它已经正常运行了很长时间: private String loadFromFile(){ RandomAccessFile inFile = null; FileCh
我在 IntellijIDEA Community Edition 2017.3 版本中收到以下错误。该项目使用java版本“1.8.0-ea”。请给我一些解决问题的想法 Error:Internal
一 点睛 在 scatter-and-gather 场景下,可以将数据写入多个 Buffer 中。在 NIO 中,也能够同时操作多个缓冲区。在很多 Channel 实现类中,都提供了多个重载的 rea
I/O简介 在 Java 编程中,直到最近一直使用 流 的方式完成 I/O。所有 I/O 都被视为单个的字节的移动,通过一个称为 Stream 的对象一次移动一个字节。流 I/O 用于与外部世界接
一 点睛 给某一个文件加锁,防止并发访问时引起的数据不安全。 在 JUC 中,可以使用 synchronized、Lock 给共享的资源加锁,或者使用 volatile、CAS 算法等防止并发冲突。在
一 点睛 给某一个文件加锁,防止并发访问时引起的数据不安全。 在 JUC 中,可以使用 synchronized、Lock 给共享的资源加锁,或者使用 volatile、CAS 算法等防止并发冲突。在
我是一名优秀的程序员,十分优秀!