Question: 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?

Answer: 
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.
You can run a formatting program like [astyle](http://astyle.sourceforge.net/)