RegexpHeader

Since Checkstyle 6.9

Description

Checks the header of a source file against a header that contains a pattern for each line of the source header.

Properties

name description type default value since
charset Specify the character encoding to use when reading the headerFile. String the charset property of the parent <a href="https://checkstyle.org/config.html#Checker">Checker</a> module 5.0
fileExtensions Specify the file extensions of the files to process. String[] all files 6.9
header Define the required header specified inline. Individual header lines must be separated by the string "\n" (even on platforms with a different line separator). For header lines containing "\n\n" checkstyle will forcefully expect an empty line to exist. See examples below. Regular expressions must not span multiple lines. String null 5.0
headerFile Specify the name of the file containing the required header. URI null 3.2
multiLines Specify the line numbers to repeat (zero or more times). int[] {} 3.4

Examples

To configure the check such that no violations arise. Default values of properties are used.


<module name="Checker">
  <module name="RegexpHeader"/>
</module>
        

Example1:


package com.puppycrawl.tools.checkstyle.checks.header.header;
// OK, as by default there is not specific header defined to validate it presence
public class Example1 { }
        

To configure the check to use header file "java.header" and 10 and 13 multi-lines:


<module name="Checker">
  <module name="RegexpHeader">
    <property name="headerFile" value="${config.folder}/java.header"/>
    <property name="multiLines" value="10, 13"/>
  </module>
</module>
        

content of "java.header" file:


///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
// Copyright (C) 20XX-20XX the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
///////////////////////////////////////////////////////////////////////////////////////////////
        

Example2:


package com.puppycrawl.tools.checkstyle.checks.header.header;
/* violation on first line 'Line does not match expected header line of' */
// because headerFile is bigger then target java file
public class Example2 { }
        

To configure the check to verify that each file starts with the header


<module name="Checker">
  <module name="RegexpHeader">
    <property
      name="header"
      value="^// Copyright \(C\) (\d\d\d\d -)? 2004 MyCompany$
      \n^// All rights reserved$"/>
  </module>
</module>
        

Example3:


package com.puppycrawl.tools.checkstyle.checks.header.regexpheader;
/* violation on first line 'Line does not match expected header line' */
public class Example3 { }
        

For regex containing ".*"


<module name="Checker">
  <module name="RegexpHeader">
    <property
      name="header"
      value="^.*"/>
  </module>
</module>
        

".*" will match all lines and expect no violation. For example -


package com.puppycrawl.tools.checkstyle.checks.header.regexpheader;
//OK, as regex value matches all lines
public class Example4 { }
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.header

Parent Module

Checker