MultipleStringLiterals

Since Checkstyle 3.5

Description

Checks for multiple occurrences of the same string literal within a single file.

Rationale: Code duplication makes maintenance more difficult, so it can be better to replace the multiple occurrences with a constant.

Properties

name description type default value since
allowedDuplicates Specify the maximum number of occurrences to allow without generating a warning. int 1 3.5
ignoreOccurrenceContext Specify token type names where duplicate strings are ignored even if they don't match ignoredStringsRegexp. This allows you to exclude syntactical contexts like annotations or static initializers from the check. subset of tokens TokenTypes ANNOTATION 4.4
ignoreStringsRegexp Specify RegExp for ignored strings (with quotation marks). Pattern "^""$" 4.0

Examples

To configure the check:

<module name="Checker">
  <module name="TreeWalker">
    <module name="MultipleStringLiterals"/>
  </module>
</module>
        

Example:

public class MyClass {
  String a = "StringContents";
  String a1 = "unchecked";
  @SuppressWarnings("unchecked") // OK, duplicate strings are ignored in annotations
  public void myTest() {
    String a2 = "StringContents"; // violation, "StringContents" occurs twice
    String a3 = "DoubleString" + "DoubleString"; // violation, "DoubleString" occurs twice
    String a4 = "SingleString"; // OK
    String a5 = ", " + ", " + ", "; // violation, ", " occurs three times
  }
}
        

To configure the check so that it allows two occurrences of each string:

<module name="Checker">
  <module name="TreeWalker">
    <module name="MultipleStringLiterals">
      <property name="allowedDuplicates" value="2"/>
    </module>
  </module>
</module>
        

Example:

public class MyClass {
  String a = "StringContents";
  String a1 = "unchecked";
  @SuppressWarnings("unchecked") // OK, duplicate strings are ignored in annotations
  public void myTest() {
    String a2 = "StringContents"; // OK, two occurrences are allowed
    String a3 = "DoubleString" + "DoubleString"; // OK, two occurrences are allowed
    String a4 = "SingleString"; // OK
    String a5 = ", " + ", " + ", "; // violation, three occurrences are NOT allowed
  }
}
        

To configure the check so that it ignores ", " and empty strings:

<module name="Checker">
  <module name="TreeWalker">
    <module name="MultipleStringLiterals">
      <property name="ignoreStringsRegexp"
        value='^(("")|(", "))$'/>
    </module>
  </module>
</module>
        

Example:

public class MyClass {
  String a = "StringContents";
  String a1 = "unchecked";
  @SuppressWarnings("unchecked") // OK, duplicate strings are ignored in annotations
  public void myTest() {
    String a2 = "StringContents"; // violation, "StringContents" occurs twice
    String a3 = "DoubleString" + "DoubleString"; // violation, "DoubleString" occurs twice
    String a4 = "SingleString"; // OK
    String a5 = ", " + ", " + ", "; // OK, multiple occurrences of ", " are allowed
  }
}
        

To configure the check so that it flags duplicate strings in all syntactical contexts, even in annotations like @SuppressWarnings("unchecked"):

<module name="Checker">
  <module name="TreeWalker">
    <module name="MultipleStringLiterals">
      <property name="ignoreOccurrenceContext" value=""/>
    </module>
  </module>
</module>
        

Example:

public class MyClass {
  String a = "StringContents";
  String a1 = "unchecked";
  @SuppressWarnings("unchecked") // violation, "unchecked" occurs twice
  public void myTest() {
    String a2 = "StringContents"; // violation, "StringContents" occurs twice
    String a3 = "DoubleString" + "DoubleString"; // violation, "DoubleString" occurs twice
    String a4 = "SingleString"; // OK
    String a5 = ", " + ", " + ", "; // violation, ", " occurs three times
  }
}
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.coding

Parent Module

TreeWalker