6

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
the qwerty
  • 229
  • 3
  • 11

10 Answers10

2

Google java format works perfect for me. https://github.com/google/google-java-format

After maven build, locate the google-java-format-0.1-SNAPSHOT.jar in core/target folder, and try java -jar google-java-format-0.1-SNAPSHOT.jar to see usage information.

digiter
  • 21
  • 3
1

You can use Eclipse's or Jalopy code formatters to reformat generated code.

Eugene Kuleshov
  • 31,461
  • 5
  • 66
  • 67
  • but i want to do it programatically. eclipse's code formater can be used as a standalone lib? – the qwerty May 20 '10 at 17:28
  • Theoretically yes, but isn't going to be a trivial exercise. But I believe jalopy formatter can be embedded much easier. – Eugene Kuleshov May 20 '10 at 18:28
  • yes, my first implementation was done using Eclipse's formatter. but i ended up exchanging that with http://sourceforge.net/projects/jastyle/ because of its simplicity. – the qwerty May 21 '10 at 16:15
1

You can run a formatting program like astyle

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
  • do you know a lib in java that does the same. i want one to include in my project, and pass all the strings generated to the formatter. – the qwerty May 20 '10 at 17:43
1

Jalopy works beautifully. You can use it's CLI for standalone usage. Japlopy Console Plugin

golfradio
  • 457
  • 3
  • 7
  • 17
1

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);
}
the qwerty
  • 229
  • 3
  • 11
0

You can format it when you edit the .java file in Eclipse. When you don't edit it, it doesn't matter if it is formatted or not.

Daniel Engmann
  • 2,840
  • 1
  • 23
  • 17
0

I switched from FreeMarker to my own Java source code generation utility. The sources are accessible from here : https://source.mysema.com/svn/mysema/projects/codegen/trunk/

It is designed in such a way that you just call the API and the output is properly formatted. Here is an example :

    JavaWriter writer = new JavaWriter(new StringWriter());   
    writer.beginClass("FieldTests");
    writer.privateField("String", "privateField");
    writer.privateStaticFinal("String", "privateStaticFinal", "\"val\"");
    writer.protectedField("String","protectedField");
    writer.field("String","field");
    writer.publicField("String","publicField");
    writer.publicStaticFinal("String", "publicStaticFinal", "\"val\"");
    writer.publicFinal("String", "publicFinalField");
    writer.publicFinal("String", "publicFinalField2", "\"val\"");        
    writer.end();

Which turns into

public class FieldTests {

    private String privateField;

    private static final String privateStaticFinal = "val";

    protected String protectedField;

    String field;

    public String publicField;

    public static final String publicStaticFinal = "val";

    public final String publicFinalField;

    public final String publicFinalField2 = "val";

}

I developed the codegen utility for Querydsl which mirrors Java domain types into query types. So the serialization needs are very complex. Using FreeMarker templates simple didn't scale. There was too much customization in the output, which is better to control in Java than a template language syntax.

This is not an advertisement for the Codegen module. I just wanted to make the point that for highly customizable serialization FreeMarker might not scale.

Timo Westkämper
  • 21,824
  • 5
  • 78
  • 111
  • The sources were moved to https://source.mysema.com/svn/mysema/projects/deprecated/codegen/trunk – migu Mar 09 '12 at 08:48
0

The simplest approach would be to paste your code in the eclipse java IDE and do a ctrl+f on your selected code. which would format your code in easily readable form.

Sal
  • 167
  • 2
  • 10
0

For standalone command-line tools with plenty of configuration options to fit your code formatting style, you can try:

They are both free, open-source, and can format more than just Java source code (C, C++, C#, etc.)

Flux
  • 9,805
  • 5
  • 46
  • 92
0

You can simply activate a save action in eclipse and use your own or predefined formatter.

Just activate both in preferences:

  • Window -> Preferences -> Java -> Code Style -> Formatter

Also activate a save action:

  • Window -> Preferences -> Java -> Editor -> Save Action -> Format source code (Format all lines & Imports)

Code Formatter | Save Action

Chris S
  • 11
  • 1