独立的Java代码格式化器/美化器/漂亮的打印机?

我有兴趣了解用于Java的高质量,独立源代码格式化器的可用select

格式化程序必须是独立的,也就是说,它必须支持与任何特定开发环境分离“批处理”模式 。 理想情况下,它也应该独立于任何特定的操作系统 。 因此,IDE du jour的内置格式化程序在这里没什么兴趣(除非IDE支持批处理模式格式化程序调用,可能来自命令行)。 用只能在Windows上运行的闭源C / C ++编写的格式化程序并不理想,但有点有趣。

要清楚的是,“格式化程序”(或“美化”)不同于“样式检查程序”。 格式化程序接受源代码作为input,应用样式规则,并生成语义上等同于原始源代码的样式化的源代码。 语法修改仅限于修改代码布局,如更改空白或组织import语句。 很less,如果有的话,执行其他重构。 样式检查器也应用样式规则,但只是报告违反规则而不产生修改的源代码作为输出。 所以图片看起来像这样:

格式化程序 (生成符合样式规则的修改的源代码)

阅读源代码→应用样式规则→编写风格的源代码

样式检查器 (不会产生修改的源代码)

阅读源代码→应用样式规则→写入规则违规

进一步说明

解决scheme必须是高度可configuration的。 我希望能够指定我自己的风格,而不是简单地从预设列表中select。

另外,我不是在寻找一种用Java编写的通用漂亮打印机,它可以打印很多东西。 我想风格的Java代码。

也不一定许多语言的macros大统一格式化程序感兴趣 。 我想这可能是一个很好的解决scheme来支持Java以外的语言,但这不是必需的。

而且, 只执行代码突出显示的工具 正确 无误 。

我对Web服务也不感兴趣。 我想要一个我可以在本地运行的工具。

最后,解决scheme不必限于开源,公共领域,共享软件,免费软件,商业或其他任何东西。 所有forms的许可都可以接受

JIndent(商业)做你想要的:

http://www.jindent.com/

我以前也用Jalopy来做这个,它是开源的:

http://jalopy.sourceforge.net/

编辑:我会注意到,Jalopy是相当老,似乎没有任何最近的更新。 我不太确定它是否可以与Java 5/6一起工作。 你可能想看看雅可布:

http://www.tiobe.com/index.php/content/products/jacobe/Jacobe.html

在Roedy Green的网站上还有一个更完整的格式化列表:

http://mindprod.com/jgloss/beautifier.html

您也可以从命令行运行Eclipse reformatter。 看看这个博客条目

Jalopy可能会感兴趣。 它将插入Ant,因此独立于您的IDE。

Astyle也可能有用。 它是开源的(如Jalopy),但用C ++编写。 它适用于格式化Java / C / C ++,可以批量驱动(通过shell脚本或类似的方式)

更新:根据我的新理解,基于我提交的不正确的错误报告: https ://bugs.eclipse.org/bugs/show_bug.cgi ? id = 325289 OpenMRSFormatter.xml文件实际上必须是首选项文件,而不是XML文件。

你可以在这里find这样的文件: https : //bugs.eclipse.org/bugs/attachment.cgi?id = 178874

或者将任何XML导出的格式化文件转换为:cat formatter.xml | grep设置| sed's / @@'> formatter.asprefs

谢谢Misha


这是我使用Eclipse 3.6 jar制作的东西。 它是仅限于Java的命令行,并且应该跨平台兼容:

http://www.sendspace.com/file/nm4ptt

有一个使用捆绑的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$ 

这是基于这里的post: http : //www.peterfriese.de/formatting-your-code-using-the-eclipse-code-formatter/

因为我不知道这个链接会持续多久,所以我会介绍下面的内容。

这是基于Eclipse 3.6 Helios分发。 我使用的是Linux x86,但它应该适用于任何版本,因为我没有使用任何(据我所知)特定于平台的JAR文件。

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 

插件目录中的相关jar是直接从Eclipse 3.6插件目录中取得的。

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是:

 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 $* 

和OpenMRSFormatter.xml在这里可用: http ://wiki.openmrs.org/download/attachments/3346087/OpenMRSFormatter.xml?version=1&modificationDate=1282168038000

我相信应该足以重现。

在这里find一个基于相同概念的有趣项目: http : //code.google.com/p/maven-java-formatter-plugin/

谢谢Misha

尝试“Jacobe Code Beautifier”:

http://www.tiobe.com/index.php/content/products/jacobe/Jacobe.html

个人版是免费的。

我曾经使用Java博士编码和一个快速CTRL + A +选项卡它格式化所有很好的语法突出显示。 我用它在Mac和Windows,不知道他们是否支持Linux。 它是开源的,独立的,免费的。

我也用了很多PrettyPrinter内嵌JRefactory( http://jrefactory.sourceforge.net/ )。 我通过JEdit的插件发现了它。 我赞赏它,因为它有我见过的更好的javadoc报道。

不幸的是,它似乎已经死了(自2004年以来没有进化),实际上它不支持Java5。

顺便说一句,我确认Triemax是一个基于死亡开源Jalopy的商业套件。

请参阅我们的Java格式化工具,该工具可以处理Java的所有方言,包括版本7。

Jalopy不处理List stuff <MyClass>之类的语句…以及以@开头的lint注释

我已经能够强制它通过将所有<MyClass>更改为LTLTLTMyClassGTGTGT,然后将@更改为// @ // @ // @(我只是重复LT,GT或// @以确保它们不会已经存在了,那么我就可以使用Jalopy了,显然我必须把所有东西都换回原来的样子。

另外,在更改<或>之前,您必须确保您不会像<=或> =等那样进行更改。

所以,你可能把这称为穷人解决scheme,因为它涉及许多手动步骤。 如果我要更频繁地使用Jalopy,我可能会使用sed编写一个脚本,它会自动为我做这些更改。