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

我对了解 用于 Java 的高质量、独立的源代码格式化程序。的可用选择很感兴趣

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

需要说明的是,“格式化程序”(或“美化程序”)与“样式检查程序”不同格式化程序接受源代码作为输入,应用样式规则,并生成与原始源代码在语义上等价的样式化源代码。语法修改仅限于修改代码布局,比如修改空格,或者组织 import语句。很少(如果有的话)执行其他重构。样式检查器也应用样式规则,但它只是输出 报告规则违反 而不产生修改过的源代码。图片是这样的:

Formatter (生成符合样式规则的修改过的源代码)

阅读源代码 & rarr; 应用样式规则 & rarr; 编写样式化源代码

Style Checker (不产生修改过的源代码)

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

进一步澄清

解决方案必须是高度可配置的。我希望能够指定自己的样式,而不是简单地从预设列表中选择。

另外,我是 不要寻找用 Java 编写的通用漂亮打印机,可以漂亮地打印很多东西。我想样式化 Java 代码。

我也是许多语言的 不一定对大统一格式化程序感兴趣。我想,对于一个解决方案来说,支持 Java 以外的语言可能是件好事,但这并不是一个必要条件。

此外,只执行代码高亮显示的工具是右边的工具。

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

最后,解决方案不必局限于开源、公共领域、共享软件、自由软件、商业或任何其他领域。

68079 次浏览

JIndent (Commercial) does what you want:

http://www.jindent.com/

I've also used Jalopy in the past to do this, it's open source:

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:

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:

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

Jalopy may be of interest. It will plug into Ant and thus is independent of your IDE.

Astyle may 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)

I used to use Dr. Java to 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.

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.

Unfortunately it seems dead (no evolution since 2004) and actually it does not support Java5.

By the way I confirm that Triemax is a commercial suite based on dead open-source Jalopy.

You can also run Eclipse reformatter from command line.

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.

Then save {projectDirectory}/.settings/org.eclipse.jdt.core.prefs to another location.

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"

[WARNING] This seems to have stopped working in Eclipse 2018. Use Eclipse Oxygen.

Try "Jacobe Code Beautifier":

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

The personal edition is free.

UPDATE: Per my new understanding based on an incorrect bug report I filed: https://bugs.eclipse.org/bugs/show_bug.cgi?id=325289 The OpenMRSFormatter.xml file actually has to be a preferences file and not an XML file.

You can find such a file here: 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

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:

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

There is a shell script that uses a bundled OpenMRSCodeFormatter.xml file. The sample output:

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/

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.

The contents of the tar archive are:

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.

The contents of config.ini are:

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:

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

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/

Thank you Misha

See our Java Formatter for a tool that handles all dialects of Java, including version 7.

Jalopy does not handle Statements like List stuff<MyClass> ... and lint comments starting with @

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.

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.