【发布时间】:2010-03-15 16:11:56
【问题描述】:
我想打包一段绝对必须在 Java 1.5 上运行的代码。如果 VM 是 1.6 VM,则可以在代码的一部分中“增强”程序。
基本上就是这个方法:
private long[] findDeadlockedThreads() {
// JDK 1.5 only supports the findMonitorDeadlockedThreads()
// method, so you need to comment out the following three lines
if (mbean.isSynchronizerUsageSupported())
return mbean.findDeadlockedThreads();
else
return mbean.findMonitorDeadlockedThreads();
}
在 1.5 上编译而在 1.6 上执行 1.6 方法调用的最简单方法是什么?
在过去,我通过编译一个独特的 1.6 类来完成类似的操作,我将在 1.6 上使用 ClassLoader 将它与我的应用程序打包并实例化(因为 1.6 JVM 非常适合混合 0x32 和 0x31 类),但我认为这有点矫枉过正(而且有点痛苦,因为在构建过程中您必须同时构建 0x31 和 0x32 .class 文件)。
如果我想在1.5上编译上面的方法应该怎么做?也许使用反射,然后如何(我对反射一点也不熟悉)
注意:如果你好奇,上面的方法来自这篇文章:http://www.javaspecialists.eu/archive/Issue130.html
(但我不想像文章中那样“评论三行”,我希望它能够在 1.5 和 1.6 上编译和运行)
【问题讨论】: