Java条件编译:如何防止编译代码块? [英] Java conditional compilation: how to prevent code chunks from being compiled?

查看:231
本文介绍了Java条件编译:如何防止编译代码块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目需要Java 1.6进行编译和运行。现在我需要使用Java 1.5(来自营销方面)。我想替换方法体(返回类型和参数保持不变),以便使用Java 1.5进行编译而不会出错。

My project requires Java 1.6 for compilation and running. Now I have a requirement to make it working with Java 1.5 (from the marketing side). I want to replace method body (return type and arguments remain the same) to make it compiling with Java 1.5 without errors.

详细信息: I有一个名为 OS 的实用程序类,它封装了所有特定于操作系统的东西。它有一个方法

Details: I have an utility class called OS which encapsulates all OS-specific things. It has a method

public static void openFile(java.io.File file) throws java.io.IOException {
  // open the file using java.awt.Desktop
  ...
}

打开文件,例如双击(开始 Windows命令或打开等效的Mac OS X命令)。由于无法使用Java 1.5进行编译,我想在编译期间将其排除,并替换为另一种调用 run32dll for Windows或 open 对于Mac OS X使用 Runtime.exec

to open files like with double-click (start Windows command or open Mac OS X command equivalent). Since it cannot be compiled with Java 1.5, I want to exclude it during compilation and replace by another method which calls run32dll for Windows or open for Mac OS X using Runtime.exec.

问题:我该怎么办那?注释可以帮助吗?

注意:我使用ant,我可以制作两个java文件 OS4J5.java OS4J6.java 将包含 OS 类,其中包含Java 1.5和1.6的所需代码并复制其中一个是 OS.java 在编译之前(或者是一种丑陋的方式 - 有条件地替换 OS.java 的内容在java版本上)但我不想这样做,如果有另一种方式。

Note: I use ant, and I can make two java files OS4J5.java and OS4J6.java which will contain the OS class with the desired code for Java 1.5 and 1.6 and copy one of them to OS.java before compiling (or an ugly way - replace the content of OS.java conditionally depending on java version) but I don't want to do that, if there is another way.

更多地阐述:在CI中可以使用 ifdef, ifndef ,在Python中没有编译,我可以使用 hasattr 或其他东西检查一个功能,在Common Lisp中我可以使用#+特征。是否有类似的Java?

Elaborating more: in C I could use ifdef, ifndef, in Python there is no compilation and I could check a feature using hasattr or something else, in Common Lisp I could use #+feature. Is there something similar for Java?

找到这篇文章但它似乎没有帮助。

Found this post but it doesn't seem to be helpful.

非常感谢任何帮助。 kh。

Any help is greatly appreciated. kh.

推荐答案

在Java中没有任何对条件编译的支持。

Nope there isn't any support for conditional compilation in Java.

通常的计划是将应用程序的操作系统特定位隐藏在接口之后,然后在运行时检测操作系统类型并使用<$ c加载实现$ c> Class.forName(String)

The usual plan is to hide the OS specific bits of your app behind an Interface and then detect the OS type at runtime and load the implementation using Class.forName(String).

在你的情况下你没有理由不能编译这两个 OS * (以及你的整个应用程序)使用Java 1.6与 -source 1.5 -target 1.5 然后在工厂方法中获取保留 OS 类(现在是一个接口)检测到 java.awt.Desktop
类是可用并加载正确的版本。

In your case there no reason why you can't compile the both OS* (and infact your whole app) using Java 1.6 with -source 1.5 -target 1.5 then in a the factory method for getting hold of OS classes (which would now be an interface) detect that java.awt.Desktop class is available and load the correct version.

类似于:

 public interface OS {
     void openFile(java.io.File file) throws java.io.IOException;
 }

 public class OSFactory {
     public static OS create(){
         try{
             Class.forName("java.awt.Desktop");
             return new OSJ6();
         }catch(Exception e){
             //fall back
             return new OSJ5();
         }
     }
 }

这篇关于Java条件编译:如何防止编译代码块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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