forked from checkstyle/checkstyle
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue checkstyle#4530: Add the new option for Checkstyle CLI to gener…
…ate the basic suppression xpath
- Loading branch information
Showing
10 changed files
with
431 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/main/java/com/puppycrawl/tools/checkstyle/SuppressionsStringPrinter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2018 the original author or authors. | ||
// | ||
// This library is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 2.1 of the License, or (at your option) any later version. | ||
// | ||
// This library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public | ||
// License along with this library; if not, write to the Free Software | ||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
package com.puppycrawl.tools.checkstyle; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
import com.puppycrawl.tools.checkstyle.api.CheckstyleException; | ||
import com.puppycrawl.tools.checkstyle.api.DetailAST; | ||
import com.puppycrawl.tools.checkstyle.api.FileText; | ||
import com.puppycrawl.tools.checkstyle.xpath.XpathQueryGenerator; | ||
|
||
/** | ||
* Class for constructing xpath queries to suppress nodes | ||
* with specified line and column number. | ||
* @author Timur Tibeyev. | ||
*/ | ||
public final class SuppressionsStringPrinter { | ||
|
||
/** Line and column number config value pattern. */ | ||
private static final Pattern VALID_SUPPRESSION_LINE_COLUMN_NUMBER_REGEX = | ||
Pattern.compile("^([0-9]+):([0-9]+)$"); | ||
|
||
/** Prevent instances. */ | ||
private SuppressionsStringPrinter() { | ||
// no code | ||
} | ||
|
||
/** | ||
* Prints generated suppressions. | ||
* @param file the file to process. | ||
* @param suppressionLineColumnNumber line and column number of the suppression | ||
* @param tabWidth length of the tab character | ||
* @return generated suppressions. | ||
* @throws IOException if the file could not be read. | ||
* @throws CheckstyleException if the file is not a Java source. | ||
*/ | ||
public static String printSuppressions(File file, String suppressionLineColumnNumber, | ||
int tabWidth) throws IOException, CheckstyleException { | ||
final Matcher matcher = | ||
VALID_SUPPRESSION_LINE_COLUMN_NUMBER_REGEX.matcher(suppressionLineColumnNumber); | ||
if (matcher.matches()) { | ||
final FileText fileText = new FileText(file.getAbsoluteFile(), | ||
System.getProperty("file.encoding", StandardCharsets.UTF_8.name())); | ||
final DetailAST detailAST = | ||
JavaParser.parseFileText(fileText, JavaParser.Options.WITH_COMMENTS); | ||
final int lineNumber = Integer.parseInt(matcher.group(1)); | ||
final int columnNumber = Integer.parseInt(matcher.group(2)); | ||
return generate(fileText, detailAST, lineNumber, columnNumber, tabWidth); | ||
} | ||
else { | ||
final String exceptionMsg = String.format(Locale.ROOT, | ||
"%s does not match valid format 'line:column'.", | ||
suppressionLineColumnNumber); | ||
throw new IllegalStateException(exceptionMsg); | ||
} | ||
} | ||
|
||
/** | ||
* Creates {@code XpathQueryGenerator} instance and generates suppressions. | ||
* @param fileText {@code FileText} object. | ||
* @param detailAST {@code DetailAST} object. | ||
* @param lineNumber line number. | ||
* @param columnNumber column number. | ||
* @param tabWidth length of the tab character. | ||
* @return generated suppressions. | ||
*/ | ||
private static String generate(FileText fileText, DetailAST detailAST, int lineNumber, | ||
int columnNumber, int tabWidth) { | ||
final XpathQueryGenerator queryGenerator = | ||
new XpathQueryGenerator(detailAST, lineNumber, columnNumber, fileText, | ||
tabWidth); | ||
final List<String> suppressions = queryGenerator.generate(); | ||
return suppressions.stream().collect(Collectors.joining(System.lineSeparator(), | ||
"", System.lineSeparator())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.