Class CommonUtil

java.lang.Object
com.puppycrawl.tools.checkstyle.utils.CommonUtil

public final class CommonUtil extends Object
Contains utility methods.
  • Field Details

    • DEFAULT_TAB_WIDTH

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

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

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

      public static final 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 Details

    • createPattern

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

      public static Pattern createPattern(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:
      IllegalArgumentException - if unable to create Pattern object.
    • createBlockCommentNode

      public static DetailAST createBlockCommentNode(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(File file, 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, 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(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(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(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 String baseClassName(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 String relativizeAndNormalizePath(String baseDirectory, 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(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(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> Constructor<T> getConstructor(Class<T> targetClass, 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:
      IllegalStateException - if any exception occurs
      See Also:
    • invokeConstructor

      public static <T> T invokeConstructor(Constructor<T> constructor, 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:
      IllegalStateException - if any exception occurs
      See Also:
    • close

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

      public static URI getUriByFilename(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 String fillTemplateWithStringsByRegexp(String template, String lineToPlaceInTemplate, 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 String getFileNameWithoutExtension(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 String getFileExtension(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(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(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(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(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(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.