Class MultipleStringLiteralsCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.puppycrawl.tools.checkstyle.checks.coding.MultipleStringLiteralsCheck
-
- All Implemented Interfaces:
Configurable,Contextualizable
public class MultipleStringLiteralsCheck extends AbstractCheck
Checks for multiple occurrences of the same string literal within a single file.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classMultipleStringLiteralsCheck.StringInfoThis class contains information about where a string was found.
-
Field Summary
Fields Modifier and Type Field Description private intallowedDuplicatesThe allowed number of string duplicates in a file before an error is generated.private java.util.BitSetignoreOccurrenceContextMarks the TokenTypes where duplicate strings should be ignored.private java.util.regex.PatternignoreStringsRegexpPattern for matching ignored strings.static java.lang.StringMSG_KEYA key is pointing to the warning message text in "messages.properties" file.private java.util.Map<java.lang.String,java.util.List<MultipleStringLiteralsCheck.StringInfo>>stringMapThe found strings and their positions.
-
Constructor Summary
Constructors Constructor Description MultipleStringLiteralsCheck()Construct an instance with default values.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbeginTree(DetailAST rootAST)Called before the starting to process a tree.voidfinishTree(DetailAST rootAST)Called after finished processing a tree.int[]getAcceptableTokens()The configurable token set.int[]getDefaultTokens()Returns the default token a check is interested in.int[]getRequiredTokens()The tokens that this check must be registered for.private booleanisInIgnoreOccurrenceContext(DetailAST ast)Analyses the path from the AST root to a given AST for occurrences of the token types inignoreOccurrenceContext.voidsetAllowedDuplicates(int allowedDuplicates)Sets the maximum allowed duplicates of a string.voidsetIgnoreOccurrenceContext(java.lang.String... strRep)Adds a set of tokens the check is interested in.voidsetIgnoreStringsRegexp(java.util.regex.Pattern ignoreStringsRegexp)Sets regular expression pattern for ignored strings.voidvisitToken(DetailAST ast)Called to process a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
destroy, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_KEY
public static final java.lang.String MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
stringMap
private final java.util.Map<java.lang.String,java.util.List<MultipleStringLiteralsCheck.StringInfo>> stringMap
The found strings and their positions.<String, ArrayList>, with the ArrayList containing StringInfo objects.
-
ignoreOccurrenceContext
private final java.util.BitSet ignoreOccurrenceContext
Marks the TokenTypes where duplicate strings should be ignored.
-
allowedDuplicates
private int allowedDuplicates
The allowed number of string duplicates in a file before an error is generated.
-
ignoreStringsRegexp
private java.util.regex.Pattern ignoreStringsRegexp
Pattern for matching ignored strings.
-
-
Method Detail
-
setAllowedDuplicates
public void setAllowedDuplicates(int allowedDuplicates)
Sets the maximum allowed duplicates of a string.- Parameters:
allowedDuplicates- The maximum number of duplicates.
-
setIgnoreStringsRegexp
public final void setIgnoreStringsRegexp(java.util.regex.Pattern ignoreStringsRegexp)
Sets regular expression pattern for ignored strings.- Parameters:
ignoreStringsRegexp- regular expression pattern for ignored strings
-
setIgnoreOccurrenceContext
public final void setIgnoreOccurrenceContext(java.lang.String... strRep)
Adds a set of tokens the check is interested in.- Parameters:
strRep- the string representation of the tokens interested in
-
getDefaultTokens
public int[] getDefaultTokens()
Description copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
TokenTypes
-
getAcceptableTokens
public int[] getAcceptableTokens()
Description copied from class:AbstractCheckThe configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
isInIgnoreOccurrenceContext
private boolean isInIgnoreOccurrenceContext(DetailAST ast)
Analyses the path from the AST root to a given AST for occurrences of the token types inignoreOccurrenceContext.- Parameters:
ast- the node from where to start searching towards the root node- Returns:
- whether the path from the root node to ast contains one of the
token type in
ignoreOccurrenceContext.
-
beginTree
public void beginTree(DetailAST rootAST)
Description copied from class:AbstractCheckCalled before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Overrides:
beginTreein classAbstractCheck- Parameters:
rootAST- the root of the tree
-
finishTree
public void finishTree(DetailAST rootAST)
Description copied from class:AbstractCheckCalled after finished processing a tree. Ideal place to report on information collected whilst processing a tree.- Overrides:
finishTreein classAbstractCheck- Parameters:
rootAST- the root of the tree
-
-