Search in sources :

Example 11 with FileText

use of com.puppycrawl.tools.checkstyle.api.FileText in project checkstyle by checkstyle.

the class SuppressionsStringPrinter method printSuppressions.

/**
 * 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 IllegalStateException if suppressionLineColumnNumber is not of a valid format.
 * @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 String exceptionMsg = String.format(Locale.ROOT, "%s does not match valid format 'line:column'.", suppressionLineColumnNumber);
        throw new IllegalStateException(exceptionMsg);
    }
    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);
}
Also used : Matcher(java.util.regex.Matcher) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) FileText(com.puppycrawl.tools.checkstyle.api.FileText)

Example 12 with FileText

use of com.puppycrawl.tools.checkstyle.api.FileText in project checkstyle by checkstyle.

the class RegexpCheck method findMatch.

/**
 * Recursive method that finds the matches.
 */
// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
@SuppressWarnings("deprecation")
private void findMatch() {
    final boolean foundMatch = matcher.find();
    if (foundMatch) {
        final FileText text = getFileContents().getText();
        final LineColumn start = text.lineColumn(matcher.start());
        final int startLine = start.getLine();
        final boolean ignore = isIgnore(startLine, text, start);
        if (!ignore) {
            matchCount++;
            if (illegalPattern || checkForDuplicates && matchCount - 1 > duplicateLimit) {
                errorCount++;
                logMessage(startLine);
            }
        }
        if (canContinueValidation(ignore)) {
            findMatch();
        }
    } else if (!illegalPattern && matchCount == 0) {
        logMessage(0);
    }
}
Also used : LineColumn(com.puppycrawl.tools.checkstyle.api.LineColumn) FileText(com.puppycrawl.tools.checkstyle.api.FileText)

Example 13 with FileText

use of com.puppycrawl.tools.checkstyle.api.FileText in project checkstyle by checkstyle.

the class SuppressWithPlainTextCommentFilter method getFileText.

/**
 * Returns {@link FileText} instance created based on the given file name.
 *
 * @param fileName the name of the file.
 * @return {@link FileText} instance.
 * @throws IllegalStateException if the file could not be read.
 */
private static FileText getFileText(String fileName) {
    final File file = new File(fileName);
    FileText result = null;
    // some violations can be on a directory, instead of a file
    if (!file.isDirectory()) {
        try {
            result = new FileText(file, StandardCharsets.UTF_8.name());
        } catch (IOException ex) {
            throw new IllegalStateException("Cannot read source file: " + fileName, ex);
        }
    }
    return result;
}
Also used : FileText(com.puppycrawl.tools.checkstyle.api.FileText) IOException(java.io.IOException) File(java.io.File)

Example 14 with FileText

use of com.puppycrawl.tools.checkstyle.api.FileText in project checkstyle by checkstyle.

the class MultilineDetector method processLines.

/**
 * Processes an entire text file looking for matches.
 *
 * @param fileText the text to process
 */
public void processLines(FileText fileText) {
    text = new FileText(fileText);
    resetState();
    final String format = options.getFormat();
    if (format == null || format.isEmpty()) {
        options.getReporter().log(1, MSG_EMPTY);
    } else {
        matcher = options.getPattern().matcher(fileText.getFullText());
        findMatch();
        finish();
    }
}
Also used : FileText(com.puppycrawl.tools.checkstyle.api.FileText)

Example 15 with FileText

use of com.puppycrawl.tools.checkstyle.api.FileText in project checkstyle by checkstyle.

the class XpathFileGeneratorAuditListenerTest method createTreeWalkerAuditEvent.

private static TreeWalkerAuditEvent createTreeWalkerAuditEvent(String fileName, Violation violation) throws Exception {
    final File file = new File(getPath(fileName));
    final FileText fileText = new FileText(file.getAbsoluteFile(), System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
    final FileContents fileContents = new FileContents(fileText);
    final DetailAST rootAst = JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS);
    return new TreeWalkerAuditEvent(fileContents, fileName, violation, rootAst);
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File)

Aggregations

FileText (com.puppycrawl.tools.checkstyle.api.FileText)49 File (java.io.File)41 Test (org.junit.jupiter.api.Test)29 FileContents (com.puppycrawl.tools.checkstyle.api.FileContents)18 DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)15 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)7 Violation (com.puppycrawl.tools.checkstyle.api.Violation)7 ArrayList (java.util.ArrayList)6 TreeWalkerAuditEvent (com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent)5 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)4 TypeNameCheck (com.puppycrawl.tools.checkstyle.checks.naming.TypeNameCheck)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 TestLoggingReporter (com.puppycrawl.tools.checkstyle.internal.testmodules.TestLoggingReporter)2 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2 RecognitionException (antlr.RecognitionException)1 TokenStreamException (antlr.TokenStreamException)1 TokenStreamRecognitionException (antlr.TokenStreamRecognitionException)1 ClassPath (com.google.common.reflect.ClassPath)1 LineColumn (com.puppycrawl.tools.checkstyle.api.LineColumn)1