Class IllegalSymbolCheck
java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractCheck
com.puppycrawl.tools.checkstyle.checks.coding.IllegalSymbolCheck
- All Implemented Interfaces:
Configurable,Contextualizable
Checks that specified symbols (by Unicode code points or ranges) are not used in code.
Rationale: This check helps prevent emoji symbols in code, enforce ASCII-only source files, or forbid specific Unicode characters.
- Since:
- 13.1.0
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intASCII range upper bound (exclusive).private booleanControl whether only ASCII characters are allowed.static final StringA key is pointing to the warning message text in "messages.properties" file.private static final StringString Range Separator.private StringSpecify the symbols to check for, as Unicode code points or ranges. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidCheck the text for illegal symbols.int[]The configurable token set.int[]Returns the default token a check is interested in.int[]The tokens that this check must be registered for.booleanWhether comment nodes are required or not.private booleanisIllegalSymbol(int codePoint) Check if a code point is illegal based on configured ranges.private static booleanCheck if code point is in the specified range.private booleanisInSymbolCodes(int codePoint) Check if code point is in the configured symbol codes.private static intparseCodePoint(String str) Parse a code point from string representation.voidsetAsciiOnly(boolean asciiOnly) Setter to control whether only ASCII characters are allowed.voidsetSymbolCodes(String symbols) Setter to specify the symbols to check for.voidvisitToken(DetailAST ast) Called to process a token.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
RANGE_SEPARATOR
String Range Separator.- See Also:
-
ASCII_UPPER_BOUND
ASCII range upper bound (exclusive).- See Also:
-
symbolCodes
Specify the symbols to check for, as Unicode code points or ranges. -
asciiOnly
Control whether only ASCII characters are allowed.
-
-
Constructor Details
-
IllegalSymbolCheck
public IllegalSymbolCheck()
-
-
Method Details
-
setSymbolCodes
Setter to specify the symbols to check for. Format: comma-separated list of hex codes or ranges (e.g., "0x2705, 0xd83c-0xd83e").- Parameters:
symbols- the symbols specification- Since:
- 13.1.0
-
setAsciiOnly
Setter to control whether only ASCII characters are allowed.- Parameters:
asciiOnly- true to allow only ASCII characters- Since:
- 13.1.0
-
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:
-
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:
-
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:
-
isCommentNodesRequired
Description copied from class:AbstractCheckWhether comment nodes are required or not.- Overrides:
isCommentNodesRequiredin classAbstractCheck- Returns:
- false as a default value.
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
checkText
Check the text for illegal symbols.- Parameters:
text- the text to checkast- the AST node
-
isIllegalSymbol
Check if a code point is illegal based on configured ranges.- Parameters:
codePoint- the code point to check- Returns:
- true if the code point is illegal
-
isInSymbolCodes
Check if code point is in the configured symbol codes.- Parameters:
codePoint- the code point to check- Returns:
- true if in symbol codes
-
isInRange
Check if code point is in the specified range.- Parameters:
codePoint- the code point to checkrangeStr- the range string (e.g., "0x1F600-0x1F64F")- Returns:
- true if in range
-
parseCodePoint
Parse a code point from string representation. Supports formats: 0x1234, \\u1234, U+1234, or decimal.- Parameters:
str- the string to parse- Returns:
- the code point value
-