001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2022 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018///////////////////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.utils;
021
022import java.io.Closeable;
023import java.io.File;
024import java.io.IOException;
025import java.lang.reflect.Constructor;
026import java.lang.reflect.InvocationTargetException;
027import java.net.MalformedURLException;
028import java.net.URI;
029import java.net.URISyntaxException;
030import java.net.URL;
031import java.nio.file.Path;
032import java.nio.file.Paths;
033import java.util.BitSet;
034import java.util.Objects;
035import java.util.regex.Matcher;
036import java.util.regex.Pattern;
037import java.util.regex.PatternSyntaxException;
038
039import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
040
041/**
042 * Contains utility methods.
043 *
044 */
045public final class CommonUtil {
046
047    /** Default tab width for column reporting. */
048    public static final int DEFAULT_TAB_WIDTH = 8;
049
050    /** For cases where no tokens should be accepted. */
051    public static final BitSet EMPTY_BIT_SET = new BitSet();
052    /** Copied from org.apache.commons.lang3.ArrayUtils. */
053    public static final String[] EMPTY_STRING_ARRAY = new String[0];
054    /** Copied from org.apache.commons.lang3.ArrayUtils. */
055    public static final Integer[] EMPTY_INTEGER_OBJECT_ARRAY = new Integer[0];
056    /** Copied from org.apache.commons.lang3.ArrayUtils. */
057    public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
058    /** Copied from org.apache.commons.lang3.ArrayUtils. */
059    public static final int[] EMPTY_INT_ARRAY = new int[0];
060    /** Copied from org.apache.commons.lang3.ArrayUtils. */
061    public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
062    /** Copied from org.apache.commons.lang3.ArrayUtils. */
063    public static final double[] EMPTY_DOUBLE_ARRAY = new double[0];
064    /** Pseudo URL protocol for loading from the class path. */
065    public static final String CLASSPATH_URL_PROTOCOL = "classpath:";
066
067    /** Prefix for the exception when unable to find resource. */
068    private static final String UNABLE_TO_FIND_EXCEPTION_PREFIX = "Unable to find: ";
069
070    /** Stop instances being created. **/
071    private CommonUtil() {
072    }
073
074    /**
075     * Helper method to create a regular expression.
076     *
077     * @param pattern
078     *            the pattern to match
079     * @return a created regexp object
080     * @throws IllegalArgumentException
081     *             if unable to create Pattern object.
082     **/
083    public static Pattern createPattern(String pattern) {
084        return createPattern(pattern, 0);
085    }
086
087    /**
088     * Helper method to create a regular expression with a specific flags.
089     *
090     * @param pattern
091     *            the pattern to match
092     * @param flags
093     *            the flags to set
094     * @return a created regexp object
095     * @throws IllegalArgumentException
096     *             if unable to create Pattern object.
097     **/
098    public static Pattern createPattern(String pattern, int flags) {
099        try {
100            return Pattern.compile(pattern, flags);
101        }
102        catch (final PatternSyntaxException ex) {
103            throw new IllegalArgumentException(
104                "Failed to initialise regular expression " + pattern, ex);
105        }
106    }
107
108    /**
109     * Returns whether the file extension matches what we are meant to process.
110     *
111     * @param file
112     *            the file to be checked.
113     * @param fileExtensions
114     *            files extensions, empty property in config makes it matches to all.
115     * @return whether there is a match.
116     */
117    public static boolean matchesFileExtension(File file, String... fileExtensions) {
118        boolean result = false;
119        if (fileExtensions == null || fileExtensions.length == 0) {
120            result = true;
121        }
122        else {
123            // normalize extensions so all of them have a leading dot
124            final String[] withDotExtensions = new String[fileExtensions.length];
125            for (int i = 0; i < fileExtensions.length; i++) {
126                final String extension = fileExtensions[i];
127                if (startsWithChar(extension, '.')) {
128                    withDotExtensions[i] = extension;
129                }
130                else {
131                    withDotExtensions[i] = "." + extension;
132                }
133            }
134
135            final String fileName = file.getName();
136            for (final String fileExtension : withDotExtensions) {
137                if (fileName.endsWith(fileExtension)) {
138                    result = true;
139                    break;
140                }
141            }
142        }
143
144        return result;
145    }
146
147    /**
148     * Returns whether the specified string contains only whitespace up to the specified index.
149     *
150     * @param index
151     *            index to check up to
152     * @param line
153     *            the line to check
154     * @return whether there is only whitespace
155     */
156    public static boolean hasWhitespaceBefore(int index, String line) {
157        boolean result = true;
158        for (int i = 0; i < index; i++) {
159            if (!Character.isWhitespace(line.charAt(i))) {
160                result = false;
161                break;
162            }
163        }
164        return result;
165    }
166
167    /**
168     * Returns the length of a string ignoring all trailing whitespace.
169     * It is a pity that there is not a trim() like
170     * method that only removed the trailing whitespace.
171     *
172     * @param line
173     *            the string to process
174     * @return the length of the string ignoring all trailing whitespace
175     **/
176    public static int lengthMinusTrailingWhitespace(String line) {
177        int len = line.length();
178        for (int i = len - 1; i >= 0; i--) {
179            if (!Character.isWhitespace(line.charAt(i))) {
180                break;
181            }
182            len--;
183        }
184        return len;
185    }
186
187    /**
188     * Returns the length of a String prefix with tabs expanded.
189     * Each tab is counted as the number of characters is
190     * takes to jump to the next tab stop.
191     *
192     * @param inputString
193     *            the input String
194     * @param toIdx
195     *            index in string (exclusive) where the calculation stops
196     * @param tabWidth
197     *            the distance between tab stop position.
198     * @return the length of string.substring(0, toIdx) with tabs expanded.
199     */
200    public static int lengthExpandedTabs(String inputString,
201            int toIdx,
202            int tabWidth) {
203        int len = 0;
204        for (int idx = 0; idx < toIdx; idx++) {
205            if (inputString.codePointAt(idx) == '\t') {
206                len = (len / tabWidth + 1) * tabWidth;
207            }
208            else {
209                len++;
210            }
211        }
212        return len;
213    }
214
215    /**
216     * Validates whether passed string is a valid pattern or not.
217     *
218     * @param pattern
219     *            string to validate
220     * @return true if the pattern is valid false otherwise
221     */
222    public static boolean isPatternValid(String pattern) {
223        boolean isValid = true;
224        try {
225            Pattern.compile(pattern);
226        }
227        catch (final PatternSyntaxException ignored) {
228            isValid = false;
229        }
230        return isValid;
231    }
232
233    /**
234     * Returns base class name from qualified name.
235     *
236     * @param type
237     *            the fully qualified name. Cannot be null
238     * @return the base class name from a fully qualified name
239     */
240    public static String baseClassName(String type) {
241        final String className;
242        final int index = type.lastIndexOf('.');
243        if (index == -1) {
244            className = type;
245        }
246        else {
247            className = type.substring(index + 1);
248        }
249        return className;
250    }
251
252    /**
253     * Constructs a normalized relative path between base directory and a given path.
254     *
255     * @param baseDirectory
256     *            the base path to which given path is relativized
257     * @param path
258     *            the path to relativize against base directory
259     * @return the relative normalized path between base directory and
260     *     path or path if base directory is null.
261     */
262    public static String relativizeAndNormalizePath(final String baseDirectory, final String path) {
263        final String resultPath;
264        if (baseDirectory == null) {
265            resultPath = path;
266        }
267        else {
268            final Path pathAbsolute = Paths.get(path).normalize();
269            final Path pathBase = Paths.get(baseDirectory).normalize();
270            resultPath = pathBase.relativize(pathAbsolute).toString();
271        }
272        return resultPath;
273    }
274
275    /**
276     * Tests if this string starts with the specified prefix.
277     * <p>
278     * It is faster version of {@link String#startsWith(String)} optimized for
279     *  one-character prefixes at the expense of
280     * some readability. Suggested by SimplifyStartsWith PMD rule:
281     * http://pmd.sourceforge.net/pmd-5.3.1/pmd-java/rules/java/optimizations.html#SimplifyStartsWith
282     * </p>
283     *
284     * @param value
285     *            the {@code String} to check
286     * @param prefix
287     *            the prefix to find
288     * @return {@code true} if the {@code char} is a prefix of the given {@code String};
289     *     {@code false} otherwise.
290     */
291    public static boolean startsWithChar(String value, char prefix) {
292        return !value.isEmpty() && value.charAt(0) == prefix;
293    }
294
295    /**
296     * Tests if this string ends with the specified suffix.
297     * <p>
298     * It is faster version of {@link String#endsWith(String)} optimized for
299     *  one-character suffixes at the expense of
300     * some readability. Suggested by SimplifyStartsWith PMD rule:
301     * http://pmd.sourceforge.net/pmd-5.3.1/pmd-java/rules/java/optimizations.html#SimplifyStartsWith
302     * </p>
303     *
304     * @param value
305     *            the {@code String} to check
306     * @param suffix
307     *            the suffix to find
308     * @return {@code true} if the {@code char} is a suffix of the given {@code String};
309     *     {@code false} otherwise.
310     */
311    public static boolean endsWithChar(String value, char suffix) {
312        return !value.isEmpty() && value.charAt(value.length() - 1) == suffix;
313    }
314
315    /**
316     * Gets constructor of targetClass.
317     *
318     * @param <T> type of the target class object.
319     * @param targetClass
320     *            from which constructor is returned
321     * @param parameterTypes
322     *            of constructor
323     * @return constructor of targetClass
324     * @throws IllegalStateException if any exception occurs
325     * @see Class#getConstructor(Class[])
326     */
327    public static <T> Constructor<T> getConstructor(Class<T> targetClass,
328                                                    Class<?>... parameterTypes) {
329        try {
330            return targetClass.getConstructor(parameterTypes);
331        }
332        catch (NoSuchMethodException ex) {
333            throw new IllegalStateException(ex);
334        }
335    }
336
337    /**
338     * Returns new instance of a class.
339     *
340     * @param <T>
341     *            type of constructor
342     * @param constructor
343     *            to invoke
344     * @param parameters
345     *            to pass to constructor
346     * @return new instance of class
347     * @throws IllegalStateException if any exception occurs
348     * @see Constructor#newInstance(Object...)
349     */
350    public static <T> T invokeConstructor(Constructor<T> constructor, Object... parameters) {
351        try {
352            return constructor.newInstance(parameters);
353        }
354        catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) {
355            throw new IllegalStateException(ex);
356        }
357    }
358
359    /**
360     * Closes a stream re-throwing IOException as IllegalStateException.
361     *
362     * @param closeable
363     *            Closeable object
364     * @throws IllegalStateException when any IOException occurs
365     */
366    public static void close(Closeable closeable) {
367        if (closeable != null) {
368            try {
369                closeable.close();
370            }
371            catch (IOException ex) {
372                throw new IllegalStateException("Cannot close the stream", ex);
373            }
374        }
375    }
376
377    /**
378     * Resolve the specified filename to a URI.
379     *
380     * @param filename name of the file
381     * @return resolved file URI
382     * @throws CheckstyleException on failure
383     */
384    public static URI getUriByFilename(String filename) throws CheckstyleException {
385        URI uri = getWebOrFileProtocolUri(filename);
386
387        if (uri == null) {
388            uri = getFilepathOrClasspathUri(filename);
389        }
390
391        return uri;
392    }
393
394    /**
395     * Resolves the specified filename containing 'http', 'https', 'ftp',
396     * and 'file' protocols (or any RFC 2396 compliant URL) to a URI.
397     *
398     * @param filename name of the file
399     * @return resolved file URI or null if URL is malformed or non-existent
400     */
401    public static URI getWebOrFileProtocolUri(String filename) {
402        URI uri;
403        try {
404            final URL url = new URL(filename);
405            uri = url.toURI();
406        }
407        catch (URISyntaxException | MalformedURLException ignored) {
408            uri = null;
409        }
410        return uri;
411    }
412
413    /**
414     * Resolves the specified local filename, possibly with 'classpath:'
415     * protocol, to a URI.  First we attempt to create a new file with
416     * given filename, then attempt to load file from class path.
417     *
418     * @param filename name of the file
419     * @return resolved file URI
420     * @throws CheckstyleException on failure
421     */
422    private static URI getFilepathOrClasspathUri(String filename) throws CheckstyleException {
423        final URI uri;
424        final File file = new File(filename);
425
426        if (file.exists()) {
427            uri = file.toURI();
428        }
429        else {
430            final int lastIndexOfClasspathProtocol;
431            if (filename.lastIndexOf(CLASSPATH_URL_PROTOCOL) == 0) {
432                lastIndexOfClasspathProtocol = CLASSPATH_URL_PROTOCOL.length();
433            }
434            else {
435                lastIndexOfClasspathProtocol = 0;
436            }
437            uri = getResourceFromClassPath(filename
438                .substring(lastIndexOfClasspathProtocol));
439        }
440        return uri;
441    }
442
443    /**
444     * Gets a resource from the classpath.
445     *
446     * @param filename name of file
447     * @return URI of file in classpath
448     * @throws CheckstyleException on failure
449     */
450    public static URI getResourceFromClassPath(String filename) throws CheckstyleException {
451        final URL configUrl;
452        if (filename.charAt(0) == '/') {
453            configUrl = getCheckstyleResource(filename);
454        }
455        else {
456            configUrl = ClassLoader.getSystemResource(filename);
457        }
458
459        if (configUrl == null) {
460            throw new CheckstyleException(UNABLE_TO_FIND_EXCEPTION_PREFIX + filename);
461        }
462
463        final URI uri;
464        try {
465            uri = configUrl.toURI();
466        }
467        catch (final URISyntaxException ex) {
468            throw new CheckstyleException(UNABLE_TO_FIND_EXCEPTION_PREFIX + filename, ex);
469        }
470
471        return uri;
472    }
473
474    /**
475     * Finds a resource with a given name in the Checkstyle resource bundle.
476     * This method is intended only for internal use in Checkstyle tests for
477     * easy mocking to gain 100% coverage.
478     *
479     * @param name name of the desired resource
480     * @return URI of the resource
481     */
482    public static URL getCheckstyleResource(String name) {
483        return CommonUtil.class.getResource(name);
484    }
485
486    /**
487     * Puts part of line, which matches regexp into given template
488     * on positions $n where 'n' is number of matched part in line.
489     *
490     * @param template the string to expand.
491     * @param lineToPlaceInTemplate contains expression which should be placed into string.
492     * @param regexp expression to find in comment.
493     * @return the string, based on template filled with given lines
494     */
495    public static String fillTemplateWithStringsByRegexp(
496        String template, String lineToPlaceInTemplate, Pattern regexp) {
497        final Matcher matcher = regexp.matcher(lineToPlaceInTemplate);
498        String result = template;
499        if (matcher.find()) {
500            for (int i = 0; i <= matcher.groupCount(); i++) {
501                // $n expands comment match like in Pattern.subst().
502                result = result.replaceAll("\\$" + i, matcher.group(i));
503            }
504        }
505        return result;
506    }
507
508    /**
509     * Returns file name without extension.
510     * We do not use the method from Guava library to reduce Checkstyle's dependencies
511     * on external libraries.
512     *
513     * @param fullFilename file name with extension.
514     * @return file name without extension.
515     */
516    public static String getFileNameWithoutExtension(String fullFilename) {
517        final String fileName = new File(fullFilename).getName();
518        final int dotIndex = fileName.lastIndexOf('.');
519        final String fileNameWithoutExtension;
520        if (dotIndex == -1) {
521            fileNameWithoutExtension = fileName;
522        }
523        else {
524            fileNameWithoutExtension = fileName.substring(0, dotIndex);
525        }
526        return fileNameWithoutExtension;
527    }
528
529    /**
530     * Returns file extension for the given file name
531     * or empty string if file does not have an extension.
532     * We do not use the method from Guava library to reduce Checkstyle's dependencies
533     * on external libraries.
534     *
535     * @param fileNameWithExtension file name with extension.
536     * @return file extension for the given file name
537     *         or empty string if file does not have an extension.
538     */
539    public static String getFileExtension(String fileNameWithExtension) {
540        final String fileName = Paths.get(fileNameWithExtension).toString();
541        final int dotIndex = fileName.lastIndexOf('.');
542        final String extension;
543        if (dotIndex == -1) {
544            extension = "";
545        }
546        else {
547            extension = fileName.substring(dotIndex + 1);
548        }
549        return extension;
550    }
551
552    /**
553     * Checks whether the given string is a valid identifier.
554     *
555     * @param str A string to check.
556     * @return true when the given string contains valid identifier.
557     */
558    public static boolean isIdentifier(String str) {
559        boolean isIdentifier = !str.isEmpty();
560
561        for (int i = 0; isIdentifier && i < str.length(); i++) {
562            if (i == 0) {
563                isIdentifier = Character.isJavaIdentifierStart(str.charAt(0));
564            }
565            else {
566                isIdentifier = Character.isJavaIdentifierPart(str.charAt(i));
567            }
568        }
569
570        return isIdentifier;
571    }
572
573    /**
574     * Checks whether the given string is a valid name.
575     *
576     * @param str A string to check.
577     * @return true when the given string contains valid name.
578     */
579    public static boolean isName(String str) {
580        boolean isName = !str.isEmpty();
581
582        final String[] identifiers = str.split("\\.", -1);
583        for (int i = 0; isName && i < identifiers.length; i++) {
584            isName = isIdentifier(identifiers[i]);
585        }
586
587        return isName;
588    }
589
590    /**
591     * Checks if the value arg is blank by either being null,
592     * empty, or contains only whitespace characters.
593     *
594     * @param value A string to check.
595     * @return true if the arg is blank.
596     */
597    public static boolean isBlank(String value) {
598        return Objects.isNull(value)
599                || indexOfNonWhitespace(value) >= value.length();
600    }
601
602    /**
603     * Method to find the index of the first non-whitespace character in a string.
604     *
605     * @param value the string to find the first index of a non-whitespace character for.
606     * @return the index of the first non-whitespace character.
607     */
608    public static int indexOfNonWhitespace(String value) {
609        final int length = value.length();
610        int left = 0;
611        while (left < length) {
612            final int codePointAt = value.codePointAt(left);
613            if (!Character.isWhitespace(codePointAt)) {
614                break;
615            }
616            left += Character.charCount(codePointAt);
617        }
618        return left;
619    }
620
621    /**
622     * Checks whether the string contains an integer value.
623     *
624     * @param str a string to check
625     * @return true if the given string is an integer, false otherwise.
626     */
627    public static boolean isInt(String str) {
628        boolean isInt;
629        if (str == null) {
630            isInt = false;
631        }
632        else {
633            try {
634                Integer.parseInt(str);
635                isInt = true;
636            }
637            catch (NumberFormatException ignored) {
638                isInt = false;
639            }
640        }
641        return isInt;
642    }
643
644    /**
645     * Converts the Unicode code point at index {@code index} to it's UTF-16
646     * representation, then checks if the character is whitespace. Note that the given
647     * index {@code index} should correspond to the location of the character
648     * to check in the string, not in code points.
649     *
650     * @param codePoints the array of Unicode code points
651     * @param index the index of the character to check
652     * @return true if character at {@code index} is whitespace
653     */
654    public static boolean isCodePointWhitespace(int[] codePoints, int index) {
655        //  We only need to check the first member of a surrogate pair to verify that
656        //  it is not whitespace.
657        final char character = Character.toChars(codePoints[index])[0];
658        return Character.isWhitespace(character);
659    }
660
661}