Reputation: 23200
I have many Java files from a project which have not been formatted properly. Also due to enforcement of a common style many people have introduced their own coding style. I want to bring all this code to one consistent format. Are there any auto-formatters available which I can run as script on all the files. I would like to rethink and fine tune following options:
Number of spaces used for indentation.
Spaces around operators like + - * / etc.
Separation between parentheses.
etc.
I was looking at Netbeans auto format and looked cool. Moreover the parameters can be set from the option dialog. However I am not able to completely grasp how to use the API
Upvotes: 2
Views: 1613
Reputation: 2169
Well I don't know if you use eclipse, but to do spacing and parenthasis you just press ctrl + shift + f and to do auto indentation is ctrl + i
Upvotes: 0
Reputation: 21663
You can use the Eclipse JavaCodeFormatter
from the command line:
The following example runs the formatting of code specified by the configuration file
D:/formatter.prefs
on the files Java belonging (directly or indirectly) to the directoryD:/tmp/src
:eclipse -application org.eclipse.jdt.core.JavaCodeFormatter -config D:/formatter.prefs D:/tmp/src
See http://wiyoo.blogspot.com/2007/05/batch-formatting-java-source-code-with.html for details.
Upvotes: 5
Reputation: 236004
Try JIndent. Or here're a bunch of open source code formatters for Java, some of them can be run as standalone products, others as plugins inside an IDE (Eclipse, NetBeans).
Upvotes: 0
Reputation: 160181
Why use the API? Bring it in as a NetBeans project. Same with Eclipse or IntelliJ; they can all format all source files in a project.
For standalone, try Jalopy. It can also be set up to run as part of a build, as a commit hook, etc.
Upvotes: 0