Java代码格式 [英] Java Code Formatting

查看:164
本文介绍了Java代码格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用FreeMarker来生成Java代码,但是由于它大部分是动态生成的,所以很难控制代码的形成。

我想要得到格式良好的代码。有没有人知道一个lib或类似的打印机的Java代码?

这家伙: http://ssscripting.wordpress.com/2009/06/10/how-to-use-the-eclipse-code-formatter-from-your-code/

更新:
使用jastyle结束( http://sourceforge.net/projects/jastyle/ )。
这里有一个例子:

$ $ $ $ $ $ $ $ $ $ $ $ $'新的ASFormatter();

// lib执行的错误。这里报告:http://barenka.blogspot.com/2009/10/source-code-formatter-library-for-java.html
code.replace({,{\ n);

读者在= new BufferedReader(new StringReader(code));
formatter.setJavaStyle();
in.close();
返回FormatterHelper.format(in,formatter);
}


I'm using FreeMarker to generate java code, but as most of it is dynamically generated it's difficult to control the code formation.

I want to get code well formatted. Does anyone knows a lib or something like a pretty printer for java code?

解决方案

I guess i will use Eclipse's CodeFormatter, just like this guy: http://ssscripting.wordpress.com/2009/06/10/how-to-use-the-eclipse-code-formatter-from-your-code/

UPDATE: ended up using jastyle ( http://sourceforge.net/projects/jastyle/ ). here's an example:

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);
}

这篇关于Java代码格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆