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

查看:32
本文介绍了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.

详细信息:我有一个名为 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
  ...
}

像双击一样打开文件(start Windows 命令或 open Mac OS X 命令等效).由于它不能用 Java 1.5 编译,我想在编译过程中排除它并用另一种方法替换它,该方法为 Windows 调用 run32dll 或使用 为 Mac OS X 调用 open运行时.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.javaOS4J6.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 中我可以使用#+feature.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.

非常感谢任何帮助.嗯.

Any help is greatly appreciated. kh.

推荐答案

不,Java 不支持条件编译.

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

通常的计划是将应用的操作系统特定位隐藏在 Interface 后面,然后在运行时检测操作系统类型并使用 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).

在您的情况下,您没有理由不能使用带有 -source 1.5 -target 1.5 的 Java 1.6 编译 OS*(实际上是整个应用程序)然后在获取 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天全站免登陆