MultiFileRegexpHeader

Since Checkstyle 10.24.0

Description

Checks the header of a source file against multiple header files that contain a pattern for each line of the source header.

Properties

name description type default value since
fileExtensions Specify the file extensions of the files to process. String[] all files 10.24.0
headerFiles Specify a comma-separated list of files containing the required headers. If a file's header matches none, the violation references the first file in this list. Users can order files to set a preferred header for such reporting. String null 10.24.0

Examples

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


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

Example1:


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

To configure the check to use multiple header files:


<?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="MultiFileRegexpHeader">
        <property name="fileExtensions" value="java"/>
        <property name="headerFiles" value="${config.folder}/java.header, ${config.folder}/apache.header"/>
    </module>
</module>

content of java.header file:


// Checkstyle - Java code style checker. Copyright (C) 20XX-20XX the authors.
// Licensed under the LGPL v2.1 or later. See LICENSE for details.
// Distributed without warranty; see the GNU LGPL for more info.

content of apache.header file:


^/\* Licensed to the ASF under the Apache License, Version 2\.0 \(see NOTICE\)\.
^\* You may not use this file except in compliance:
^\* http://www\.apache\.org/licenses/LICENSE-2\.0 \(AS IS, no warranties/conditions\)\. \*/

Example2:


package com.puppycrawl.tools.checkstyle.checks.header.multifileregexpheader;
/* violation on first line 'Header mismatch, expected line content was' */
// because headerFile is bigger then target java file
public class Example2 { }

To configure the check to verify that each file starts with one of multiple headers.


<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
        "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
        "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
    <module name="MultiFileRegexpHeader">
        <property name="headerFiles"
                  value="${config.folder}/java.header, ${config.folder}/apache.header"/>
    </module>
</module>

Example3:


package com.puppycrawl.tools.checkstyle.checks.header.multifileregexpheader;
/* violation on first line 'Header mismatch, expected line content was' */
public class Example3 { }

For a configuration where any header is accepted:


<?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="MultiFileRegexpHeader">
    <property
      name="headerFiles"
      value="${config.folder}/universal.header"/>
  </module>
</module>

content of universal.header file:


^.*

".*" matches all lines, so no violations are expected.


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