Static analysis tools and usage summary (b) Checkstyle

Source: Internet
Author: User
Tags modifiers naming convention checkstyle

Here are three open source tools, PMD, Checkstyle and findbugs, with emphasis on ant calls, and commercial software Jtest is said to be a well-known tool for code analysis, haha.

Checkstyle (http://checkstyle.sourceforge.net/) version 4.2

Introduction: Checkstyle is a development tool that can help programmers follow code specifications to write code,

It automatically handles these annoying but important tasks, which are highly configurable and support the vast majority of code specifications, such as the Sun-recommended code contract (http://java.sun.com/docs/codeconv/) and other well-known conventions, Checkstyle can check many aspects of the code, from the traditional point of view, it is mainly used to check the level of the code, since the third edition, its internal architecture has made significant changes, many other intent of the detection added, now checkstyle can check the problem of class design, duplicate code, Double check bug mode such as lock.

 

Configuration: Checkstyle configuration plug-in modules applied to Java code, modules is a

Checker module is the root of the tree structure, the hierarchy under the root includes:

Filesetchecks takes a set of input files and prompts for error messages

Filters filtering audit events, including error messages

Auditlisteners Report accepted Events

Many of the checks are Treewalker filesetchecks module Submodules,treewalker separate the conversion of each Java source file into an abstract syntax tree, and then according to these submodules processing results, These submodules take turns looking at some aspect of the tree. Checkstyle gets the configuration from an XML document that specifies the hierarchical configuration and attributes of module, and if you call Checkstyle in the command-line, you need to provide a file that contains the configuration document, which is also called in Ant. The doc directory in the Checkstyle release contains a sample configuration file sun_checks.xml that configures Checkstyle to check that the code conforms to Sun's code conventions.

such as: < module name = "Checker" >
< module name = "Packagehtml"/>
< module name = "Treewalker" >
< module name = "Avoidstarimport"/>
< module name = "Constantname"/>
< module name = "Emptyblock"/>
</module >
</module >

In this configuration, the root check module includes packagehtml (check that all packages must contain package descriptions) and Treewalker (check the Java source code), and the above Checkstyle loads a class by the name of the module. There are other ways to load a module class, such as specifying a class with the full name of the package, such as <module name= "Com.puppycrawl.tools.checkstyle.TreeWalker" >.

Each module will have default attributes that you can specify if the default value does not meet your requirements, and refer to the documentation for a more detailed reference.

usage: You can perform checkstyle at the command line, such as:

Java com.puppycrawl.tools.checkstyle.main-c docs/sun_checks.xml \-R src/

Indicates that all Java source files under Src are checked, and that the configuration file is used by the Sun_checks.xml

I mainly describe the main ant configuration information listed under the call in Ant: < path id = "Checkstyle.path" >
< Fileset dir = "${lib.dir}/checkstyle-4.2" >
< include name = "**/*.jar"/>
</fileset >
</path >

< target name = "Checkstyle" >
< Taskdef resource = "Checkstyletask.properties" classpathref = "Checkstyle.path"/>
< Checkstyle config = "${lib.dir}/checkstyle-4.2/sun_checks.xml"
Failureproperty = "Checkstyle.failure"
Failonviolation = "false" >
<!--uncomment to print to console as-->
<!--formatter type= "plain"/-->
< formatter type = "xml" ToFile = "D:\checkstyle.xml"/>
< Fileset dir = "${src.dir}" >
< include name = "**/*.java"/>
</fileset >
</Checkstyle >
< style in = "d:\checkstyle.xml" out = "d:\checkstyle.html"
style = "${lib.dir}/checkstyle-4.2/contrib/checkstyle-noframes.xsl"/>
</Target >

Self-Test introduction: Checkstyle has a lot of checks, its website has the reference according to the function division, divides into the standard

Check and optional check, optional check provides Java EE aspects of the inspection, such as EJB and Localhomeinterface, and some other checks. The standard inspection covers the following areas:

Javadoc Comments and Javadoc annotation related checks, such as Javadoc annotations for checking classes, methods, etc.

naming conventions a naming convention, you can check the names of classes, methods, and so on according to regular expressions

Headers Check to see if the file starts with some lines, such as "Copyright All rights reserved"

Imports and import-related inspections, such as checking for import with "*"

Size violations A check of code block sizes, such as the number of lines of code for classes, methods, and so on

whitespace A blank check, such as Checking for tab compliance (' \ t ')

modifiers modifiers, such as the order of decorated symbols

Block Checks inspection, such as checking for empty blocks or unnecessary, invalid blocks, etc.

coding Code related to a series of checks, such as empty conditions, private variables if not changed value should be declared

Final, magic number in code, switch lacks default, etc.

class Design classes are designed to check that classes that have only private constructors should be declared final

Duplicate Code check for duplicate codes

Metrics Metrics, such as the complexity of Boolean expressions, the complexity of class dependencies, and the complexity of algorithms

Miscellaneous Other miscellaneous checks, such as whether the file ends with a new line, a todo comment, no comment main

Method

 

Extended Checkstyle: Checkstyle has three ways to expand, write checks, write filters, and write

Listeners.

Write Checks

Checkstyle There are two kinds of checks, before you start, you want to realize which kind of check, Checkstyle function is to be able to embed its modules to extend, modules can also contain modules, they can form a tree structure, The top-level module, which accesses the Checkstyle kernel directly, expands the Filesetcheck interface and is actually a module that forms the root of the tree structure, which is well understood by reading a set of input files and raising an error message. Checkstyle offers a number of filesetcheck extensions, such as Treewal

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.