Class CheckUtil

  • public final class CheckUtil
    extends Object
    Contains utility methods for the checks.
    • 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.
        ast - the method definition AST to test. Precondition: ast is a TokenTypes.METHOD_DEF node.
        true if ast defines an equals covariant.
      • parseDouble

        public static double parseDouble​(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.
        text - the string to be parsed.
        type - the token type of the text. Should be a constant of TokenTypes.
        the double value represented by the string argument.
      • parseNumber

        private static double parseNumber​(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.
        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 of TokenTypes.
        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.
        node - the root of tree for search.
        sub-node with minimal (line, column) pair.
      • isBeforeInSource

        public static boolean isBeforeInSource​(DetailAST ast1,
                                               DetailAST ast2)
        Retrieves whether ast1 is located before ast2.
        ast1 - the first node.
        ast2 - the second node.
        true, if ast1 is located before ast2.
      • getTypeParameterNames

        public static List<StringgetTypeParameterNames​(DetailAST node)
        Retrieves the names of the type parameters to the node.
        node - the parameterized AST node
        a list of type parameter names
      • getTypeParameters

        public static List<DetailASTgetTypeParameters​(DetailAST node)
        Retrieves the type parameters to the node.
        node - the parameterized AST node
        a list of type parameter names
      • isNonVoidMethod

        public static boolean isNonVoidMethod​(DetailAST methodDefAst)
        Checks whether a method is a not void one.
        methodDefAst - the method node.
        true if method is a not void one.
      • isReceiverParameter

        public static boolean isReceiverParameter​(DetailAST parameterDefAst)
        Checks whether a parameter is a receiver.

        A receiver parameter is a special parameter that represents the object for which the method is invoked. It is denoted by the reserved keyword this in the method declaration. Check PARAMETER_DEF

        parameterDefAst - the parameter node.
        true if the parameter is a receiver.
        See Also:
      • 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.
        ast - the token of the method/constructor.
        the access modifier of the method/constructor.
      • getSurroundingAccessModifier

        public static AccessModifierOption getSurroundingAccessModifier​(DetailAST node)
        Returns the access modifier of the surrounding "block".
        node - the node to return the access modifier for
        the access modifier of the surrounding block
      • parseClassNames

        public static Set<StringparseClassNames​(String... classNames)
        Create set of class names and short class names.
        classNames - array of class names.
        set of class names and short class names.
      • stripIndentAndInitialNewLineFromTextBlock

        public static String stripIndentAndInitialNewLineFromTextBlock​(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:
        textBlockContent - the actual content of the text block.
        string consistent with javac representation.
      • stripIndentAndTrailingWhitespaceFromLine

        private static String stripIndentAndTrailingWhitespaceFromLine​(String line,
                                                                       int indent)
        Helper method for stripIndentAndInitialNewLineFromTextBlock, strips correct indent from string, and trailing whitespace, or returns empty string if no text.
        line - the string to strip indent and trailing whitespace from
        indent - the amount of indent to remove
        modified string with removed indent and trailing whitespace, or empty string.
      • getSmallestIndent

        private static int getSmallestIndent​(Collection<String> lines)
        Helper method for stripIndentAndInitialNewLineFromTextBlock, to determine the smallest indent in a text block string literal.
        lines - collection of actual text block content, split by line.
        number of spaces representing the smallest indent in this text block.
      • lastIndexOfNonWhitespace

        private static int lastIndexOfNonWhitespace​(String line)
        Helper method to find the index of the last non-whitespace character in a string.
        line - the string to find the last index of a non-whitespace character for.
        the index of the last non-whitespace character.
      • typeDeclarationNameMatchingCount

        public static int typeDeclarationNameMatchingCount​(String patternClass,
                                                           String classToBeMatched)
        Calculates and returns the type declaration name matching count.

        Suppose our pattern class is foo.a.b and class to be matched is foo.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).

        patternClass - class against which the given class has to be matched
        classToBeMatched - class to be matched
        class name matching count
      • getQualifiedTypeDeclarationName

        public static String getQualifiedTypeDeclarationName​(String packageName,
                                                             String outerClassQualifiedName,
                                                             String className)
        Get the qualified name of type declaration by combining packageName, outerClassQualifiedName and className.
        packageName - packageName
        outerClassQualifiedName - outerClassQualifiedName
        className - className
        the qualified name of type declaration by combining packageName, outerClassQualifiedName and className
      • extractQualifiedName

        public static String extractQualifiedName​(DetailAST ast)
        Get name of package and super class of anon inner class by concatenating the identifier values under TokenTypes.DOT.
        ast - ast to extract superclass or package name from
        qualified name
      • getShortNameOfAnonInnerClass

        public static 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
        literalNewAst - ast node of type TokenTypes.LITERAL_NEW
        short name of base class of anonymous inner class
      • isPackageInfo

        public static boolean isPackageInfo​(String filePath)
        Checks if the given file path is a file.
        filePath - path to the file.
        true if the package file.