Java代码格式

the*_*rty 5 java formatting coding-style

我正在使用FreeMarker生成java代码,但由于大多数是动态生成的,因此很难控制代码的形成.

我希望代码格式化得很好.有没有人知道lib或类似于java代码的漂亮打印机?

the*_*rty 1

我想我会使用 Eclipse 的 CodeFormatter,就像这个人一样:http ://ssscripting.wordpress.com/2009/06/10/how-to-use-the-eclipse-code-formatter-from-your-code/

更新: 最终使用 jastyle ( http://sourceforge.net/projects/jastyle/ )。这是一个例子:

public static String formatJavaCode(String code) throws Exception {
    ASFormatter formatter = new ASFormatter();

    // bug on lib's implementation. reported here: http://barenka.blogspot.com/2009/10/source-code-formatter-library-for-java.html
    code.replace("{", "{\n");

    Reader in = new BufferedReader(new StringReader(code));
    formatter.setJavaStyle();
    in.close();
    return FormatterHelper.format(in,formatter);
}
Run Code Online (Sandbox Code Playgroud)