如何在 Java 中提供预处理器指令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20546659/
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-08-13 02:41:59  来源:igfitidea点击:

How to provide preprocessor directives in Java

javapreprocessor-directive

提问by Robert H

How can I correctly provide the following functionally from C# in Java?

如何从 Java 中的 C# 正确地提供以下功能?

[C#]

[C#]

#define PRODUCTION //Change from sandbox to production to switch between both systems.

#if SANDBOX
    using NetSuite.com.netsuite.sandbox.webservices;
#endif

#if PRODUCTION
    using NetSuite.com.netsuite.webservices;
#endif

采纳答案by Tim B

Java doesn't have a preprocessor - so the simple answer is that you can't.

Java 没有预处理器 - 所以简单的答案是你不能。

This sort of thing is normally handled in Java using Dependency Injection - which is both more powerful and more flexible.

这种事情通常在 Java 中使用依赖注入来处理——它更强大也更灵活。

http://www.vogella.com/articles/DependencyInjection/article.html

http://www.vogella.com/articles/DependencyInjection/article.html

回答by Dave Newton

Use Dependency Injection/Inversion of Control. Depending on your actual needs, you might be able to get away with something as simple as property file/environment variables to control things.

使用依赖注入/控制反转。根据您的实际需要,您可能可以使用属性文件/环境变量等简单的方法来控制事物。

You might be able to use static defines around sometypes of initialization/code.

您也许可以在某些类型的初始化/代码周围使用静态定义。

回答by Ingo

Java doesn't have a preprocessor, yet that doesn't mean that you can't run Java code through cpp- though it would not be supported by any tools, AFAIK.

Java 没有预处理器,但这并不意味着您不能运行 Java 代码cpp——尽管任何工具都不支持它,AFAIK。

回答by Scott

Check out Manifold'sJava preprocessor. It plugs directly into the compiler, so it's really simple to use -- no build steps or intermediate code gen etc.

查看Manifold 的Java 预处理器。它直接插入编译器,因此使用起来非常简单——没有构建步骤或中间代码生成等。

Manifold's Java Preprocessor

Manifold 的 Java 预处理器

回答by Oo.oO

You can use something based on <#FreeMarker>.

你可以使用基于 <#FreeMarker> 的东西。

enter image description here

在此处输入图片说明

source: https://github.com/mkowsiak/jpp

来源:https: //github.com/mkowsiak/jpp

However, this solution will require pre-compilation step, if you want to change the code. On the other hand, you can still create code that works without pre-processing steps - sort of "default" compilation.

但是,如果要更改代码,此解决方案将需要预编译步骤。另一方面,您仍然可以创建无需预处理步骤即可工作的代码 - 某种“默认”编译。