Since Checkstyle 3.5
Rationale: Code duplication makes maintenance more difficult, so it can be better to replace the multiple occurrences with a constant.
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 |
To configure the check:
<module name="Checker"> <module name="TreeWalker"> <module name="MultipleStringLiterals"/> </module> </module>
Example:
public class Example1 { String a = "StringContents"; // violation, "StringContents" occurs twice String a1 = "unchecked"; @SuppressWarnings("unchecked") // OK, duplicate strings are ignored in annotations public void myTest() { String a2 = "StringContents"; String a3 = "DuoString" + "DuoString"; // violation, "DuoString" occurs twice String a4 = "SingleString"; 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 Example2 { String a = "StringContents"; // OK, two occurrences are allowed String a1 = "unchecked"; @SuppressWarnings("unchecked") // OK, duplicate strings are ignored in annotations public void myTest() { String a2 = "StringContents"; String a3 = "DuoString" + "DuoString"; // OK, two occurrences are allowed String a4 = "SingleString"; 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 Example3 { String a = "StringContents"; // violation, "StringContents" occurs twice String a1 = "unchecked"; @SuppressWarnings("unchecked") // OK, duplicate strings are ignored in annotations public void myTest() { String a2 = "StringContents"; String a3 = "DuoString" + "DuoString"; // violation, "DuoString" occurs twice String a4 = "SingleString"; 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 Example4 { String a = "StringContents"; // violation, "StringContents" occurs twice String a1 = "unchecked"; // // violation, "unchecked" occurs twice @SuppressWarnings("unchecked") public void myTest() { String a2 = "StringContents"; String a3 = "DuoString" + "DuoString"; // violation, "DuoString" occurs twice String a4 = "SingleString"; String a5 = ", " + ", " + ", "; // violation, ", " occurs three times } }
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.coding