Regexp v2.1.1

These checks work by applying regular expressions.

RegexpOnFilenameOrg

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. :-1:
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:

  1. Select the files to check. Only files which match the regular expression given in selection are checked. Leave blank to include all files.
  2. The expression given in 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.

Properties

selection regular expression
Limits the check to files whose canonical path name contains the given pattern. The canonical path is the simplest possible absolute path, including the file name (no .. elements etc.). unrestricted
regexp regular expression
The regular expression applied to the file name. ^(?:\s+.*|.*?\s+)$
mode Mode
whether regexp finds required or illegal matches. required means that all selected files must match the expression. illegal means that they must not. illegal
simple Boolean
If 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).

Custom Messages

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).

Examples

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.

<module name="RegexpOnFilenameOrg">
  <message key="regexp.filepath.illegal" value="Filename ''{0}'' contains leading or trailing spaces."/>
</module>

To configure the check to ensure that Java files reside in Java source folders, not resource folders:

<module name="RegexpOnFilenameOrg">
  <property name="selection" value="\.java$"/>
  <property name="regexp" value="[\\/]src[\\/](?:test|main)[\\/]java[\\/]"/>
  <property name="mode" value="required"/>
  <property name="simple" value="false"/>
  <message key="regexp.filepath.required"
      value="The Java file ''{0}'' must reside in a Java source folder."/>
</module>

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_:

<module name="RegexpOnFilenameOrg">
  <property name="selection" value="[\\/]src[\\/]main[\\/]resources[\\/]html[\\/]views[\\/].+?\.html$"/>
  <property name="regexp" value="^view_.*"/>
  <property name="mode" value="required"/>
  <message key="regexp.filepath.required" value="Name of ''{0}'' must start with ''view_''."/>
</module>

To configure the check to ban GIF files in favor of PNG:

<module name="RegexpOnFilenameOrg">
  <property name="selection" value="(?i)\.gif$"/>
  <property name="regexp" value="."/>
  <message key="regexp.filepath.illegal" value="''{0}'' must be in PNG format, not GIF."/>
</module>

The (?i) at the start of the selection expression turns on case insensitivity, so that .gif, .GIF, or even .Gif are all matched.

Parent Module

Important: This check goes directly under Checker, not under TreeWalker.

Checker

RegexpOnString

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).

Properties

regexp regular expression
The regular expression applied to each String literal found in a source file. ^(?!x)x (check disabled)

Custom Messages

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).

Examples

To check for some hard-coded host names, including an optional custom message text:

<module name="RegexpOnString">
  <property name="regexp" value="(?:localhost|\.mydomain\.com)"/>
  <message key="regexp.string" value="String {0} appears to contain a hard-coded hostname."/>
</module>

The following configuration finds hard-coded IPv4 addresses:

<module name="RegexpOnString">
  <property name="regexp" value="\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b"/>
  <message key="regexp.string" value="String {0} appears to contain a hard-coded IP address."/>
</module>

Parent Module

TreeWalker