These checks work by applying regular expressions.
This check applies a regular expression to the names of files. Depending on the configuration, a warning is logged if a required match is not found, or if an illegal match is found.
The Checkstyle team liked this check so much that they added it to the core Checkstyle product. We feel
honored! Sadly, even though Checkstyle Addons is admittedly their inspiration, and we published 10 months ahead of
them, the fact goes unmentioned.
In order to avoid name collisions with our original, we had to rename this check from RegexpOnFilename to
RegexpOnFilenameOrg.
This check is useful for situations such as:
By default, this check flags leading and trailing spaces in file names.
The check works like this:
selection
are checked. Leave blank to include all files.regexp
is matched against each selected file name. What part of the file name it is applied to, and how the result is interpreted is governed by the check properties...
elements etc.).
unrestricted
^(?:\s+.*|.*?\s+)$
regexp
finds required or illegal matches. required
means that all selected files must match the expression. illegal
means that they must not.
illegal
true
, only the simple name of the file will be checked against the pattern specified by regexp
; if false
, the entire canonical path will be checked.
Note that this option applies only to the pattern specified by regexp
; the selection
property is always treated as if simple=false
.
true
Since this check is a FileSetCheck, it also inherits the fileExtensions
property, which may be configured independently of selection
. In that case, both properties must match (e.g. fileExtensions
and selection
, or either of the two if one is missing).
In addition to the properties, optionally adding a message
element may benefit this check to make the warning easier to understand. The message key depends on the value of the mode
option. If mode=required
, the message key regexp.filepath.required
is used. If mode=illegal
, the message key regexp.filepath.illegal
is used. The message text can make use of placeholders {0}
(the file name as used by the matcher) and {1}
(the regular expression used by the matcher).
By default, the check detects leading and trailing spaces in file names. It is recommended to still add a custom message as shown, but that’s optional.
To configure the check to ensure that Java files reside in Java source folders, not resource folders:
This check is also useful to enforce arbitrary naming conventions. In the following example, we require all HTML files in a folder html/view to start with the prefix view_
:
To configure the check to ban GIF files in favor of PNG:
The (?i)
at the start of the selection
expression turns on case insensitivity, so that .gif
, .GIF
, or even .Gif
are all matched.
Important: This check goes directly under Checker, not under TreeWalker.
This check applies a regular expression to String literals found in the source.
This is useful in order to ensure that Strings do not contain illegal content such as hard-coded host names, IP addresses, known user names, or improperly encoded characters.
This check covers single String literals that appear anywhere in the source, and it also covers expressions which consist only of String literals concatenated with +
(as those are going to be made into single String literals by the compiler).
^(?!x)x
(check disabled)
In addition to the properties, optionally adding a message
element may benefit this check to make the warning easier to understand. The message key is regexp.string
. The message text can make use of placeholders {0}
(the String literal in question, excluding quotes) and {1}
(the regular expression used by the matcher).
To check for some hard-coded host names, including an optional custom message text:
The following configuration finds hard-coded IPv4 addresses: