Checks that a source file begins with a specified header. Property
headerFile specifies a file that contains
the required header. Alternatively, the header specification can be
set directly in the header property
without the need for an external file.
Property ignoreLines specifies the line
numbers to ignore when matching lines in a header file. This
property is very useful for supporting headers that contain
copyright dates. For example, consider the following header:
Since the year information will change over time, you can tell
Checkstyle to ignore line 4 by setting property ignoreLines to 4.
| name | description | type | default value |
|---|---|---|---|
| headerFile | name of the file containing the required header | string | null |
| charset | character encoding to use when reading the headerFile | string | the charset property of the parent Checker module |
| header |
the required header specified inline. Individual header lines
must be separated by the string "\n" (even on platforms with a
different line separator), see examples below.
|
string | null |
| ignoreLines | line numbers to ignore | list of integers | {} |
| fileExtensions | file type extension of files to process | String Set | {} |
To configure the check to use header file "java.header" and ignore lines 2, 3, and 4 and only process Java files:
To configure the check to verify that each file starts with the header
without the need for an external header file:
com.puppycrawl.tools.checkstyle.checks.header
Checks the header of a source file against a header that contains a regular expression for each line of the source header.
Rationale: In some projects checking against a fixed header is not sufficient, e.g. the header might require a copyright line where the year information is not static.
For example, consider the following header:
Lines 1 and 6 demonstrate a more compact notation for 71 '/' characters. Line 4 enforces that the copyright notice includes a four digit year. Line 5 is an example how to enforce revision control keywords in a file header. Lines 12-14 is a template for javadoc (line 13 is so complicated to remove conflict with and of javadoc comment).
Different programming languages have different comment syntax rules, but all of them start a comment with a non-word character. Hence you can often use the non-word character class to abstract away the concrete comment syntax and allow checking the header for different languages with a single header definition. For example, consider the following header specification (note that this is not the full Apache license header):
Lines 1 and 2 leave room for technical header lines, e.g. the "#!/bin/sh" line in Unix shell scripts, or the XML file header of XML files. Set the multiline property to "1, 2" so these lines can be ignored for file types where they do no apply. Lines 3 through 6 define the actual header content. Note how lines 2, 4 and 5 use escapes for characters that have special regexp semantics.
| name | description | type | default value |
|---|---|---|---|
| headerFile | name of the file containing the required header | string | null |
| charset | character encoding to use when reading the headerFile | string | the charset property of the parent Checker module |
| header |
the required header specified inline. Individual header lines
must be separated by the string "\n" (even on platforms with a
different line separator), and regular expressions must not span
multiple lines.
|
string | null |
| multiLines | line numbers to repeat (zero or more times) | list of integers | {} |
| fileExtensions | file type extension of files to process | String Set | {} |
To configure the check to use header file "java.header" and 10 and 13 muli-lines:
To configure the check to verify that each file starts with the header
without the need for an external header file:
Note: ignoreLines property has been
removed from this check to simplify it. To make some line optional
use "^.*$" regexp for this line.
com.puppycrawl.tools.checkstyle.checks.header