有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

编码风格独立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 highlightingrightout

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

最后,解决方案不必局限于开源、公共领域、共享软件、自由软件、商业或任何其他领域所有形式的许可都是可以接受的


共 (6) 个答案

  1. # 1 楼答案

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

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

    顺便说一句,我确认Triemax是一个基于过时的开源老爷车的商业套件

  2. # 2 楼答案

    Jalopy可能会引起兴趣。它将插入Ant,因此独立于IDE

    Astyle也可能有用。它是开源的(像JalalEp),但是用C++编写。它面向Java/C/C++格式,可以是批处理驱动的(通过shell脚本或类似方式)

  3. # 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. # 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. # 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文件。示例输出:

    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$ 
    

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

    由于我不知道链接会持续多久,我将在下面描述内容

    这是基于Eclipse3.6Helios分布。我使用了一个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直接取自Eclipse3.6插件目录

    配置的内容。它们是:

    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
    

    格式。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 $*
    

    还有OpenMRFormatter。xml可在以下位置获得: http://wiki.openmrs.org/download/attachments/3346087/OpenMRSFormatter.xml?version=1&modificationDate=1282168038000

    我认为这应该足以复制

    以下是一个基于相同概念的有趣项目: http://code.google.com/p/maven-java-formatter-plugin/

    谢谢! 米莎