Checkstyle

 
 
Checkstyle is an open-source development tool to help programmers write Java code that adheres to a coding standard. It automates the process of checking Java code. It is ideal for projects that want to enforce a coding standard. Checkstyle is a code checker and not a code fixer. If you need a code fixer you could use Jalopy.

Checkstyle is highly configurable and can be made to support almost any coding standard. Configuration files are supplied to support well known coding standards such as the Sun Code Conventions for the Java Programming Language.

Checkstyle is released under the GNU Lesser General Public License (LGPL).

Checkstyle can check for:
  • Javadoc Comments
  • Naming Conventions
  • Headers
  • Imports
  • Size Violations
  • Whitespace
  • Modifiers
  • Blocks
  • Miscellaneous Checks (including some useful checks like unnecessary System.out and printstackTrace)
More information about Checkstyle can be found at:
http://checkstyle.sourceforge.net/

Plug-ins are written to integrate Checkstyle with an IDE, such as Eclipse / WebSphere Studio Application Developer (WSAD).

The latest Eclipse Checkstyle plug-in version can be downloaded from:
http://sourceforge.net/projects/eclipse-cs/







Installing Eclipse Checkstyle v3.1 on WSAD



Information
none

Operating system used
Windows XP Home Edition Version 5.1 SP 2

Software prerequisites
WebSphere Studio Application Developer

Procedure
  1. Download and unzip com.atlassw.tools.eclipse.checkstyle_3.1.1.zip

  2. If you had a pre 3.1.0 version of the Eclipse Checkstyle plug-in installed you will need to remove it before installing the new plug-in. Also, remove any .checkstyle files from the root of your projects, these files are not compatible with this version of the plug-in.
    See: C:\Documents and Settings\<user>\
    My documents\IBM\wsad\workspace\.metadata\.plugins.
    After removing these files be sure and refresh your workspace so that Eclipse is aware that they have been removed.

  3. Copy the new com.atlassw.tools.eclipse.checkstyle_<version> plug-in folder to ..\WebSphere Studio\eclipse\plugins\

  4. Restart WSAD if its already running.

  5. To enable Checkstyle on a project right-click the project and select the project's properties.

  6. Select the project's Checkstyle properties in the left column and enable the check-box and select OK.

    Enable checkstyle

  7. As your code is compiled Checkstyle reports any problems it finds in the workbench Tasks window along with placing a marker next to the line with the problem in the source code editor. To go to the line of code with the problem just double click on the message in the Tasks window.

    Checkstyle Task window



  8. Often you don't want all files within a project inspected. You can filter these files out so they are not inspected by creating a file filter. A file filter is a regular expression that is used to exclude file. The regular expression operates on the complete file name within the project, so folder names can be used in the expression.

    In the example above the file IntSessionWrapper is checked. To disable the inspection for this file, add the filename in the File Filters window.

    Checkstyle filter

  9. The Checkstyle core engine can be configured to check for a number of different items in your code. The Checkstyle plug-in can be configured through the menubar: Window | Preferences screen.

    Checkstyle preferences

  10. The full set of properties that can be specified can be found in the Checkstyle configuration page (see ..\WebSphere Studio\eclipse\plugins\
    com.atlassw.tools.eclipse.checkstyle_2.1.0\doc\csdocs\config.html) of the core engine documentation (see ..\WebSphere Studio\eclipse\plugins\
    com.atlassw.tools.eclipse.checkstyle_2.1.0\doc\csdocs\index.html).

  11. Using the Import/Export buttons the Checkstyle configuration properties can be imported/exported to/from a properties file. This file can then be distributed amoung a team of developers or used to run the Checkstyle core from the command line.