独立的 Java 代码格式化程序/美化程序/漂亮的打印机?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/996646/
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
Stand-alone Java code formatter/beautifier/pretty printer?
提问by Greg Mattes
I'm interested in learning about the available choices of high-quality, stand-alone source code formatters for Java.
我有兴趣了解可供选择的高质量、独立的 Java 源代码格式化程序。
The formatter must be stand-alone, that is, it must support a "batch" modethat is decoupled from any particular development environment. Ideally, it should be independent of any particular operating systemas well. So, a built-in formatter for the IDE du jour is of little interest here (unless that IDE supports batch mode formatter invocation, perhaps from the command line). A formatter written in closed-source C/C++ that only runs on, say, Windows is not ideal, but is somewhat interesting.
该格式必须是独立的,也就是说,它必须支持“批量”模式是从任何特定的开发环境脱钩。理想情况下,它也应该独立于任何特定的操作系统。因此,IDE du jour 的内置格式化程序在这里没什么用(除非 IDE 支持批处理模式格式化程序调用,可能是从命令行调用)。用闭源 C/C++ 编写的格式化程序只在 Windows 上运行并不理想,但有点有趣。
To be clear, a "formatter" (or "beautifier") is not the same as a "style checker."A formatter accepts source code as input, applies styling rules, and produces styled source code that is semantically equivalent to the original source code. Syntactic modifications are limited to things like modifying code layout as in changing whitespace, or organizing import
statements. Very little, if any, other refactoring is performed. A style checker also applies styling rules, but it simply reportsrule violations without producing modified source codeas output. So the picture looks like this:
需要明确的是,“格式化程序”(或“美化程序”)与“样式检查程序”不同。格式化程序接受源代码作为输入,应用样式规则,并生成在语义上与原始源代码等效的样式源代码。语法修改仅限于修改代码布局(如更改空格)或组织import
语句等。很少(如果有)执行其他重构。样式检查器也应用样式规则,但它只是报告违反规则的情况,而不会生成修改后的源代码作为输出。所以图片看起来是这样的:
Formatter(produces modified source code that conforms to styling rules)
格式化程序(生成符合样式规则的修改后的源代码)
Read Source Code → Apply Styling Rules → Write Styled Source Code
阅读源代码 → 应用样式规则 → 编写样式化的源代码
Style Checker(does not produce modified source code)
样式检查器(不产生修改的源代码)
Read Source Code → Apply Styling Rules → Write Rule Violations
阅读源代码 → 应用样式规则 → 写入规则违规
Further Clarifications
进一步说明
Solutions must be highly configurable. I want to be able to specify my own style, not simply select from a preset list.
解决方案必须是高度可配置的。我希望能够指定我自己的风格,而不是简单地从预设列表中进行选择。
Also, I'm not looking for a general purpose pretty-printer written in Javathat can pretty-print many things. I want to style Java code.
另外,我不是在寻找用 Java 编写的通用漂亮打印机,它可以漂亮地打印很多东西。我想设计 Java 代码的样式。
I'm also not necessarilyinterested in a grand-unified formatterfor many languages. I suppose it might be nice for a solution to have support for languages other than Java, but that is not a requirement.
我也不一定对多种语言的大统一格式化程序感兴趣。我想解决方案支持 Java 以外的语言可能会很好,但这不是必需的。
Furthermore, tools that only perform code highlightingare rightout.
此外,只有执行的代码高亮工具是正确的了。
I'm also not interested in a web service.I want a tool that I can run locally.
我也对网络服务不感兴趣。我想要一个可以在本地运行的工具。
Finally, solutions need not be restricted to open source, public domain, shareware, free software, commercial, or anything else. All forms of licensing are acceptable.
最后,解决方案不必局限于开源、公共领域、共享软件、免费软件、商业或其他任何东西。所有形式的许可都是可以接受的。
采纳答案by Jon
JIndent (Commercial) does what you want:
JIndent(商业)做你想要的:
I've also used Jalopy in the past to do this, it's open source:
我过去也使用过 Jalopy 来做到这一点,它是开源的:
http://jalopy.sourceforge.net/
http://jalopy.sourceforge.net/
EDIT: I will note that Jalopy is fairly old and there doesn't seem to be any recent updates to it. I'm not quite sure if it will work with Java 5/6. You might want to look at Jacobe instead:
编辑:我会注意到 Jalopy 相当古老,而且最近似乎没有任何更新。我不太确定它是否适用于 Java 5/6。你可能想看看 Jacobe:
http://www.tiobe.com/index.php/content/products/jacobe/Jacobe.html
http://www.tiobe.com/index.php/content/products/jacobe/Jacobe.html
There's also a more complete list of formatters here on Roedy Green's site:
Roedy Green 的网站上还有更完整的格式化程序列表:
回答by Brian Agnew
Jalopymay be of interest. It will plug into Ant and thus is independent of your IDE.
Jalopy可能很有趣。它将插入 Ant,因此独立于您的 IDE。
Astylemay also be useful. It's open source (like Jalopy) but written in C++. It's geared towards formatting Java/C/C++ and can be batch-driven (via shell scripts or similar)
Astyle也可能有用。它是开源的(像 Jalopy)但是是用 C++ 编写的。它面向格式化 Java/C/C++ 并且可以是批处理驱动的(通过 shell 脚本或类似的)
回答by Petey B
I used to use Dr. Javato code in and with a quick ctrl+A + tab it formats it all nicely with syntax highlighting. Ive used it on a Mac and Windows, dont know if they support linux. And it's open source, stand alone, and free.
我曾经使用Dr. Java编写代码,并使用快速 ctrl+A + 选项卡将其格式化并突出显示语法。我在 Mac 和 Windows 上使用过它,不知道它们是否支持 linux。它是开源的、独立的、免费的。
回答by Michael Zilbermann
I also used a lot the PrettyPrinter embedded inside JRefactory (http://jrefactory.sourceforge.net/). I discovered it through its plugin for JEdit. I appreciated it as it had the better javadoc coverage I ever saw.
我还使用了很多嵌入在 JRefactory ( http://jrefactory.sourceforge.net/) 中的 PrettyPrinter 。我是通过它的 JEdit 插件发现的。我很欣赏它,因为它有我见过的更好的 javadoc 覆盖范围。
Unfortunately it seems dead (no evolution since 2004) and actually it does not support Java5.
不幸的是它似乎已经死了(自 2004 年以来没有进化),实际上它不支持 Java5。
By the way I confirm that Triemax is a commercial suite based on dead open-source Jalopy.
顺便说一下,我确认 Triemax 是一个基于死开源 Jalopy 的商业套件。
回答by notnoop
You can also run Eclipse reformatter from command line.
您还可以从命令行运行 Eclipse 重新格式化程序。
Here is the Eclipse documentation of the feature
First you have to create configuration file specific to your project of your Java code formatter settings.
首先,您必须创建特定于您的 Java 代码格式化程序设置项目的配置文件。
Then save {projectDirectory}/.settings/org.eclipse.jdt.core.prefs
to another location.
然后保存{projectDirectory}/.settings/org.eclipse.jdt.core.prefs
到另一个位置。
Now you will call the formatter.
现在您将调用格式化程序。
"c:\path\to\eclipse\eclipse.exe" -vm "C:\path\to\jdk\bin\java.exe" -application org.eclipse.jdt.core.JavaCodeFormatter -config "c:\path\to\org.eclipse.jdt.core.prefs" "C:\path\to\your\src"
"c:\path\to\eclipse\eclipse.exe" -vm "C:\path\to\jdk\bin\java.exe" -application org.eclipse.jdt.core.JavaCodeFormatter -config "c:\path\to\org.eclipse.jdt.core.prefs" "C:\path\to\your\src"
[WARNING] This seems to have stopped working in Eclipse 2018. Use Eclipse Oxygen.
[警告] 这似乎在 Eclipse 2018 中停止工作。使用 Eclipse Oxygen。
回答by user152102
Try "Jacobe Code Beautifier":
试试“雅各代码美化器”:
http://www.tiobe.com/index.php/content/products/jacobe/Jacobe.html
http://www.tiobe.com/index.php/content/products/jacobe/Jacobe.html
The personal edition is free.
个人版是免费的。
回答by Миша Кошелев
UPDATE: Per my new understanding based on an incorrectbug report I filed: https://bugs.eclipse.org/bugs/show_bug.cgi?id=325289The OpenMRSFormatter.xml file actually has to be a preferences file and not an XML file.
更新:根据我提交的错误错误报告的新理解:https://bugs.eclipse.org/bugs/show_bug.cgi ?id =325289 OpenMRSFormatter.xml 文件实际上必须是首选项文件而不是 XML文件。
You can find such a file here: https://bugs.eclipse.org/bugs/attachment.cgi?id=178874
你可以在这里找到这样的文件:https: //bugs.eclipse.org/bugs/attachment.cgi?id=178874
or convert any XML exported formatter file with: cat formatter.xml | grep setting | sed 's/@@' > formatter.asprefs
或使用以下命令转换任何 XML 导出的格式化程序文件: cat formatter.xml | grep 设置 | sed 's/@@' > formatter.asprefs
Thank you Misha
谢谢米莎
Here is something that I made using Eclipse 3.6 jars. It is Java-only, command line, and should be cross platform compatible:
这是我使用 Eclipse 3.6 jars 制作的东西。它是仅限 Java 的命令行,并且应该是跨平台兼容的:
http://www.sendspace.com/file/nm4ptt
http://www.sendspace.com/file/nm4ptt
There is a shell script that uses a bundled OpenMRSCodeFormatter.xml file. The sample output:
有一个使用捆绑的 OpenMRSCodeFormatter.xml 文件的 shell 脚本。示例输出:
misha@misha-d630:~/openmrs-code-formatter$ cat ../Test.java
public class Test {
public a() {
System.out.println("! I did not format this right!");
}
}misha@misha-d630:~/openmrs-code-formatter$ ./format.sh ../Test.java
Configuration Name: OpenMRSFormatter.xml
Starting format job ...
Done.
cat ../misha@misha-d630:~/openmrs-code-formatter$ cat ../Test.java
public class Test {
public a() {
System.out.println("! I did not format this right!");
}
}misha@misha-d630:~/openmrs-code-formatter$
This is based on the post here: http://www.peterfriese.de/formatting-your-code-using-the-eclipse-code-formatter/
这是基于这里的帖子:http: //www.peterfriese.de/formatting-your-code-using-the-eclipse-code-formatter/
As I do not know how long the link will stay up, I will describe the contents below.
由于我不知道链接会保留多久,我将在下面描述内容。
This is based on Eclipse 3.6 Helios distribution. I used the one for Linux x86, but it should work for any version, as I did not use any (to my knowledge) platform specific JAR files.
这是基于 Eclipse 3.6 Helios 发行版。我将它用于 Linux x86,但它应该适用于任何版本,因为我没有使用任何(据我所知)平台特定的 JAR 文件。
The contents of the tar archive are:
tar 存档的内容是:
misha@misha-d630:~$ tar tvjf openmrs-code-formatter.tar.bz2 | sort
tar: Record size = 8 blocks
drwxr-xr-x misha/misha 0 2010-09-11 19:32 openmrs-code-formatter/plugins/
drwxr-xr-x misha/misha 0 2010-09-11 19:33 openmrs-code-formatter/configuration/org.eclipse.equinox.simpleconfigurator/
drwxr-xr-x misha/misha 0 2010-09-11 19:38 openmrs-code-formatter/configuration/
-rw-r--r-- misha/misha 101957 2010-09-11 19:03 openmrs-code-formatter/plugins/org.eclipse.equinox.common_3.6.0.v20100503.jar
-rw-r--r-- misha/misha 107210 2010-09-11 19:32 openmrs-code-formatter/plugins/org.eclipse.core.commands_3.6.0.I20100512-1500.jar
-rw-r--r-- misha/misha 111098 2010-09-11 19:06 openmrs-code-formatter/plugins/org.eclipse.equinox.preferences_3.3.0.v20100503.jar
-rw-r--r-- misha/misha 1147220 2010-09-11 18:45 openmrs-code-formatter/plugins/org.eclipse.osgi_3.6.0.v20100517.jar
-rw-r--r-- misha/misha 1812 2010-09-11 19:33 openmrs-code-formatter/configuration/org.eclipse.equinox.simpleconfigurator/bundles.info
-rw-r--r-- misha/misha 182596 2010-09-11 19:05 openmrs-code-formatter/plugins/org.eclipse.equinox.registry_3.5.0.v20100503.jar
-rw-r--r-- misha/misha 248071 2010-09-11 19:29 openmrs-code-formatter/plugins/org.eclipse.text_3.5.0.v20100601-1300.jar
-rw-r--r-- misha/misha 265 2010-09-11 19:21 openmrs-code-formatter/configuration/config.ini
-rw-r--r-- misha/misha 28554 2010-09-11 19:34 openmrs-code-formatter/OpenMRSFormatter.xml
-rw-r--r-- misha/misha 39331 2010-09-11 19:17 openmrs-code-formatter/plugins/org.eclipse.equinox.simpleconfigurator_1.0.200.v20100503.jar
-rw-r--r-- misha/misha 4504121 2010-09-11 19:27 openmrs-code-formatter/plugins/org.eclipse.jdt.core_3.6.0.v_A58.jar
-rw-r--r-- misha/misha 47023 2010-09-11 18:42 openmrs-code-formatter/plugins/org.eclipse.equinox.launcher_1.1.0.v20100507.jar
-rw-r--r-- misha/misha 52710 2010-09-11 19:29 openmrs-code-formatter/plugins/org.eclipse.core.filesystem_1.3.0.v20100526-0737.jar
-rw-r--r-- misha/misha 6451211 2010-09-11 19:32 openmrs-code-formatter/plugins/com.ibm.icu_4.2.1.v20100412.jar
-rw-r--r-- misha/misha 70928 2010-09-11 19:01 openmrs-code-formatter/plugins/org.eclipse.core.runtime_3.6.0.v20100505.jar
-rw-r--r-- misha/misha 769898 2010-09-11 19:29 openmrs-code-formatter/plugins/org.eclipse.core.resources_3.6.0.v20100526-0737.jar
-rw-r--r-- misha/misha 83817 2010-09-11 19:07 openmrs-code-formatter/plugins/org.eclipse.equinox.app_1.3.0.v20100512.jar
-rw-r--r-- misha/misha 86986 2010-09-11 19:31 openmrs-code-formatter/plugins/org.eclipse.core.expressions_3.4.200.v20100505.jar
-rw-r--r-- misha/misha 90837 2010-09-11 19:05 openmrs-code-formatter/plugins/org.eclipse.core.jobs_3.5.0.v20100515.jar
-rw-r--r-- misha/misha 91928 2010-09-11 19:06 openmrs-code-formatter/plugins/org.eclipse.core.contenttype_3.4.100.v20100505-1235.jar
-rwxr-xr-x misha/misha 111 2010-09-11 19:39 openmrs-code-formatter/format.sh
Relevant jars in plugins directory are taken directly from Eclipse 3.6 plugins directory.
plugins 目录中的相关 jars 直接取自 Eclipse 3.6 plugins 目录。
The contents of config.ini are:
config.ini 的内容是:
misha@misha-d630:~$ more openmrs-code-formatter/configuration/config.ini
osgi.bundles=reference\:file\:org.eclipse.equinox.simpleconfigurator_1.0.200.v20100503.jar@1\:start
org.eclipse.equinox.simpleconfigurator.configUrl=file\:org.eclipse.equinox.simpleconfigurator/bundles.info
eclipse.application=org.eclipse.jdt.core.JavaCodeFormatter
format.sh is:
format.sh 是:
misha@misha-d630:~$ more openmrs-code-formatter/format.sh
#!/bin/bash
java -jar plugins/org.eclipse.equinox.launcher_1.1.0.v20100507.jar -config OpenMRSFormatter.xml $*
and OpenMRSFormatter.xml is available here: http://wiki.openmrs.org/download/attachments/3346087/OpenMRSFormatter.xml?version=1&modificationDate=1282168038000
和 OpenMRSFormatter.xml 可在此处获得:http://wiki.openmrs.org/download/attachments/3346087/OpenMRSFormatter.xml?version=1&modificationDate=1282168038000
I believe that should be sufficient to reproduce.
我相信这应该足以重现。
An interesting project based on the same concept is found here: http://code.google.com/p/maven-java-formatter-plugin/
这里有一个基于相同概念的有趣项目:http: //code.google.com/p/maven-java-formatter-plugin/
Thank you Misha
谢谢米莎
回答by Ira Baxter
See our Java Formatterfor a tool that handles all dialects of Java, including version 7.
请参阅我们的Java Formatter以获取处理所有 Java 方言(包括版本 7)的工具。
回答by cpu
Jalopy does not handle Statements like List stuff<MyClass> ... and lint comments starting with @
Jalopy 不处理像 List stuff<MyClass> ... 和以 @ 开头的 lint 注释之类的语句
I have been able to force it to format by changing all the <MyClass> into LTLTLTMyClassGTGTGT and then @ into //@//@//@ (I just repeat the LT, GT or //@ to make sure they don't already exist. Then I was able to Jalopy, and obviously I have to replace everything back to what it was.
我已经能够通过将所有 <MyClass> 更改为 LTLTLTMyClassGTGTGT 然后将 @ 更改为 //@//@//@(我只是重复 LT、GT 或 //@ 以确保它们不已经存在。然后我就可以使用 Jalopy,显然我必须将所有东西都替换回原来的样子。
Also, prior to changing the < or > you have to make sure you won't change compares like <= or >= etc.
此外,在更改 < 或 > 之前,您必须确保不会更改 <= 或 >= 等比较。
So, you might call this the poor man solution because it involves many manual steps. If I was to do Jalopy more often, I would probably write a script using sed that would make those changes automatically for me.
因此,您可以称其为穷人解决方案,因为它涉及许多手动步骤。如果我要更频繁地使用 Jalopy,我可能会使用 sed 编写一个脚本,它会自动为我进行这些更改。