条件Java编译 [英] Conditional Java compilation

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

问题描述

我是一个长期的C ++程序员,是Java的新手。我正在Eclipse中开发一个Java Blackberry项目。问题 - 有没有办法在项目中引入不同的配置集,然后根据这些编译稍微不同的代码?



在Visual Studio中,我们有项目配置和#ifdef ;我知道Java中没有#ifdef,但也可能是文件级别的一些?

解决方案

您可以设置'final'如果要编译器来优化编译的字节码。

  ... 
public static final boolean myFinalVar =假;
...
if(myFinalVar){
do something ....
....
}

如果代码编译时'myFinalVar'为false,那么做某事....将被错过编译的类。如果你有不止一个条件 - 可以把它们整理一下:把它们全部转移到另一个类(比如'Config.myFinalVar'),然后这些条件都可以保存在一个整齐的位置。



这种机制在'Hardcore 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?

解决方案

You can set up 'final' fields and ifs to get the compiler to optimize the compiled byte-codes.

...
public static final boolean myFinalVar=false;
...
if (myFinalVar) { 
 do something ....
 ....
}

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

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

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