eclipse #if 在 java 中,就像在 c 预处理器中一样

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14208524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 19:46:06  来源:igfitidea点击:

#if in java, like in c preprocessors

javaandroideclipsec-preprocessor

提问by Charan Pai

Possible Duplicate:
How to mark java code such that it's not compiled

可能的重复:
如何标记 java 代码使其不被编译

In c, we can prevent compilation of block code like this :

c 中,我们可以防止像这样编译块代码:

#if 0

    //code here

#endif

So even if code block is error prone the code compiles, I want the same thing in Java, So that I can skip that part of code which won't compile because some library is missing.

因此,即使代码块在编译时容易出错,我也希望在 Java 中做同样的事情,这样我就可以跳过由于缺少某些库而无法编译的那部分代码。

Can anyone help me ?

谁能帮我 ?

回答by Ted Hopp

There is no preprocessor in Java. Depending on your build system, you may be able to use a third-party preprocessor (you can find lots of them by searching for "java preprocessor"). Some examples are

Java 中没有预处理器。根据您的构建系统,您可以使用第三方预处理器(您可以通过搜索“java 预处理器”找到很多)。一些例子是

Depending on the scope you want to comment out, you can use block comments and sometimes something like

根据您要注释掉的范围,您可以使用块注释,有时也可以使用类似

if (false) {
    . . .
}

If all else fails, just comment out every line using //. Most IDEs have a way to do this (and undo it) efficiently.

如果所有其他方法都失败了,只需使用//. 大多数 IDE 都有一种方法可以有效地执行此操作(并撤消它)。

P.S. If you can use block comments (not always possible, since block comments can't be nested in Java), there's a nice trick that makes it easier to toggle off and on the comment-out portion. Start your block comment on a line by itself, but end it with another isolated line that starts with a line comment, like this:

PS 如果您可以使用块注释(并非总是可行,因为块注释不能嵌套在 Java 中),有一个很好的技巧可以更轻松地关闭和打开注释部分。在一行上单独开始块注释,但以另一行以行注释开头的独立行结束,如下所示:

/*
   <block of code ignored as comment>
//*/

Then if you want to turn the commented-out section back on, just add a second /at the start of the block:

然后,如果您想重新打开注释掉的部分,只需/在块的开头添加第二个:

//*
   <block of code now active>
//*/

To toggle the code off again, just remove the first /. (Without the //at the start of the last line, the dangling */would be a syntax error whenever you activated the code by adding a /to the opening line of the block.)

要再次关闭代码,只需删除第一个/. (如果没有//在最后一行的开头,*/每当您通过将 a 添加/到块的开头行来激活代码时,悬空将是一个语法错误。)

回答by Habib

You have to comment out the code, you can't use pre-processor directive in java.

你必须注释掉代码,你不能在 java 中使用预处理器指令。

回答by Chuck D

I'm under the assumption that the compiler will strip the code inside blocks enforced with constant/final flags? This is how you can leave code in your project that isn't shipped in the final apk.

我的假设是编译器将剥离用常量/最终标志强制执行的块内的代码?这就是您可以在项目中保留未在最终 apk 中提供的代码的方式。

public final static boolean DEBUG = false;

if(DEBUG) {
    //add messy error prone code here
    //compiler will strip this out as unreachable when false
}

Read here:

在这里阅读:

http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.21

http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.21

回答by Chris Dodd

One technique I've used is to use m4as a preprocessor for java files. You write your code in classname.java.m4and use a Makefile or other build system rule to run m4:

我使用的一种技术是使用m4作为 java 文件的预处理器。您编写代码classname.java.m4并使用 Makefile 或其他构建系统规则来运行 m4:

%: %.m4
        @echo "/* automatically generated from $< -- don not edit*/" >$@
        m4 $< >>$@

回答by Bhavesh Patadiya

java Do not provide facility of pre-processor.

java 不提供预处理器的功能。

BTW you Can Use Comment Like as Below :

顺便说一句,您可以使用如下评论:

you're using Eclipse, you can simply mark the entire block of code you want to comment out then use CTRL+SHIFT+C for Commenting and uncommenting the Block of code.

您正在使用 Eclipse,您可以简单地标记要注释掉的整个代码块,然后使用 CTRL+SHIFT+C 对代码块进行注释和取消注释。

回答by paxdiablo

Java has no pre-processor along the lines of C.

Java 没有 C 语言的预处理器。

Since, according to the tags, you're using Eclipse, you can simply mark the entire block of code you want to comment out then use CTRL - /.

由于根据标签,您正在使用 Eclipse,您可以简单地标记要注释掉的整个代码块,然后使用CTRL - /.

You can also use that same key sequence to uncomment an already-commented-out block.

您还可以使用相同的键序列来取消注释已注释掉的块。