Code specification Tool-checkstyle Manual

Source: Internet
Author: User
Tags checkstyle

The previous time center needs the external package project to carry on the code quality Management, therefore in this to the previous period work does the summary.

Now many open source tools can be code to standardize the audit, more popular with the following several, roughly to a simple introduction.

PMD: is a BSD protocol issued by the Java program Code Checker, you can do to check whether the Java code contains unused variables, whether there is an empty grab block, contains unnecessary objects and so on.
FindBugs: is a static analysis tool that examines a class or JAR file and compares bytecode with a set of bug patterns to identify possible problems.
Checkstyle: is a static analysis tool that examines Java program code.
Cppcheck is a tool for static inspection of C/s code defects.
Pc-lint is also a static code detection tool that examines C or C + +.

At present, the center uses the Checkstyle tool. I personally feel that PMD and Checkstyle are very similar and can be integrated into either eclipse or myeclipse development environments as Plug-ins. Below is a detailed introduction to the use of Checkstyle in Eclipse, and you are welcome to continue to add.

1 Checkstyle Download and installation

Download of 1.1 Checkstyle

Checkstyle can be downloaded from its official website http://checkstyle.sourceforge.net/. The official website also provides checkstyle related documents, such as configuration files, code check items, and so on, the content is richer, the coverage is also more complete. Can be based on their own needs, reference to the official web related information. Enter the official website of checkstyle, enter download page, can download Checkstyle. Currently, most development projects use Eclipse or myeclipse integrated development environment, so I recommend entering http://en.sourceforge.jp/projects/sfnet_eclipse-cs/releases/downloads, There are various versions of Eclipsecheckstyle plug-in on the site. In the introduction below, I am using the net.sf.eclipsecs-updatesite_5.5.0.201111092104-bin.zip version of the Checkstyle plug-in.

Installation of 1.2 Checkstyle

The following installation process is based on Eclipse SDK-3.7.2.

(1) Open Eclipse and select Install software in Help.

(2) Click Add, select the Checkstyle installation package net.sf.eclipsecs-updatesite_5.5.0.201111092104-bin.zip;

(3) Click OK to enter the next step;

(4) Restart Eclipse.

To confirm the installation success:

Open Eclipse, select Checkstyle in the window->preferences-> left navigation bar, and if it appears checkstyle you install successfully.


2 Configuration of Checkstyle

Checkstyle The default configuration file provides two: one is Sun Checks, the corresponding configuration file is Sun_checks.xml file, meaning Checkstyle configuration that Checks the sun coding Conventions General Checkstyle The default setting is Sun_checks.xml configuration file. The other is Sun Checks (Eclipse), the corresponding configuration file is Sun_checks_eclipse.xml, meaning slightly modified version of Sun Checks that better The default code formatter setting of Eclipse.

The general Sun checks check is more stringent, our actual project does not require such a perfect programming specification, then you can define the profile of the check, and finally set the Checkstyle configuration file as our custom file. As for the check set, I will be explained in the following section. Let's take a look at how fast checkstyle can be used.

Quick Start use for 3 Checkstyle

Once the Checkstyle is installed, you can use it to check our code.

3.1 Using Checkstyle to check a single Java file

Select the file, right-click to select Checkstyle, and select Check Code with Checkstyle.



At this point, look at the changes in the original file. Yellow color will appear in the original file where it does not conform to the specification. Number, the mouse to stay in the exclamation point, you can see information tips. Alternatively, in the console console, you can see a checkstyle violations view with an informational hint.



Select Checksty Violations Chart view, will be every kind of irregular information to do the statistics, draw into a chart, enough humane Bar, hahaha.



3.2 Use Checkstyle to check the entire project

On the project name, right-click to select Checkstyle and select Check Code with Checkstyle.


View irregular information in the same way as Section 3.1.


Checkstyle can also check the entire package file, or a code snippet of the file. Concrete, we can rely on the needs, I will not introduce one by one.

4, Checkstyle of various inspection items

Before the user can customize a variety of checks, below to introduce the next checkstyle in a variety of commonly used checks. In the fifth part, I'll explain how to customize the check file according to the requirements of the check.

Because of space limitations, in order to let everyone not to see too tired, also for speaking more thoroughly, I put the checkstyle of the inspection configuration content as a separate blog. Interested readers can browse thecheckstyle inspection configuration Details
"article.

Members are welcome to participate in the discussion with a view to continuing the improvement.







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.