java打包出来的代码不一样,Java:最简单的方法来打包Java 1.5和1.6代码

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.

Basically it's this method:

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();

}

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

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).

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)

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

(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)

解决方案

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.

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();

}

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.

EDIT: 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值