编码风格独立Java代码格式化程序/美化器/漂亮打印机?
我有兴趣了解高质量、独立的Java源代码格式化程序的可用选项强>
格式化程序必须是独立的,也就是说,它必须支持decoupled from any particular development environment的“批处理”模式。理想情况下,它也应该独立于任何特定的操作系统。因此,这里对IDE的内置格式化程序不感兴趣(除非IDE支持批处理模式格式化程序调用,可能是从命令行调用)。用封闭源代码C/C++编写的格式化程序只在Windows上运行并不理想,但有点有趣
要清楚,“格式化程序”(或“美化程序”)与“样式检查器”不同格式化程序接受源代码作为输入,应用样式规则,并生成语义上与原始源代码等价的样式化源代码。语法修改仅限于修改代码布局,如更改空格或组织import
语句。很少(如果有的话)执行其他重构。样式检查器也应用样式规则,但它只报告规则冲突,而不生成修改后的源代码作为输出。因此,图片如下所示:
格式化程序(生成符合样式规则的修改后的源代码)
读取源代码→ 应用样式规则→ 编写样式化源代码
样式检查器(不生成修改过的源代码)
读取源代码→ 应用样式规则→ 写入规则冲突
进一步澄清
解决方案必须是高度可配置的。我希望能够指定自己的样式,而不是简单地从预设列表中选择
另外,我不是在寻找一个可以打印很多东西的general purpose pretty-printer written in Java。我想设计Java代码的样式
我对许多语言的grand-unified formatter也不一定感兴趣。我认为一个解决方案支持Java以外的语言可能很好,但这不是一个要求
此外,tools that only perform code highlighting是rightout强>
我对web服务也不感兴趣我想要一个可以在本地运行的工具
最后,解决方案不必局限于开源、公共领域、共享软件、自由软件、商业或任何其他领域所有形式的许可都是可以接受的
# 1 楼答案
我还使用了很多嵌入JRefactory(http://jrefactory.sourceforge.net/)中的PrettyPrinter。我是通过JEdit的插件发现的。我很欣赏它,因为它有我见过的更好的javadoc报道
不幸的是,它似乎已经死了(自2004年以来没有进化),实际上它不支持Java5
顺便说一句,我确认Triemax是一个基于过时的开源老爷车的商业套件
# 2 楼答案
Jalopy可能会引起兴趣。它将插入Ant,因此独立于IDE
Astyle也可能有用。它是开源的(像JalalEp),但是用C++编写。它面向Java/C/C++格式,可以是批处理驱动的(通过shell脚本或类似方式)
# 3 楼答案
JIndent(商业版)满足您的需求:
http://www.jindent.com/
我过去也用过Jalopy,它是开源的:
http://jalopy.sourceforge.net/
编辑:我会注意到,老爷车是相当旧的,似乎没有任何最近的更新。我不确定它是否适用于Java5/6。你可能想看看雅各布:
http://www.tiobe.com/index.php/content/products/jacobe/Jacobe.html
Roedy Green的网站上还有一个更完整的格式化程序列表:
http://mindprod.com/jgloss/beautifier.html
# 4 楼答案
您还可以从命令行运行Eclipse重新格式化
这是Eclipse documentation of the feature
首先你必须create configuration file specific to your project of your Java code formatter settings.
然后将
{projectDirectory}/.settings/org.eclipse.jdt.core.prefs
保存到另一个位置现在您将调用格式化程序
"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"
[警告]这似乎已经在Eclipse 2018中停止工作。使用氧气
# 5 楼答案
试试“雅各布代码美化器”:
http://www.tiobe.com/index.php/content/products/jacobe/Jacobe.html
个人版是免费的
# 6 楼答案
更新:根据我的新理解,基于我提交的错误报告:https://bugs.eclipse.org/bugs/show_bug.cgi?id=325289 OpenMsFormatter。xml文件实际上必须是首选项文件,而不是xml文件
你可以在这里找到这样一个文件: https://bugs.eclipse.org/bugs/attachment.cgi?id=178874
或使用以下格式转换任何XML导出的格式化程序文件: 猫格式化程序。xml | grep设置| sed's/@@>;格式化程序。asprefs
谢谢! 米莎
下面是我使用Eclipse3.6JAR制作的一些东西。它是仅限Java的命令行,并且应该是跨平台兼容的:
http://www.sendspace.com/file/nm4ptt
有一个shell脚本使用捆绑的OpenMRSCodeFormatter。xml文件。示例输出:
这是基于这里的帖子: http://www.peterfriese.de/formatting-your-code-using-the-eclipse-code-formatter/
由于我不知道链接会持续多久,我将在下面描述内容
这是基于Eclipse3.6Helios分布。我使用了一个Linux x86,但它应该适用于任何版本,因为我没有使用任何(据我所知)特定于平台的JAR文件
tar档案的内容包括:
插件目录中的相关JAR直接取自Eclipse3.6插件目录
配置的内容。它们是:
格式。sh是:
还有OpenMRFormatter。xml可在以下位置获得: http://wiki.openmrs.org/download/attachments/3346087/OpenMRSFormatter.xml?version=1&modificationDate=1282168038000
我认为这应该足以复制
以下是一个基于相同概念的有趣项目: http://code.google.com/p/maven-java-formatter-plugin/
谢谢! 米莎