Class CheckUtil
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.utils.CheckUtil
-
public final class CheckUtil extends java.lang.Object
Contains utility methods for the checks.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.util.regex.Pattern
ALL_NEW_LINES
Compiled pattern for all system newlines.private static int
BASE_10
Decimal radix.private static int
BASE_16
Hex radix.private static int
BASE_2
Binary radix.private static int
BASE_8
Octal radix.private static int
GETTER_BODY_SIZE
Maximum nodes allowed in a body of getter.private static java.util.regex.Pattern
GETTER_PATTERN
Pattern matching names of getter methods.private static char
PACKAGE_SEPARATOR
Package separator.private static int
SETTER_BODY_SIZE
Maximum nodes allowed in a body of setter.private static int
SETTER_GETTER_MAX_CHILDREN
Maximum children allowed in setter/getter.private static java.util.regex.Pattern
SETTER_PATTERN
Pattern matching names of setter methods.private static java.util.regex.Pattern
UNDERSCORE_PATTERN
Pattern matching underscore characters ('_').
-
Constructor Summary
Constructors Modifier Constructor Description private
CheckUtil()
Prevent instances.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
extractQualifiedName(DetailAST ast)
Get name of package and super class of anon inner class by concatenating the identifier values underTokenTypes.DOT
.static AccessModifierOption
getAccessModifierFromModifiersToken(DetailAST ast)
Returns the access modifier of the method/constructor at the specified AST.private static AccessModifierOption
getAccessModifierFromModifiersTokenDirectly(DetailAST modifiersToken)
ReturnsAccessModifierOption
based on the information about access modifier taken from the given token of typeTokenTypes.MODIFIERS
.static DetailAST
getFirstNode(DetailAST node)
Finds sub-node for given node minimal (line, column) pair.static java.lang.String
getQualifiedTypeDeclarationName(java.lang.String packageName, java.lang.String outerClassQualifiedName, java.lang.String className)
Get the qualified name of type declaration by combiningpackageName
,outerClassQualifiedName
andclassName
.static java.lang.String
getShortNameOfAnonInnerClass(DetailAST literalNewAst)
Get the short name of super class of anonymous inner class.private static int
getSmallestIndent(java.util.Collection<java.lang.String> lines)
Helper method for stripIndentAndInitialNewLineFromTextBlock, to determine the smallest indent in a text block string literal.static AccessModifierOption
getSurroundingAccessModifier(DetailAST node)
Returns the access modifier of the surrounding "block".static java.util.List<java.lang.String>
getTypeParameterNames(DetailAST node)
Retrieves the names of the type parameters to the node.static java.util.List<DetailAST>
getTypeParameters(DetailAST node)
Retrieves the type parameters to the node.static boolean
isBeforeInSource(DetailAST ast1, DetailAST ast2)
Retrieves whether ast1 is located before ast2.private static boolean
isElse(DetailAST ast)
Returns whether a token represents an ELSE.static boolean
isElseIf(DetailAST ast)
Returns whether a token represents an ELSE as part of an ELSE / IF set.private static boolean
isElseWithCurlyBraces(DetailAST ast)
Returns whether a token represents an SLIST as part of an ELSE statement.static boolean
isEqualsMethod(DetailAST ast)
Tests whether a method definition AST defines an equals covariant.static boolean
isGetterMethod(DetailAST ast)
Returns whether an AST represents a getter method.static boolean
isNonVoidMethod(DetailAST methodDefAst)
Checks whether a method is a not void one.static boolean
isPackageInfo(java.lang.String filePath)
Checks if the given file path is a package-info.java file.static boolean
isReceiverParameter(DetailAST parameterDefAst)
Checks whether a parameter is a receiver.static boolean
isSetterMethod(DetailAST ast)
Returns whether an AST represents a setter method.private static int
lastIndexOfNonWhitespace(java.lang.String line)
Helper method to find the index of the last non-whitespace character in a string.static java.util.Set<java.lang.String>
parseClassNames(java.lang.String... classNames)
Create set of class names and short class names.static double
parseDouble(java.lang.String text, int type)
Returns the value represented by the specified string of the specified type.private static double
parseNumber(java.lang.String text, int radix, int type)
Parses the string argument as an integer or a long in the radix specified by the second argument.static java.lang.String
stripIndentAndInitialNewLineFromTextBlock(java.lang.String textBlockContent)
Strip initial newline and preceding whitespace on each line from text block content.private static java.lang.String
stripIndentAndTrailingWhitespaceFromLine(java.lang.String line, int indent)
Helper method for stripIndentAndInitialNewLineFromTextBlock, strips correct indent from string, and trailing whitespace, or returns empty string if no text.static int
typeDeclarationNameMatchingCount(java.lang.String patternClass, java.lang.String classToBeMatched)
Calculates and returns the type declaration name matching count.
-
-
-
Field Detail
-
BASE_2
private static final int BASE_2
Binary radix.- See Also:
- Constant Field Values
-
BASE_8
private static final int BASE_8
Octal radix.- See Also:
- Constant Field Values
-
BASE_10
private static final int BASE_10
Decimal radix.- See Also:
- Constant Field Values
-
BASE_16
private static final int BASE_16
Hex radix.- See Also:
- Constant Field Values
-
SETTER_GETTER_MAX_CHILDREN
private static final int SETTER_GETTER_MAX_CHILDREN
Maximum children allowed in setter/getter.- See Also:
- Constant Field Values
-
SETTER_BODY_SIZE
private static final int SETTER_BODY_SIZE
Maximum nodes allowed in a body of setter.- See Also:
- Constant Field Values
-
GETTER_BODY_SIZE
private static final int GETTER_BODY_SIZE
Maximum nodes allowed in a body of getter.- See Also:
- Constant Field Values
-
UNDERSCORE_PATTERN
private static final java.util.regex.Pattern UNDERSCORE_PATTERN
Pattern matching underscore characters ('_').
-
SETTER_PATTERN
private static final java.util.regex.Pattern SETTER_PATTERN
Pattern matching names of setter methods.
-
GETTER_PATTERN
private static final java.util.regex.Pattern GETTER_PATTERN
Pattern matching names of getter methods.
-
ALL_NEW_LINES
private static final java.util.regex.Pattern ALL_NEW_LINES
Compiled pattern for all system newlines.
-
PACKAGE_SEPARATOR
private static final char PACKAGE_SEPARATOR
Package separator.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
CheckUtil
private CheckUtil()
Prevent instances.
-
-
Method Detail
-
isEqualsMethod
public static boolean isEqualsMethod(DetailAST ast)
Tests whether a method definition AST defines an equals covariant.- Parameters:
ast
- the method definition AST to test. Precondition: ast is a TokenTypes.METHOD_DEF node.- Returns:
- true if ast defines an equals covariant.
-
isElseIf
public static boolean isElseIf(DetailAST ast)
Returns whether a token represents an ELSE as part of an ELSE / IF set.- Parameters:
ast
- the token to check- Returns:
- whether it is
-
isElse
private static boolean isElse(DetailAST ast)
Returns whether a token represents an ELSE.- Parameters:
ast
- the token to check- Returns:
- whether the token represents an ELSE
-
isElseWithCurlyBraces
private static boolean isElseWithCurlyBraces(DetailAST ast)
Returns whether a token represents an SLIST as part of an ELSE statement.- Parameters:
ast
- the token to check- Returns:
- whether the toke does represent an SLIST as part of an ELSE
-
parseDouble
public static double parseDouble(java.lang.String text, int type)
Returns the value represented by the specified string of the specified type. Returns 0 for types other than float, double, int, and long.- Parameters:
text
- the string to be parsed.type
- the token type of the text. Should be a constant ofTokenTypes
.- Returns:
- the double value represented by the string argument.
-
parseNumber
private static double parseNumber(java.lang.String text, int radix, int type)
Parses the string argument as an integer or a long in the radix specified by the second argument. The characters in the string must all be digits of the specified radix.- Parameters:
text
- the String containing the integer representation to be parsed. Precondition: text contains a parsable int.radix
- the radix to be used while parsing text.type
- the token type of the text. Should be a constant ofTokenTypes
.- Returns:
- the number represented by the string argument in the specified radix.
-
getFirstNode
public static DetailAST getFirstNode(DetailAST node)
Finds sub-node for given node minimal (line, column) pair.- Parameters:
node
- the root of tree for search.- Returns:
- sub-node with minimal (line, column) pair.
-
isBeforeInSource
public static boolean isBeforeInSource(DetailAST ast1, DetailAST ast2)
Retrieves whether ast1 is located before ast2.- Parameters:
ast1
- the first node.ast2
- the second node.- Returns:
- true, if ast1 is located before ast2.
-
getTypeParameterNames
public static java.util.List<java.lang.String> getTypeParameterNames(DetailAST node)
Retrieves the names of the type parameters to the node.- Parameters:
node
- the parameterized AST node- Returns:
- a list of type parameter names
-
getTypeParameters
public static java.util.List<DetailAST> getTypeParameters(DetailAST node)
Retrieves the type parameters to the node.- Parameters:
node
- the parameterized AST node- Returns:
- a list of type parameter names
-
isSetterMethod
public static boolean isSetterMethod(DetailAST ast)
Returns whether an AST represents a setter method.- Parameters:
ast
- the AST to check with- Returns:
- whether the AST represents a setter method
-
isGetterMethod
public static boolean isGetterMethod(DetailAST ast)
Returns whether an AST represents a getter method.- Parameters:
ast
- the AST to check with- Returns:
- whether the AST represents a getter method
-
isNonVoidMethod
public static boolean isNonVoidMethod(DetailAST methodDefAst)
Checks whether a method is a not void one.- Parameters:
methodDefAst
- the method node.- Returns:
- true if method is a not void one.
-
isReceiverParameter
public static boolean isReceiverParameter(DetailAST parameterDefAst)
Checks whether a parameter is a receiver.- Parameters:
parameterDefAst
- the parameter node.- Returns:
- true if the parameter is a receiver.
-
getAccessModifierFromModifiersToken
public static AccessModifierOption getAccessModifierFromModifiersToken(DetailAST ast)
Returns the access modifier of the method/constructor at the specified AST. If the method is in an interface or annotation block, the access modifier is assumed to be public.- Parameters:
ast
- the token of the method/constructor.- Returns:
- the access modifier of the method/constructor.
-
getAccessModifierFromModifiersTokenDirectly
private static AccessModifierOption getAccessModifierFromModifiersTokenDirectly(DetailAST modifiersToken)
ReturnsAccessModifierOption
based on the information about access modifier taken from the given token of typeTokenTypes.MODIFIERS
.- Parameters:
modifiersToken
- token of typeTokenTypes.MODIFIERS
.- Returns:
AccessModifierOption
.- Throws:
java.lang.IllegalArgumentException
- when expected non-null modifiersToken with type 'MODIFIERS'
-
getSurroundingAccessModifier
public static AccessModifierOption getSurroundingAccessModifier(DetailAST node)
Returns the access modifier of the surrounding "block".- Parameters:
node
- the node to return the access modifier for- Returns:
- the access modifier of the surrounding block
-
parseClassNames
public static java.util.Set<java.lang.String> parseClassNames(java.lang.String... classNames)
Create set of class names and short class names.- Parameters:
classNames
- array of class names.- Returns:
- set of class names and short class names.
-
stripIndentAndInitialNewLineFromTextBlock
public static java.lang.String stripIndentAndInitialNewLineFromTextBlock(java.lang.String textBlockContent)
Strip initial newline and preceding whitespace on each line from text block content. In order to be consistent with how javac handles this task, we have modeled this implementation after the code from: github.com/openjdk/jdk14u/blob/master/src/java.base/share/classes/java/lang/String.java- Parameters:
textBlockContent
- the actual content of the text block.- Returns:
- string consistent with javac representation.
-
stripIndentAndTrailingWhitespaceFromLine
private static java.lang.String stripIndentAndTrailingWhitespaceFromLine(java.lang.String line, int indent)
Helper method for stripIndentAndInitialNewLineFromTextBlock, strips correct indent from string, and trailing whitespace, or returns empty string if no text.- Parameters:
line
- the string to strip indent and trailing whitespace fromindent
- the amount of indent to remove- Returns:
- modified string with removed indent and trailing whitespace, or empty string.
-
getSmallestIndent
private static int getSmallestIndent(java.util.Collection<java.lang.String> lines)
Helper method for stripIndentAndInitialNewLineFromTextBlock, to determine the smallest indent in a text block string literal.- Parameters:
lines
- collection of actual text block content, split by line.- Returns:
- number of spaces representing the smallest indent in this text block.
-
lastIndexOfNonWhitespace
private static int lastIndexOfNonWhitespace(java.lang.String line)
Helper method to find the index of the last non-whitespace character in a string.- Parameters:
line
- the string to find the last index of a non-whitespace character for.- Returns:
- the index of the last non-whitespace character.
-
typeDeclarationNameMatchingCount
public static int typeDeclarationNameMatchingCount(java.lang.String patternClass, java.lang.String classToBeMatched)
Calculates and returns the type declaration name matching count.Suppose our pattern class is
foo.a.b
and class to be matched isfoo.a.ball
then type declaration name matching count would be calculated by comparing every character, and updating main counter when we hit "." to prevent matching "a.b" with "a.ball". In this case type declaration name matching count would be equal to 6 and not 7 (b of ball is not counted).- Parameters:
patternClass
- class against which the given class has to be matchedclassToBeMatched
- class to be matched- Returns:
- class name matching count
-
getQualifiedTypeDeclarationName
public static java.lang.String getQualifiedTypeDeclarationName(java.lang.String packageName, java.lang.String outerClassQualifiedName, java.lang.String className)
Get the qualified name of type declaration by combiningpackageName
,outerClassQualifiedName
andclassName
.- Parameters:
packageName
- packageNameouterClassQualifiedName
- outerClassQualifiedNameclassName
- className- Returns:
- the qualified name of type declaration by combining
packageName
,outerClassQualifiedName
andclassName
-
extractQualifiedName
public static java.lang.String extractQualifiedName(DetailAST ast)
Get name of package and super class of anon inner class by concatenating the identifier values underTokenTypes.DOT
.- Parameters:
ast
- ast to extract superclass or package name from- Returns:
- qualified name
-
getShortNameOfAnonInnerClass
public static java.lang.String getShortNameOfAnonInnerClass(DetailAST literalNewAst)
Get the short name of super class of anonymous inner class. Example:TestClass.NestedClass obj = new Test().new NestedClass() {}; // Short name will be Test.NestedClass
- Parameters:
literalNewAst
- ast node of typeTokenTypes.LITERAL_NEW
- Returns:
- short name of base class of anonymous inner class
-
isPackageInfo
public static boolean isPackageInfo(java.lang.String filePath)
Checks if the given file path is a package-info.java file.- Parameters:
filePath
- path to the file.- Returns:
- true if the package file.
-
-