RegexpHeader

Since Checkstyle 3.2

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 3.2
fileExtensions Specify the file extensions of the files to process. String[] all files 3.2
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.


<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
        "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
        "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
    <module name="RegexpHeader"/>
</module>

Example1:


package com.puppycrawl.tools.checkstyle.checks.header.regexpheader;
// 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:


<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
        "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
        "https://checkstyle.org/dtds/configuration_1_3.dtd">
<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:


// Wrong header format
package com.puppycrawl.tools.checkstyle.checks.header.regexpheader;

public class Example2 {
    // Some code
}
// violation 6 lines above 'Missing a header - not enough lines in file.'

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


<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
        "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
        "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
    <module name="RegexpHeader">
        <property name="headerFile" value="${config.folder}/copyright.header"/>
    </module>
</module>

^// Copyright \(C\) (\d\d\d\d - )?2004 MyCompany$
^// All rights reserved$

Example3:


// Copyright (C) 2004 MyCompany
// All rights reserved
package com.puppycrawl.tools.checkstyle.checks.header.regexpheader;

public class Example3 {
    // Some code
}

For regex containing ".*"


<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
        "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
        "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
    <module name="RegexpHeader">
        <property name="headerFile" value="${config.folder}/universal.header"/>
    </module>
</module>

content of "universal.header" file:


^.*

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


// Any header works here
package com.puppycrawl.tools.checkstyle.checks.header.regexpheader;
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