简体   繁体   中英

Automatically pretty format large number of Java source files

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:

  1. Number of spaces used for indentation.

  2. Spaces around operators like + - * / etc.

  3. Separation between parentheses.

  4. 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

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 directory D:/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.

好吧,我不知道您是否使用eclipse,但是要进行间距和括号运算,只需按ctrl + shift + f并执行自动缩进就是ctrl + i

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM