Class MultiFileRegexpHeaderCheck
java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck
com.puppycrawl.tools.checkstyle.checks.header.MultiFileRegexpHeaderCheck
- All Implemented Interfaces:
Configurable
,Contextualizable
,ExternalResourceHolder
,FileSetCheck
public class MultiFileRegexpHeaderCheck
extends AbstractFileSetCheck
implements ExternalResourceHolder
Checks the header of a source file against multiple header files that contain a
pattern for each line of the source header.
-
Property
fileExtensions
- Specify the file extensions of the files to process. Type isjava.lang.String[]
. Default value is""
. -
Property
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. Type isjava.lang.String
. Default value isnull
.
Parent is com.puppycrawl.tools.checkstyle.Checker
Violation Message Keys:
-
multi.file.regexp.header.mismatch
-
multi.file.regexp.header.missing
- Since:
- 10.24.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
Metadata holder for a header file, storing its URI, compiled patterns, and line contents.private static final class
Represents the result of a header match check, containing information about any mismatch.Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Pattern
Compiled regex pattern for a blank line.private static final String
Regex pattern for a blank line.private String
Specify a comma-separated list of files containing the required headers.private final List<MultiFileRegexpHeaderCheck.HeaderFileMetadata>
List of metadata objects for each configured header file, containing patterns and line contents.static final int
Constant indicating that no header line mismatch was found.static final String
A key is pointing to the warning message text in "messages.properties" file.static final String
A key is pointing to the warning message text in "messages.properties" file. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static MultiFileRegexpHeaderCheck.MatchResult
createMismatchResult
(MultiFileRegexpHeaderCheck.HeaderFileMetadata headerFile, FileText fileText, int mismatchLine) Creates a MatchResult for a mismatch case.Returns a comma-separated string of all configured header file paths.Returns a set of external configuration resource locations which are used by the module.Reads all lines from the specified header file URI.private static MultiFileRegexpHeaderCheck.MatchResult
matchHeader
(FileText fileText, MultiFileRegexpHeaderCheck.HeaderFileMetadata headerFile) Analyzes if the file text matches the header file patterns and generates a detailed result.protected void
processFiltered
(File file, FileText fileText) Called to process a file that matches the specified file extensions.void
setHeaderFiles
(String... headerFiles) Setter to specify a comma-separated list of files containing the required headers.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck
addViolations, beginProcessing, destroy, finishProcessing, fireErrors, getFileContents, getFileExtensions, getMessageDispatcher, getTabWidth, getViolations, init, log, log, process, setFileContents, setFileExtensions, setMessageDispatcher, setTabWidth
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
Methods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.puppycrawl.tools.checkstyle.api.Configurable
configure
Methods inherited from interface com.puppycrawl.tools.checkstyle.api.Contextualizable
contextualize
-
Field Details
-
MISMATCH_CODE
Constant indicating that no header line mismatch was found.- See Also:
-
MSG_HEADER_MISSING
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_HEADER_MISMATCH
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
EMPTY_LINE_PATTERN
Regex pattern for a blank line.- See Also:
-
BLANK_LINE
Compiled regex pattern for a blank line. -
headerFilesMetadata
List of metadata objects for each configured header file, containing patterns and line contents. -
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.
-
-
Constructor Details
-
MultiFileRegexpHeaderCheck
public MultiFileRegexpHeaderCheck()
-
-
Method Details
-
setHeaderFiles
Setter to 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.- Parameters:
headerFiles
- comma-separated list of header files- Throws:
IllegalArgumentException
- if headerFiles is null or empty- Since:
- 10.24.0
-
getConfiguredHeaderPaths
Returns a comma-separated string of all configured header file paths.- Returns:
- A comma-separated string of all configured header file paths, or an empty string if no header files are configured or none have valid paths.
-
getExternalResourceLocations
Description copied from interface:ExternalResourceHolder
Returns a set of external configuration resource locations which are used by the module. ATTENTION! If 'getExternalResourceLocations()' return null, there will beNullPointerException
inChecker
. Such behaviour will signal that your module (check or filter) is designed incorrectly. It makes sense to return an empty set from 'getExternalResourceLocations()' only for composite modules likeTreeWalker
.- Specified by:
getExternalResourceLocations
in interfaceExternalResourceHolder
- Returns:
- a set of external configuration resource locations which are used by the module.
-
processFiltered
Description copied from class:AbstractFileSetCheck
Called to process a file that matches the specified file extensions.- Specified by:
processFiltered
in classAbstractFileSetCheck
- Parameters:
file
- the file to be processedfileText
- the contents of the file.
-
matchHeader
private static MultiFileRegexpHeaderCheck.MatchResult matchHeader(FileText fileText, MultiFileRegexpHeaderCheck.HeaderFileMetadata headerFile) Analyzes if the file text matches the header file patterns and generates a detailed result.- Parameters:
fileText
- the text of the file being checkedheaderFile
- the header file metadata to check against- Returns:
- a MatchResult containing the result of the analysis
-
createMismatchResult
private static MultiFileRegexpHeaderCheck.MatchResult createMismatchResult(MultiFileRegexpHeaderCheck.HeaderFileMetadata headerFile, FileText fileText, int mismatchLine) Creates a MatchResult for a mismatch case.- Parameters:
headerFile
- the header file metadatafileText
- the text of the file being checkedmismatchLine
- the line number of the mismatch (0-based)- Returns:
- a MatchResult representing the mismatch
-
getLines
Reads all lines from the specified header file URI.- Parameters:
headerFile
- path to the header file (for error messages)uri
- URI of the header file- Returns:
- list of lines read from the header file
- Throws:
IllegalArgumentException
- if the file cannot be read or is empty
-