Intellij IDEA 中的 Java 预处理支持
有谁知道 Intellij IDEA 中的 Java 预处理支持吗?可能有一些第三方插件?
举个例子:下面是一段为 Netbeans 编写的 Java 代码(支持预处理):
//#if JSR82
//# import javax.bluetooth.*;
//#endif
Netbeans 内置预处理器会解析这些 //# 键,并根据定义的预处理器键对代码片段进行注释或取消注释。
我希望 Intellij IDEA 中有类似的东西。
提前感谢
Does anyone know about Java preprocessing support in Intellij IDEA? Probably there are some 3rd party plugins?
Just for example: here's a piece of Java code written for Netbeans (which supports preprocessing):
//#if JSR82
//# import javax.bluetooth.*;
//#endif
Netbeans built-in preprocessor parses those //# keys and comments or uncomments pieces of code depending on defined preprocessor keys.
I'd expect something similar in Intellij IDEA.
Thanx in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
(这对于评论来说太长了,因此答案并不是真正的答案,而是更多的评论)。
我广泛使用 Java 预处理器和 IntelliJ IDEA。我不知道有任何插件/附加组件允许使用预处理器(但这很棒)。
除此之外,当讨论 Java 和预处理器时,人们总是会指出“这样的事情不存在”。
当然,其中有几个是存在的。例如,这里有一篇很酷的诺基亚(您可能听说过该公司,他们生产一些 Java 手机)文章,名为:“使用预处理器指令进行 Java ME 移植”。
事实是:Java从未兑现其 WORA 承诺,尤其是在 J(2)ME 世界中。
http://wiki.forum.nokia.com/index.php/Java_ME_Porting_using_preprocessor_directives
使用自定义(遗憾的是自定义)预处理器和代码生成器可以在(优秀的)Trove API 源代码中看到:基本上这是避免对所有 Java 原语重复相同代码的唯一方法,等等。
我也听说过关于人们想要生成“相同”.jar 的不同版本,而不将所有代码放入生成的各种 .jar 中。当然,这可以通过“Java 友好的方式”使用令人惊奇的解决方法来完成......但是在这种情况下,一些预处理也可以节省时间。
要点是:Java 预处理器有有效的情况。我倾向于喜欢诺基亚的,因为很难与它争论;)
(this is too long for a comment, hence the answer that isn't really an answer but more of a comment).
I've worked extensively with Java pre-processors and IntelliJ IDEA. I don't know of any plugin/add-on allowing to work with pre-processors (but that would be great).
Besides that, invariably when a discussion comes on Java and preprocessors, people will point out that "such a thing doesn't exist".
Yet of course several of these exists. For example here's a cool Nokia (you may have heard of that company, they produce a few Java cellphones) article called: "Java ME Porting using preprocessor directives".
Truth is: Java never delivered its WORA promise, especially not in the J(2)ME world.
http://wiki.forum.nokia.com/index.php/Java_ME_Porting_using_preprocessor_directives
Another very valid reason to use custom (sadly custom) preprocessors and code-generators can be seen in things like the (excellent) Trove API source code: basically it's your only way to avoid repeating the same code for all the Java primitives, etc.
I've also heard about people wanting to generate different versions of the "same" .jar, without putting all the code in the various .jars produced. Sure, this can be done in a "Java friendly way" using amazing workarounds... But some pre-processing saves the day too in such a case.
Point is: there are valid case for Java pre-processors. I tend to like the Nokia one because it's kinda hard to argue versus that ;)
您可能想考虑为不同版本的库配置不同的artifacts然后重组代码以隔离仅包含在相关工件中的子类/组件中的版本特定代码。这可能不切实际,尤其是在现有项目中,但比编写插件麻烦少。 Maven 通过分类器具有类似的功能。
You may want to look into configuring different artifacts for the various versions of the lib and then restructuring your code to isolate the version specific code in subclasses/components that are only including in the relevant artifact. This may be impractical, especially in an existing project, but less trouble than writing a plugin. Maven has similar functionality via classifiers.
Java 程序员通常不会像 C/C++ 那样使用预处理器。其他技术和系统已经开发出来,例如注释、代码生成等。也许如果您让我们知道您想要解决什么问题,有人会知道一种 Java 友好的方式来提供帮助。
编辑:我认为共识是你正在考虑编写一个 IntelliJ 插件
Java programmers don't normally use preprocessers the way C/C++ do. Other techniques and systems have been developed like annotations, code generation, etc. Perhaps if you let us know what problem you're trying to solve, someone will know of a java friendly way to help.
Edit: I think the consensus is that you're looking at writing an IntelliJ plugin
这个问题已经过去了 11 年,似乎出现了 Manifold 预处理器,它通过插件与 Java 编译器和 IntelliJ 集成。
Eleven years on from this question, there seems to be the Manifold preprocessor which integrates with the Java compiler and with IntelliJ via a plugin.