Class CommonUtil


  • public final class CommonUtil
    extends java.lang.Object
    Contains utility methods.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_TAB_WIDTH
      Default tab width for column reporting.
      static byte[] EMPTY_BYTE_ARRAY
      Copied from org.apache.commons.lang3.ArrayUtils.
      static double[] EMPTY_DOUBLE_ARRAY
      Copied from org.apache.commons.lang3.ArrayUtils.
      static int[] EMPTY_INT_ARRAY
      Copied from org.apache.commons.lang3.ArrayUtils.
      static java.lang.Integer[] EMPTY_INTEGER_OBJECT_ARRAY
      Copied from org.apache.commons.lang3.ArrayUtils.
      static java.lang.Object[] EMPTY_OBJECT_ARRAY
      Copied from org.apache.commons.lang3.ArrayUtils.
      static java.lang.String[] EMPTY_STRING_ARRAY
      Copied from org.apache.commons.lang3.ArrayUtils.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String baseClassName​(java.lang.String type)
      Returns base class name from qualified name.
      static void close​(java.io.Closeable closeable)
      Closes a stream re-throwing IOException as IllegalStateException.
      static DetailAST createBlockCommentNode​(antlr.Token token)
      Create block comment from token.
      static DetailAST createBlockCommentNode​(java.lang.String content)
      Create block comment from string content.
      static java.util.regex.Pattern createPattern​(java.lang.String pattern)
      Helper method to create a regular expression.
      static java.util.regex.Pattern createPattern​(java.lang.String pattern, int flags)
      Helper method to create a regular expression with a specific flags.
      static boolean endsWithChar​(java.lang.String value, char suffix)
      Tests if this string ends with the specified suffix.
      static java.lang.String fillTemplateWithStringsByRegexp​(java.lang.String template, java.lang.String lineToPlaceInTemplate, java.util.regex.Pattern regexp)
      Puts part of line, which matches regexp into given template on positions $n where 'n' is number of matched part in line.
      static <T> java.lang.reflect.Constructor<T> getConstructor​(java.lang.Class<T> targetClass, java.lang.Class<?>... parameterTypes)
      Gets constructor of targetClass.
      static java.lang.String getFileExtension​(java.lang.String fileNameWithExtension)
      Returns file extension for the given file name or empty string if file does not have an extension.
      static java.lang.String getFileNameWithoutExtension​(java.lang.String fullFilename)
      Returns file name without extension.
      static java.net.URI getUriByFilename​(java.lang.String filename)
      Resolve the specified filename to a URI.
      static boolean hasWhitespaceBefore​(int index, java.lang.String line)
      Returns whether the specified string contains only whitespace up to the specified index.
      static int indexOfNonWhitespace​(java.lang.String value)
      Method to find the index of the first non-whitespace character in a string.
      static <T> T invokeConstructor​(java.lang.reflect.Constructor<T> constructor, java.lang.Object... parameters)
      Returns new instance of a class.
      static boolean isBlank​(java.lang.String value)
      Checks if the value arg is blank by either being null, empty, or contains only whitespace characters.
      static boolean isIdentifier​(java.lang.String str)
      Checks whether the given string is a valid identifier.
      static boolean isInt​(java.lang.String str)
      Checks whether the string contains an integer value.
      static boolean isName​(java.lang.String str)
      Checks whether the given string is a valid name.
      static boolean isPatternValid​(java.lang.String pattern)
      Validates whether passed string is a valid pattern or not.
      static int lengthExpandedTabs​(java.lang.String inputString, int toIdx, int tabWidth)
      Returns the length of a String prefix with tabs expanded.
      static int lengthMinusTrailingWhitespace​(java.lang.String line)
      Returns the length of a string ignoring all trailing whitespace.
      static boolean matchesFileExtension​(java.io.File file, java.lang.String... fileExtensions)
      Returns whether the file extension matches what we are meant to process.
      static java.lang.String relativizeAndNormalizePath​(java.lang.String baseDirectory, java.lang.String path)
      Constructs a normalized relative path between base directory and a given path.
      static boolean startsWithChar​(java.lang.String value, char prefix)
      Tests if this string starts with the specified prefix.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_TAB_WIDTH

        public static final int DEFAULT_TAB_WIDTH
        Default tab width for column reporting.
        See Also:
        Constant Field Values
      • EMPTY_STRING_ARRAY

        public static final java.lang.String[] EMPTY_STRING_ARRAY
        Copied from org.apache.commons.lang3.ArrayUtils.
      • EMPTY_INTEGER_OBJECT_ARRAY

        public static final java.lang.Integer[] EMPTY_INTEGER_OBJECT_ARRAY
        Copied from org.apache.commons.lang3.ArrayUtils.
      • EMPTY_OBJECT_ARRAY

        public static final java.lang.Object[] EMPTY_OBJECT_ARRAY
        Copied from org.apache.commons.lang3.ArrayUtils.
      • EMPTY_INT_ARRAY

        public static final int[] EMPTY_INT_ARRAY
        Copied from org.apache.commons.lang3.ArrayUtils.
      • EMPTY_BYTE_ARRAY

        public static final byte[] EMPTY_BYTE_ARRAY
        Copied from org.apache.commons.lang3.ArrayUtils.
      • EMPTY_DOUBLE_ARRAY

        public static final double[] EMPTY_DOUBLE_ARRAY
        Copied from org.apache.commons.lang3.ArrayUtils.
    • Method Detail

      • createPattern

        public static java.util.regex.Pattern createPattern​(java.lang.String pattern)
        Helper method to create a regular expression.
        Parameters:
        pattern - the pattern to match
        Returns:
        a created regexp object
        Throws:
        java.lang.IllegalArgumentException - if unable to create Pattern object.
      • createPattern

        public static java.util.regex.Pattern createPattern​(java.lang.String pattern,
                                                            int flags)
        Helper method to create a regular expression with a specific flags.
        Parameters:
        pattern - the pattern to match
        flags - the flags to set
        Returns:
        a created regexp object
        Throws:
        java.lang.IllegalArgumentException - if unable to create Pattern object.
      • createBlockCommentNode

        public static DetailAST createBlockCommentNode​(java.lang.String content)
        Create block comment from string content.
        Parameters:
        content - comment content.
        Returns:
        DetailAST block comment
      • createBlockCommentNode

        public static DetailAST createBlockCommentNode​(antlr.Token token)
        Create block comment from token.
        Parameters:
        token - Token object.
        Returns:
        DetailAST with BLOCK_COMMENT type.
      • matchesFileExtension

        public static boolean matchesFileExtension​(java.io.File file,
                                                   java.lang.String... fileExtensions)
        Returns whether the file extension matches what we are meant to process.
        Parameters:
        file - the file to be checked.
        fileExtensions - files extensions, empty property in config makes it matches to all.
        Returns:
        whether there is a match.
      • hasWhitespaceBefore

        public static boolean hasWhitespaceBefore​(int index,
                                                  java.lang.String line)
        Returns whether the specified string contains only whitespace up to the specified index.
        Parameters:
        index - index to check up to
        line - the line to check
        Returns:
        whether there is only whitespace
      • lengthMinusTrailingWhitespace

        public static int lengthMinusTrailingWhitespace​(java.lang.String line)
        Returns the length of a string ignoring all trailing whitespace. It is a pity that there is not a trim() like method that only removed the trailing whitespace.
        Parameters:
        line - the string to process
        Returns:
        the length of the string ignoring all trailing whitespace
      • lengthExpandedTabs

        public static int lengthExpandedTabs​(java.lang.String inputString,
                                             int toIdx,
                                             int tabWidth)
        Returns the length of a String prefix with tabs expanded. Each tab is counted as the number of characters is takes to jump to the next tab stop.
        Parameters:
        inputString - the input String
        toIdx - index in string (exclusive) where the calculation stops
        tabWidth - the distance between tab stop position.
        Returns:
        the length of string.substring(0, toIdx) with tabs expanded.
      • isPatternValid

        public static boolean isPatternValid​(java.lang.String pattern)
        Validates whether passed string is a valid pattern or not.
        Parameters:
        pattern - string to validate
        Returns:
        true if the pattern is valid false otherwise
      • baseClassName

        public static java.lang.String baseClassName​(java.lang.String type)
        Returns base class name from qualified name.
        Parameters:
        type - the fully qualified name. Cannot be null
        Returns:
        the base class name from a fully qualified name
      • relativizeAndNormalizePath

        public static java.lang.String relativizeAndNormalizePath​(java.lang.String baseDirectory,
                                                                  java.lang.String path)
        Constructs a normalized relative path between base directory and a given path.
        Parameters:
        baseDirectory - the base path to which given path is relativized
        path - the path to relativize against base directory
        Returns:
        the relative normalized path between base directory and path or path if base directory is null.
      • startsWithChar

        public static boolean startsWithChar​(java.lang.String value,
                                             char prefix)
        Tests if this string starts with the specified prefix.

        It is faster version of String.startsWith(String) optimized for one-character prefixes at the expense of some readability. Suggested by SimplifyStartsWith PMD rule: http://pmd.sourceforge.net/pmd-5.3.1/pmd-java/rules/java/optimizations.html#SimplifyStartsWith

        Parameters:
        value - the String to check
        prefix - the prefix to find
        Returns:
        true if the char is a prefix of the given String; false otherwise.
      • endsWithChar

        public static boolean endsWithChar​(java.lang.String value,
                                           char suffix)
        Tests if this string ends with the specified suffix.

        It is faster version of String.endsWith(String) optimized for one-character suffixes at the expense of some readability. Suggested by SimplifyStartsWith PMD rule: http://pmd.sourceforge.net/pmd-5.3.1/pmd-java/rules/java/optimizations.html#SimplifyStartsWith

        Parameters:
        value - the String to check
        suffix - the suffix to find
        Returns:
        true if the char is a suffix of the given String; false otherwise.
      • getConstructor

        public static <T> java.lang.reflect.Constructor<T> getConstructor​(java.lang.Class<T> targetClass,
                                                                          java.lang.Class<?>... parameterTypes)
        Gets constructor of targetClass.
        Type Parameters:
        T - type of the target class object.
        Parameters:
        targetClass - from which constructor is returned
        parameterTypes - of constructor
        Returns:
        constructor of targetClass
        Throws:
        java.lang.IllegalStateException - if any exception occurs
        See Also:
        Class.getConstructor(Class[])
      • invokeConstructor

        public static <T> T invokeConstructor​(java.lang.reflect.Constructor<T> constructor,
                                              java.lang.Object... parameters)
        Returns new instance of a class.
        Type Parameters:
        T - type of constructor
        Parameters:
        constructor - to invoke
        parameters - to pass to constructor
        Returns:
        new instance of class
        Throws:
        java.lang.IllegalStateException - if any exception occurs
        See Also:
        Constructor.newInstance(Object...)
      • close

        public static void close​(java.io.Closeable closeable)
        Closes a stream re-throwing IOException as IllegalStateException.
        Parameters:
        closeable - Closeable object
        Throws:
        java.lang.IllegalStateException - when any IOException occurs
      • getUriByFilename

        public static java.net.URI getUriByFilename​(java.lang.String filename)
                                             throws CheckstyleException
        Resolve the specified filename to a URI.
        Parameters:
        filename - name os the file
        Returns:
        resolved header file URI
        Throws:
        CheckstyleException - on failure
      • fillTemplateWithStringsByRegexp

        public static java.lang.String fillTemplateWithStringsByRegexp​(java.lang.String template,
                                                                       java.lang.String lineToPlaceInTemplate,
                                                                       java.util.regex.Pattern regexp)
        Puts part of line, which matches regexp into given template on positions $n where 'n' is number of matched part in line.
        Parameters:
        template - the string to expand.
        lineToPlaceInTemplate - contains expression which should be placed into string.
        regexp - expression to find in comment.
        Returns:
        the string, based on template filled with given lines
      • getFileNameWithoutExtension

        public static java.lang.String getFileNameWithoutExtension​(java.lang.String fullFilename)
        Returns file name without extension. We do not use the method from Guava library to reduce Checkstyle's dependencies on external libraries.
        Parameters:
        fullFilename - file name with extension.
        Returns:
        file name without extension.
      • getFileExtension

        public static java.lang.String getFileExtension​(java.lang.String fileNameWithExtension)
        Returns file extension for the given file name or empty string if file does not have an extension. We do not use the method from Guava library to reduce Checkstyle's dependencies on external libraries.
        Parameters:
        fileNameWithExtension - file name with extension.
        Returns:
        file extension for the given file name or empty string if file does not have an extension.
      • isIdentifier

        public static boolean isIdentifier​(java.lang.String str)
        Checks whether the given string is a valid identifier.
        Parameters:
        str - A string to check.
        Returns:
        true when the given string contains valid identifier.
      • isName

        public static boolean isName​(java.lang.String str)
        Checks whether the given string is a valid name.
        Parameters:
        str - A string to check.
        Returns:
        true when the given string contains valid name.
      • isBlank

        public static boolean isBlank​(java.lang.String value)
        Checks if the value arg is blank by either being null, empty, or contains only whitespace characters.
        Parameters:
        value - A string to check.
        Returns:
        true if the arg is blank.
      • indexOfNonWhitespace

        public static int indexOfNonWhitespace​(java.lang.String value)
        Method to find the index of the first non-whitespace character in a string.
        Parameters:
        value - the string to find the first index of a non-whitespace character for.
        Returns:
        the index of the first non-whitespace character.
      • isInt

        public static boolean isInt​(java.lang.String str)
        Checks whether the string contains an integer value.
        Parameters:
        str - a string to check
        Returns:
        true if the given string is an integer, false otherwise.