Class CommonUtil
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.utils.CommonUtil
-
public final class CommonUtil extends Object
Contains utility methods.
-
-
Field Summary
Fields Modifier and Type Field Description static String
CLASSPATH_URL_PROTOCOL
Pseudo URL protocol for loading from the class path.static int
DEFAULT_TAB_WIDTH
Default tab width for column reporting.static BitSet
EMPTY_BIT_SET
For cases where no tokens should be accepted.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 Integer[]
EMPTY_INTEGER_OBJECT_ARRAY
Copied from org.apache.commons.lang3.ArrayUtils.static Object[]
EMPTY_OBJECT_ARRAY
Copied from org.apache.commons.lang3.ArrayUtils.static String[]
EMPTY_STRING_ARRAY
Copied from org.apache.commons.lang3.ArrayUtils.private static String
EXTENSION_SEPARATOR
The extension separator.private static String
UNABLE_TO_FIND_EXCEPTION_PREFIX
Prefix for the exception when unable to find resource.
-
Constructor Summary
Constructors Modifier Constructor Description private
CommonUtil()
Stop instances being created.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
baseClassName(String type)
Returns base class name from qualified name.static void
close(Closeable closeable)
Closes a stream re-throwing IOException as IllegalStateException.static Pattern
createPattern(String pattern)
Helper method to create a regular expression.static Pattern
createPattern(String pattern, int flags)
Helper method to create a regular expression with a specific flags.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.static URL
getCheckstyleResource(String name)
Finds a resource with a given name in the Checkstyle resource bundle.static <T> Constructor<T>
getConstructor(Class<T> targetClass, Class<?>... parameterTypes)
Gets constructor of targetClass.static String
getFileExtension(String fileNameWithExtension)
Returns file extension for the given file name or empty string if file does not have an extension.static String
getFileNameWithoutExtension(String fullFilename)
Returns file name without extension.private static URI
getFilepathOrClasspathUri(String filename)
Resolves the specified local filename, possibly with 'classpath:' protocol, to a URI.static URI
getResourceFromClassPath(String filename)
Gets a resource from the classpath.static URI
getUriByFilename(String filename)
Resolve the specified filename to a URI.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.static boolean
hasWhitespaceBefore(int index, String line)
Returns whether the specified string contains only whitespace up to the specified index.static int
indexOfNonWhitespace(String value)
Method to find the index of the first non-whitespace character in a string.static <T> T
invokeConstructor(Constructor<T> constructor, Object... parameters)
Returns new instance of a class.static boolean
isBlank(String value)
Checks if the value arg is blank by either being null, empty, or contains only whitespace characters.static boolean
isCodePointWhitespace(int[] codePoints, int index)
Converts the Unicode code point at indexindex
to it's UTF-16 representation, then checks if the character is whitespace.static boolean
isIdentifier(String str)
Checks whether the given string is a valid identifier.static boolean
isName(String str)
Checks whether the given string is a valid name.static boolean
isPatternValid(String pattern)
Validates whether passed string is a valid pattern or not.static int
lengthExpandedTabs(String inputString, int toIdx, int tabWidth)
Returns the length of a String prefix with tabs expanded.static int
lengthMinusTrailingWhitespace(String line)
Returns the length of a string ignoring all trailing whitespace.static boolean
matchesFileExtension(File file, String... fileExtensions)
Returns whether the file extension matches what we are meant to process.static String
relativizePath(String baseDirectory, String path)
Constructs a relative path between base directory and a given path.static InputSource
sourceFromFilename(String filename)
Creates an input source from a file.
-
-
-
Field Detail
-
DEFAULT_TAB_WIDTH
public static final int DEFAULT_TAB_WIDTH
Default tab width for column reporting.- See Also:
- Constant Field Values
-
EMPTY_BIT_SET
public static final BitSet EMPTY_BIT_SET
For cases where no tokens should be accepted.
-
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.
-
CLASSPATH_URL_PROTOCOL
public static final String CLASSPATH_URL_PROTOCOL
Pseudo URL protocol for loading from the class path.- See Also:
- Constant Field Values
-
UNABLE_TO_FIND_EXCEPTION_PREFIX
private static final String UNABLE_TO_FIND_EXCEPTION_PREFIX
Prefix for the exception when unable to find resource.- See Also:
- Constant Field Values
-
EXTENSION_SEPARATOR
private static final String EXTENSION_SEPARATOR
The extension separator.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
CommonUtil
private CommonUtil()
Stop instances being created.
-
-
Method Detail
-
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 matchflags
- 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 toline
- 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 StringtoIdx
- index in string (exclusive) where the calculation stopstabWidth
- 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 relativizedpath
- 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 returnedparameterTypes
- of constructor- Returns:
- constructor of targetClass
- Throws:
IllegalStateException
- if any exception occurs- See Also:
Class.getConstructor(Class[])
-
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 invokeparameters
- to pass to constructor- Returns:
- new instance of class
- Throws:
IllegalStateException
- if any exception occurs- See Also:
Constructor.newInstance(Object...)
-
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 indexindex
to it's UTF-16 representation, then checks if the character is whitespace. Note that the given indexindex
should correspond to the location of the character to check in the string, not in code points.- Parameters:
codePoints
- the array of Unicode code pointsindex
- the index of the character to check- Returns:
- true if character at
index
is whitespace
-
-