Recently, I often think about some things developed by the team.CodeInconsistent style is one of the most common problems. It is reasonable to say that you can negotiate and communicate with each other in a uniform style. In fact, it is not because it is an era of individuality!
I have used checkstyle for my work code review for less than half a year. I think it is very good, very powerful, and very convenient. The code written by everyone is the same as that written by a person. I would like to leave the old words here to welcome the new, but I would like to explain it, same as Seoul.
1. Introduction
Http://checkstyle.sourceforge.net/
Checkstyle provides a tool to help Java developers comply with certain encoding specifications. It can automate the code standardization check process, freeing developers from this important but boring task.
Main content of the checkstyle check
(1). javadoc Annotation
(2). Naming Conventions
(3). Title
(4). Import Statement
(5). volume size
(6). Blank
(7). Modifier
(8). Block
(9). Code Problems
(10). Class Design
(11). Hybrid check (package some useful such as non-essential system. Out and printstacktrace)
2. Customization
The code specifications provided by the official website are often too strict to be used in actual work. Therefore, it is necessary to customize the specific code specifications based on the actual situation, checkstyle provides a lot of flexibility for code specification customization.
Next we will define some basic specifications, and we will modify them later.
(1). Do not use the tab key;
(2 ).Avoid repeated import, unnecessary import and import *
(3). constants are all uppercase letters (static final );
(4). The member variable starts with M;
(5 ).When there are multiple modifiers, the modifiers are in the following order :(Public,Protected,Private,Abstract,Static,Final,Transient,Volatile,Synchronized,Native,Strictfp)
......
The rest will be modified later.
3. Configuration File
When running checkstyle, you need a parameter (Note: checkstyle5.4 installed on my Ubuntu server, the latest version is 5.5 ):
This configuration file is used to define your own custom code specifications, you can refer to the official instructions: http://checkstyle.sourceforge.net/availablechecks.html
Here, we will write such a code_check.xml as described above:
<? XML version = "1.0"?> <! Doctype module public "-// puppy crawl // DTD Check Configuration 1.2 // en" "http://www.puppycrawl.com/dtds/configuration_1_2.dtd"> <Module name = "checker"> <! -- Check whether the file ends with a new line --> <Module name = "newlineatendoffile"/> <! -- Check whether the file contains the tab key --> <Module name = "filetabcharacter"/> <Module name = "treewalker"> <! -- All constants are capitalized --> <Module name = "constantname"/> <! -- Avoid. *, duplicate unnecessary and unused import --> <Module name = "avoidstarimport"/> <Module name = "redundantimport"/> <Module name = "unusedimports"/> <! -- The member variable format is: m + upper case + letter --> <Module name = "membername"> <property name = "format" value = "^ m [A-Z] [a-zA-Z0-9] * $"/> </ module> <! -- Check the code block: it must start with braces and if, and cannot be empty, end the braces and start another line --> <Module name = "leftcurly"/> <Module name = "needbraces"/> <Module name = "rightcurly"/> <! -- When there are multiple modifiers, the modifiers are in the following order: (public, protected, Private, abstract, static, final, transient, volatile, synchronized, native, strictfp) --> <Module name = "modifierorder"/> </module>
For more information about inspection specifications, see garden address: http://www.cnblogs.com/liugang/archive/2010/10/26/1860903.html
4. Use checkstyle
I will not introduce the use of plug-in tools. I just think of the role of the plug-in tool. The checkstyle command is very simple. Here I only describe three parameters:-C,-R,-o.
/********************* Checkstyle usage *************** * ***** // ***** parameter * 1. -C configuration file and verify file * 2. -O output result * 3. -R traversal directory ** // use our custom code_checks.xml to check the file $> checkstyle-C ~ /Gitproj/World/code_checks.xml ~ /Gitproj/World/floworld/src/COM/Tianxia/APP/floworld/appreciate/appreciatelatestactivity. Java // $> checkstyle-C ~ /Gitproj/World/code_checks.xml-R ~ /Gitproj/World/floworld/src/COM/Tianxia/APP/floworld/appreciate/
Open result.txt to view the output result:
It is also named incorrectly and is very accurate.
5. hook up with GIT
It's about 12 o'clock.ArticlePost this part, and I will try again later.