com.puppycrawl.tools.checkstyle.checks
Class RegexpHeaderCheck

java.lang.Object
  extended bycom.puppycrawl.tools.checkstyle.api.AutomaticBean
      extended bycom.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
          extended bycom.puppycrawl.tools.checkstyle.api.Check
              extended bycom.puppycrawl.tools.checkstyle.checks.HeaderCheck
                  extended bycom.puppycrawl.tools.checkstyle.checks.RegexpHeaderCheck
All Implemented Interfaces:
Configurable, Contextualizable

public class RegexpHeaderCheck
extends HeaderCheck

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

Rationale: In some projects checking against a fixed header is not sufficient (see HeaderCheck), e.g. the header might require a copyright line where the year information is not static.

For example, consider the following header file:

 line 1: /{71}
 line 2: // checkstyle:
 line 3: // Checks Java source code for adherence to a set of rules\.
 line 4: // Copyright \(C\) \d\d\d\d  Oliver Burn
 line 5: // Last modification by \$Author.*\$
 line 6: /{71}
 

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.

An example of how to configure the check to use header file "java.header" and ignore lines 4 and 5 is:

 <module name="RegexpHeader">
    <property name="headerFile" value="java.header"/>
    <property name="ignoreLines" value="4, 5"/>
 </module>
 

Author:
Lars Kühne

Constructor Summary
RegexpHeaderCheck()
           
 
Method Summary
protected  boolean isMatch(int aLineNumber)
          Checks if a code line matches the required header line.
 void setHeaderFile(java.lang.String aFileName)
          Sets the file that contains the header to check against.
 
Methods inherited from class com.puppycrawl.tools.checkstyle.checks.HeaderCheck
beginTree, finishLocalSetup, getDefaultTokens, getHeaderLines, setIgnoreLines
 
Methods inherited from class com.puppycrawl.tools.checkstyle.api.Check
destroy, finishTree, getAcceptableTokens, getClassLoader, getFileContents, getLines, getRequiredTokens, getTabWidth, getTokenNames, init, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens, visitToken
 
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
getMessageBundle, getSeverity, getSeverityLevel, log, log, log, log, log, log, setSeverity
 
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, getConfiguration, setupChild
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RegexpHeaderCheck

public RegexpHeaderCheck()
Method Detail

setHeaderFile

public void setHeaderFile(java.lang.String aFileName)
                   throws ConversionException
Sets the file that contains the header to check against.

Overrides:
setHeaderFile in class HeaderCheck
Parameters:
aFileName - the file that contains the header to check against.
Throws:
ConversionException - if the file cannot be loaded or one line is not a regexp.

isMatch

protected boolean isMatch(int aLineNumber)
Description copied from class: HeaderCheck
Checks if a code line matches the required header line.

Overrides:
isMatch in class HeaderCheck
Parameters:
aLineNumber - the linenumber to check against the header
Returns:
true if and only if the line matches the required header line
See Also:
HeaderCheck

Back to the Checkstyle Home Page