分工

分工

永潮:

谈瑞:

PMD

checkstyle

原理

introduction

The explorative study should cover the background, objectives, functionalities and features, usage,
and experimentations with the tool.

checkstyle

background

Checkstyle[1] is a static code analysis tool used in software development for checking if Java)source code complies with coding rules.

Originally developed by Oliver Burn back in 2001, the project is maintained by a team of developers from around the world.

The current stable release is version 8.17[2] which is targeted at the Java 8 language.

Checkstyle is highly configurable and can be made to support almost any coding standard. An example configuration files are supplied supporting the Sun Code Conventions, Google Java Style.

A good example of a report that can be produced using Checkstyle and Maven can be seen here .

Current site contains documentation only for latest, released version. We only support this latest version. We highly recommend read latest documentation first.
However, you might need documentation for previous releases because you are still using an older version and latest version describe some features that does not work for you.
You can find documentation for most old versions using a URL format like http://checkstyle.sourceforge.net/version/X.X where “X.X” is the version number.
Example: http://checkstyle.sourceforge.net/version/6.18 for version 6.18.

objectives

Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. It automates the process of checking Java code to spare humans of this boring (but important) task. This makes it ideal for projects that want to enforce a coding standard.

functionalities

Checks:

  • annotations

    • Check location of annotation on language elements. By default, Check enforce to locate annotations immediately after documentation block and before target element, annotation should be located on separate line from target element.
  • block checks

    • Finds nested blocks, i.e. blocks that are used freely in the code.
  • class design

    • The check finds classes that are designed for extension (subclass creation).

      Nothing wrong could be with founded classes. This check makes sense only for library projects (not application projects) which care of ideal OOP-design to make sure that class works in all cases even misusage. Even in library projects this check most likely will find classes that are designed for extension by somebody. User needs to use suppressions extensively to got a benefit from this check, and keep in suppressions all confirmed/known classes that are deigned for inheritance intentionally to let the check catch only new classes, and bring this to team/user attention.

  • coding

    • Checks that array initialization contains a trailing comma.
    • The check demands a comma at the end if neither left nor right curly braces are on the same line as the last element of the array.
  • headers

  • imports

  • javadoc comments

  • metrics

  • miscellaneous

  • modifiers

  • naming conventions

  • regexp

  • Size violations

  • whitespace

CheckStyle默认提供一下主要检查内容:
  •Javadoc注释
  •命名约定
  •标题
  •Import语句
  •体积大小
  •空白
  •修饰符
  •块
  •代码问题
  •类设计
  •混合检查(包活一些有用的比如非必须的 System.out和printstackTrace)

The Standard Checkstyle Checks are applicable to general Java coding style and require no external libraries. The standard checks are included in the base distribution.

The site navigation menu lets you browse the individual checks by functionality.

Checkstyle provides many checks that you can apply to your source code. Below is an alphabetical reference, the site navigation menu provides a reference organized by functionality. The whole list of what check style can check about is in the website “http://checkstyle.sourceforge.net/checks.html“.

feartures

Checkstyle can check many aspects of your source code.

It can find class design problems, method design problems. It also has the ability to check code layout and formatting issues.

For a detailed list of available checks please refer to the Checks page.

The programming style adopted by a software development project can help to ensure that the code complies with good programming practices which improves the quality, readability, re-usability of the code and may reduce the cost of development. The checks performed by Checkstyle are mainly limited to the presentation of the code. These checks do not confirm the correctness or completeness of the code.

usage

This section documents the out-of-box supported methods for running Checkstyle.

Note that there are loads of plug-ins for all the IDEs out there.

here shows the steps of running checkstyle using the command line tool

1
2
3
4
5
6
7
java -D<property>=<value>  \
com.puppycrawl.tools.checkstyle.Main \
-c <configurationFile> \
[-f <format>] [-p <propertiesFile>] [-o <file>] \
[-s <line:column>] [-g | --generate-xpath-suppression] [--tabWidth <length>] \
[-t | --tree] [-T | --treeWithComments] [-J | treeWithJavadoc] [-j | --javadocTree] \
[-V | --version] [-h | --help] file...

Checkstyle will process the specified files and by default report errors to standard out in plain format. Checkstyle requires a configuration XML filethat configures the checks to apply. Command line options are:

  • -c configurationFile - specifies the location of the file that defines the configuration modules. The location can either be a filesystem location, or a name passed to the ClassLoader.getResource() method.

  • -f format - specify the output format. Options are "plain" for the DefaultLogger and "xml" for the XMLLogger. Defaults to "plain".

  • -p propertiesFile - specify a properties file to use.

  • -o file - specify the file to output to.

  • -s line:column - print xpath suppressions at the file’s line and column position. Line and column numbers are integer values separated by a :, specify location in the file the suppression should be generated for. The option cannot be used with other options and requires exactly one file to run on to be specified. ATTENTION: generated result will have few queries, joined by pipe(|). Together they will match all AST nodes on specified line and column. You need to choose only one and recheck that it works. Usage of all of them is also ok, but might result in undesirable matching and suppress other issues.

  • -g,--generate-xpath-suppression - generate to output a suppression.xml to use to suppress all violations from user’s config. Instead of printing every violation, all errors will be catched and single suppressions.xml file will be printed out. Used only with -coption.

  • --tabWidth length - set the length of the tab character. Used only with -s and -gxs options. Default value is 8.

  • -t, --tree - print Abstract Syntax Tree(AST) of the checked file. The option cannot be used other options and requires exactly one file to run on to be specified.

  • -T, --treeWithComments - print Abstract Syntax Tree(AST) with comment nodes of the checked file. The option cannot be used with other options and requires exactly one file to run on to be specified.

  • -J, --treeWithJavadoc - print Abstract Syntax Tree(AST) with Javadoc nodes and comment nodes of the checked file. Attention that line number and columns will not be the same as it is a file due to the fact that each javadoc comment is parsed separately from java file. The option cannot be used with other options and requires exactly one file to run on to be specified.

  • -j, —javadocTree

- print Parse Tree of the Javadoc comment. The file have to contain

only Javadoc comment content

without including ‘/*’ and ‘/‘ at the beginning and at the end respectively. For example: MyTestFile.javadoc

1
2
* Test method.
* @return true

The option cannot be used other options and requires exactly one file to run on to be specified.

  • -d, --debug - Print all debug logging of CheckStyle utility.

  • -e, --exclude excludedPath - Directory/file to exclude from CheckStyle. The path can be the full, absolute path, or relative to the current path. Multiple excludes are allowed.

  • -x, --exclude-regexp excludedPathPattern - Directory/file pattern to exclude from CheckStyle. Multiple excludes are allowed.

  • -V, --version - print product version and exit. Any other option is ignored.

  • -h, --help - print usage help message and exit. Any other option is ignored.

  • --executeIgnoredModules - Allows ignored modules to be run.

Note that the -n packageNamesFile option has been dropped for Checkstyle 5.0, because of significant changes regarding package name file handling. See for details.

Set the properties for expanded property values by either by assigning system properties using the -D<property>=<value> arguments to java or specifying a property file using the -p option. If a property file is specified, the system properties are ignored.

CLI is implemented by means of picocli library. Our CLI supports definition of arguments in file by means of AtFiles feature and also command line completion in Bash or ZSH Unix shells, see how to make ithere.

checkstyle in IDEA

  1. install
    1. go to Settings ->plugins->browse repositories, search checkstyle plugin and click install button
  2. use
    1. Add own configuration file

experimentation

给咱来个🍰,啾咪