条件 Java 编译
我是一名资深 C++ 程序员,刚接触 Java。我正在 Eclipse 中开发 Java Blackberry 项目。问题 - 有没有办法在项目中引入不同的配置集,然后根据这些配置集编译略有不同的代码?
在Visual Studio中,我们有项目配置和#ifdef;我知道 Java 中没有 #ifdef,但也许是文件级别的东西?
I'm a longtime C++ programmer, new to Java. I'm developing a Java Blackberry project in Eclipse. Question - is there a way to introduce different configuration sets within the project and then compile slightly different code based on those?
In Visual Studio, we have project configurations and #ifdef; I know there's no #ifdef in Java, but maybe something on file level?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您可以设置“final”字段和 ifs 以使编译器优化编译后的字节码。
如果在编译代码时“myFinalVar”为 false,则编译后的类中将会丢失“do some....”位。如果您有多个条件 - 可以稍微整理一下:将它们全部转移到另一个类(例如“Config.myFinalVar”),然后所有条件都可以保存在一个整齐的地方。
此机制在'Hardcore Java'。
[实际上我认为这与之前发布的“穷人的 ifdef”机制相同。]
You can set up 'final' fields and ifs to get the compiler to optimize the compiled byte-codes.
If 'myFinalVar' is false when the code is compiled the 'do something....' bit will be missed out of the compiled class. If you have more than one condition - this can be tidied up a bit: shift them all to another class (say 'Config.myFinalVar') and then the conditions can all be kept in one neat place.
This mechanism is described in 'Hardcore Java'.
[Actually I think this is the same mechanism as the "poor man's ifdef" posted earlier.]
您可以管理不同的类路径,例如,在一组不同的目录中实现每个“操作”:
然后为每个版本使用不同的类路径
或
you can manage different classpath, for example, implement each 'Action' in a set of distinct directories:
then use a different classpath for each version
or
在JDK6中,可以使用Java的ServiceLoader接口来完成。
请在此处查看。
In JDK6, you can do it by using Java's ServiceLoader interface.
Check it here.
如果您想要专门用于 BlackBerry,BlackBerry JDE 有 预处理器:
然后您可以在代码中执行以下操作:
有关详细信息,请参阅 指定预处理器定义
If you want this specifically for BlackBerry, the BlackBerry JDE has a pre-processor:
Then you can do things in the code like:
For details see Specifying preprocessor defines
可以说这是一个穷人
ifdef
:http: //www.javapractices.com/topic/TopicAction.do?Id=64?Can one call that a poor mans
ifdef
: http://www.javapractices.com/topic/TopicAction.do?Id=64?不,Java 没有与该功能完全匹配的功能。您可以使用方面,或使用 IOC 容器来注入不同的实现类。
No, Java doesn't have an exact match for that functionality. You could use aspects, or use an IOC container to inject different implementation classes.
您可以将 m4 集成到您的构建过程中,以有效地捆绑类似于Java编译器前面的C预处理器。很多麻烦在于“集成”步骤,但 m4 是适合文本处理工作的技术。
You can integrate m4 into your build process to effectively strap an analogue to the C preprocessor in front of the Java compiler. Much hand-waving lies in the "integrate" step, but m4 is the right technology for the text processing job.
除了 Maven、Ant 和其他提供类似功能的构建工具之外,人们宁愿用 Java 构建接口并在运行时切换实现。
有关更多详细信息,请参阅策略模式
与 C/C++ 相反,这不会带来很大的影响性能损失,因为 Java 的 JIT 编译器在运行时进行优化,并且在大多数情况下能够内联此模式。
这种模式的一大优点是灵活性——您可以更改底层实现而无需触及核心类。
您还应该检查 IoC 和 观察者模式了解更多详细信息。
Besides Maven, Ant and other build tools that provide similar functionality, one would rather build interfaces in Java and switch the implementations at Runtime.
See the Strategy Pattern for more details
In opposite to C/C++ this will not come with a big performance penality, as Javas JIT-compiler optimizes at runtime and is able to inline this patterns in most cases.
The big pro of this pattern is the flexibility - you can change the underlying Implementation without touching the core classes.
You should also check IoC and the Observer Pattern for more details.
您可以将 Maven 的资源过滤与公共静态最终字段结合使用,这确实会被有条件地编译。
现在你需要向你的maven pom添加一个名为“mode”的属性,它应该是
与您的 ANDROID 常量具有相同的值。
java编译器应该(!)删除if和else块,从而留下你的android代码。
不是测试集,所以没有保证,我更喜欢配置而不是条件编译。
You could use maven's resource filtering in combination mit public static final fields, which will be indeed get compiled conditionally.
Now you need to add a property to your maven pom called "mode", which should be
of the same value as your ANDROID constant.
The java compiler should (!) remove the if and the else block, thus leaving your android code.
Not testet, so there is no guarantee and i would prefer configuration instead of conditional compilation.
有几个项目为 Java 带来了对基于注释的条件编译的支持:
JPSG 中的示例:
There are a couple of projects that bring support for comment-based conditional compilation to Java:
Example in JPSG:
在 Eclipse 中,您可以使用多个项目
Version2(包含 version2 代码)
版本 1 和版本 2 包含相同的文件,但实现不同。在 Main 中,您通常会编写例如
,如果您包含 Version1/Version2 项目作为参考,它将使用 Version1/Version2 项目中的 Version.java 文件进行编译。
In eclipse you could use multiple projects
Version2 (contains version2 code)
Version1 and Version two contain the same files but different implementations. In Main you normally write e.g.
And if you included Version1/Version2 project as reference it will compile with the Version.java file from Version1/Version2 project.