Class CommonUtil

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

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

  • Constructor Details

    • CommonUtil

      private CommonUtil()
      Stop instances being created.
  • 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.
    • 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
    • relativizePath

      public static String relativizePath(String baseDirectory, String path)
      Constructs a 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.
    • 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
    • sourceFromFilename

      public static InputSource sourceFromFilename(String filename) throws CheckstyleException
      Creates an input source from a file.
      Parameters:
      filename - name of the file.
      Returns:
      input source.
      Throws:
      CheckstyleException - if an error occurs.
    • getUriByFilename

      public static URI getUriByFilename(String filename) throws CheckstyleException
      Resolve the specified filename to a URI.
      Parameters:
      filename - name of the file
      Returns:
      resolved file URI
      Throws:
      CheckstyleException - on failure
    • getWebOrFileProtocolUri

      public static URI getWebOrFileProtocolUri(String filename)
      Resolves the specified filename containing 'http', 'https', 'ftp', and 'file' protocols (or any RFC 2396 compliant URL) to a URI.
      Parameters:
      filename - name of the file
      Returns:
      resolved file URI or null if URL is malformed or non-existent
    • getFilepathOrClasspathUri

      private static URI getFilepathOrClasspathUri(String filename) throws CheckstyleException
      Resolves the specified local filename, possibly with 'classpath:' protocol, to a URI. First we attempt to create a new file with given filename, then attempt to load file from class path.
      Parameters:
      filename - name of the file
      Returns:
      resolved file URI
      Throws:
      CheckstyleException - on failure
    • getResourceFromClassPath

      public static URI getResourceFromClassPath(String filename) throws CheckstyleException
      Gets a resource from the classpath.
      Parameters:
      filename - name of file
      Returns:
      URI of file in classpath
      Throws:
      CheckstyleException - on failure
    • getCheckstyleResource

      public static URL getCheckstyleResource(String name)
      Finds a resource with a given name in the Checkstyle resource bundle. This method is intended only for internal use in Checkstyle tests for easy mocking to gain 100% coverage.
      Parameters:
      name - name of the desired resource
      Returns:
      URI of the resource
    • 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.
    • isCodePointWhitespace

      public static boolean isCodePointWhitespace(int[] codePoints, int index)
      Converts the Unicode code point at index index to it's UTF-16 representation, then checks if the character is whitespace. Note that the given index index should correspond to the location of the character to check in the string, not in code points.
      Parameters:
      codePoints - the array of Unicode code points
      index - the index of the character to check
      Returns:
      true if character at index is whitespace