Java:最简单的方法来打包Java 1.5和1.6代码 [英] Java: easiest way to package both Java 1.5 and 1.6 code

查看:174
本文介绍了Java:最简单的方法来打包Java 1.5和1.6代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想封装一段绝对必须在Java 1.5上运行的代码。有一部分代码,如果VM是1.6 VM,程序可以增强。

I want to package a piece of code that absolutely must run on Java 1.5. There's one part of the code where the program can be "enhanced" if the VM is an 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();
  }

on 1.5 and yet the 1.6 method calls when on 1.6

What would be easiest way to have this compile on 1.5 and yet do the 1.6 method calls when on 1.6 ?

在过去,我通过编译一个独特的1.6类来做类似的事情我将打包与我的应用程序和实例化使用ClassLoader时在1.6(因为1.6 JVM是完美地混合0x32和0x31类),但我认为这有点过分(和有点痛苦,因为在构建过程中,你必须构建0x31和0x32 .class文件)。

In the past I've done something similar by compiling a unique 1.6 class that I would package with my app and instantiate using a ClassLoader when on 1.6 (because an 1.6 JVM is perfectly fine mixing 0x32 and 0x31 classes), but I think it's a bit overkill (and a bit painful because during the build process you have to build both 0x31 and 0x32 .class files).

如果我想在1.5上编译上述方法,该怎么走?也许使用反射,但如何(我不熟悉反射)

How should I go if I wanted to compile the above method on 1.5? Maybe using reflection but then how (I'm not familiar at all with reflection)

注意:如果你好奇,上面的方法来自这篇文章: http://www.javaspecialists.eu/archive/Issue130.html

Note: if you're curious, the above method comes from this article: http://www.javaspecialists.eu/archive/Issue130.html

(但我不想像文章中那样注释三行,我想要编译并在1.5和1.6上运行)

(but I don't want to "comment the three lines" like in the article, I want this to compile and run on both 1.5 and 1.6)

推荐答案

你不能在1.5上编译,但是你可以在1.6上编译target-option设置为1.5(将产生1.5的字节码)

You cannot compile this on 1.5, but you can compile on 1.6 with the target-option set to 1.5 (that would produce byte-code for 1.5) and in the code using reflection to find out if the method is available.

此代码将查找方法:
mbean.getClass()。getMethod(findDeadlockedThreads ,new Class [0]);
问题是,如果方法不存在,它抛出一个NoSuchMethodException,而不是简单地返回null或类似的东西。这意味着你需要这样的代码:

This code would lookup the method: mbean.getClass().getMethod("findDeadlockedThreads", new Class[0]); Problem is, it throws an NoSuchMethodException if the method isn't present instead of simply returning null or something similar. That means, you need code like this:

try
{
  mbean.getClass().getMethod("findDeadlockedThreads", new Class<?>[0]);
  return mbean.findDeadlockedThreads();
}
catch(NoSuchMethodException ex)
{
  return mbean.findMonitorDeadlockedThreads();
}

这不是很好,因为它使用一个异常来做决定。这可能不是很快。另一种方法是改用getMethods,如果方法可用,则迭代返回的列表。这也不是很快。

That's not very nice, because it uses an Exception to make an decision. That is probably not very fast. An alternative is to use getMethods instead and iterate over the returned list if your method is available. That's also not very fast.

编辑: Christopher Oezbek建议在注释中检查方法的存在只有一次,并保存结果避免了Try-catch-block的开销。这是正确的,一个很好的解决方案。 matt b警告,如果所使用的类和方法在Java 1.5下可用,Java-Compiler的target-option不会检查。这是正确的(否则它不会工作,因为你想编译一个1.6方法),这意味着程序应该仔细测试在一个1.5-VM,以避免这个问题。感谢您的宝贵意见。

Christopher Oezbek suggests in the comments to make the check for the existence of the method only once and save the result to avoid the overhead for the Try-catch-block. That's right and a good solution. matt b warns, that the target-option of the Java-Compiler doesn't check, if the classes and methods used are available under Java 1.5. That's right (and otherwise it wouldn't work, because you want to compile against a 1.6-method) and that means, that the program should be carefully tested under a 1.5-VM, to avoid this problem. Thanks for your comments both of you.

这篇关于Java:最简单的方法来打包Java 1.5和1.6代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆