Search in sources :

Example 1 with Filter

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

the class Checker method process.

@Override
public int process(List<File> files) throws CheckstyleException {
    if (cacheFile != null) {
        cacheFile.putExternalResources(getExternalResourceLocations());
    }
    // Prepare to start
    fireAuditStarted();
    for (final FileSetCheck fsc : fileSetChecks) {
        fsc.beginProcessing(charset);
    }
    final List<File> targetFiles = files.stream().filter(file -> CommonUtil.matchesFileExtension(file, fileExtensions)).collect(Collectors.toList());
    processFiles(targetFiles);
    // Finish up
    // It may also log!!!
    fileSetChecks.forEach(FileSetCheck::finishProcessing);
    // It may also log!!!
    fileSetChecks.forEach(FileSetCheck::destroy);
    final int errorCount = counter.getCount();
    fireAuditFinished();
    return errorCount;
}
Also used : Context(com.puppycrawl.tools.checkstyle.api.Context) SortedSet(java.util.SortedSet) FilterSet(com.puppycrawl.tools.checkstyle.api.FilterSet) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener) BeforeExecutionFileFilter(com.puppycrawl.tools.checkstyle.api.BeforeExecutionFileFilter) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Charset(java.nio.charset.Charset) FileSetCheck(com.puppycrawl.tools.checkstyle.api.FileSetCheck) Locale(java.util.Locale) MessageDispatcher(com.puppycrawl.tools.checkstyle.api.MessageDispatcher) Violation(com.puppycrawl.tools.checkstyle.api.Violation) PrintWriter(java.io.PrintWriter) Filter(com.puppycrawl.tools.checkstyle.api.Filter) BeforeExecutionFileFilterSet(com.puppycrawl.tools.checkstyle.api.BeforeExecutionFileFilterSet) CommonUtil(com.puppycrawl.tools.checkstyle.utils.CommonUtil) StringWriter(java.io.StringWriter) Set(java.util.Set) IOException(java.io.IOException) ExternalResourceHolder(com.puppycrawl.tools.checkstyle.api.ExternalResourceHolder) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) FileText(com.puppycrawl.tools.checkstyle.api.FileText) List(java.util.List) Stream(java.util.stream.Stream) AutomaticBean(com.puppycrawl.tools.checkstyle.api.AutomaticBean) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) SeverityLevelCounter(com.puppycrawl.tools.checkstyle.api.SeverityLevelCounter) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RootModule(com.puppycrawl.tools.checkstyle.api.RootModule) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) SeverityLevel(com.puppycrawl.tools.checkstyle.api.SeverityLevel) FileSetCheck(com.puppycrawl.tools.checkstyle.api.FileSetCheck) File(java.io.File)

Example 2 with Filter

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

the class Checker method setupChild.

/**
 * {@inheritDoc} Creates child module.
 *
 * @noinspection ChainOfInstanceofChecks
 */
@Override
protected void setupChild(Configuration childConf) throws CheckstyleException {
    final String name = childConf.getName();
    final Object child;
    try {
        child = moduleFactory.createModule(name);
        if (child instanceof AutomaticBean) {
            final AutomaticBean bean = (AutomaticBean) child;
            bean.contextualize(childContext);
            bean.configure(childConf);
        }
    } catch (final CheckstyleException ex) {
        throw new CheckstyleException("cannot initialize module " + name + " - " + ex.getMessage(), ex);
    }
    if (child instanceof FileSetCheck) {
        final FileSetCheck fsc = (FileSetCheck) child;
        fsc.init();
        addFileSetCheck(fsc);
    } else if (child instanceof BeforeExecutionFileFilter) {
        final BeforeExecutionFileFilter filter = (BeforeExecutionFileFilter) child;
        addBeforeExecutionFileFilter(filter);
    } else if (child instanceof Filter) {
        final Filter filter = (Filter) child;
        addFilter(filter);
    } else if (child instanceof AuditListener) {
        final AuditListener listener = (AuditListener) child;
        addListener(listener);
    } else {
        throw new CheckstyleException(name + " is not allowed as a child in Checker");
    }
}
Also used : FileSetCheck(com.puppycrawl.tools.checkstyle.api.FileSetCheck) BeforeExecutionFileFilter(com.puppycrawl.tools.checkstyle.api.BeforeExecutionFileFilter) Filter(com.puppycrawl.tools.checkstyle.api.Filter) BeforeExecutionFileFilter(com.puppycrawl.tools.checkstyle.api.BeforeExecutionFileFilter) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener) AutomaticBean(com.puppycrawl.tools.checkstyle.api.AutomaticBean)

Example 3 with Filter

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

the class CheckerTest method testDuplicatedModule.

@Test
public void testDuplicatedModule() throws Exception {
    // we need to test a module with two instances, one with id and the other not
    final DefaultConfiguration moduleConfig1 = createModuleConfig(NewlineAtEndOfFileCheck.class);
    final DefaultConfiguration moduleConfig2 = createModuleConfig(NewlineAtEndOfFileCheck.class);
    moduleConfig2.addProperty("id", "ModuleId");
    final DefaultConfiguration root = new DefaultConfiguration("root");
    root.addChild(moduleConfig1);
    root.addChild(moduleConfig2);
    final Checker checker = new Checker();
    checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
    checker.configure(root);
    // BriefUtLogger does not print the module name or id postfix,
    // so we need to set logger manually
    final ByteArrayOutputStream out = TestUtil.getInternalState(this, "stream");
    final DefaultLogger logger = new DefaultLogger(out, OutputStreamOptions.CLOSE, out, OutputStreamOptions.NONE, new AuditEventDefaultFormatter());
    checker.addListener(logger);
    final String path = File.createTempFile("file", ".java", temporaryFolder).getPath();
    final String violationMessage = getCheckMessage(NewlineAtEndOfFileCheck.class, MSG_KEY_NO_NEWLINE_EOF);
    final String[] expected = { "1: " + violationMessage + " [NewlineAtEndOfFile]", "1: " + violationMessage + " [ModuleId]" };
    // super.verify does not work here, for we change the logger
    out.flush();
    final int errs = checker.process(Collections.singletonList(new File(path)));
    try (ByteArrayInputStream inputStream = new ByteArrayInputStream(out.toByteArray());
        LineNumberReader lnr = new LineNumberReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
        // we need to ignore the unrelated lines
        final List<String> actual = lnr.lines().filter(line -> !getCheckMessage(AUDIT_STARTED_MESSAGE).equals(line)).filter(line -> !getCheckMessage(AUDIT_FINISHED_MESSAGE).equals(line)).limit(expected.length).sorted().collect(Collectors.toList());
        Arrays.sort(expected);
        for (int i = 0; i < expected.length; i++) {
            final String expectedResult = "[ERROR] " + path + ":" + expected[i];
            assertWithMessage("error message " + i).that(actual.get(i)).isEqualTo(expectedResult);
        }
        assertWithMessage("unexpected output: " + lnr.readLine()).that(errs).isEqualTo(expected.length);
    }
    checker.destroy();
}
Also used : Context(com.puppycrawl.tools.checkstyle.api.Context) Arrays(java.util.Arrays) SortedSet(java.util.SortedSet) FilterSet(com.puppycrawl.tools.checkstyle.api.FilterSet) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener) AUDIT_STARTED_MESSAGE(com.puppycrawl.tools.checkstyle.DefaultLogger.AUDIT_STARTED_MESSAGE) CloseAndFlushTestByteArrayOutputStream(com.puppycrawl.tools.checkstyle.internal.utils.CloseAndFlushTestByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Locale(java.util.Locale) OutputStreamOptions(com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions) MSG_KEY_NO_NEWLINE_EOF(com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck.MSG_KEY_NO_NEWLINE_EOF) Method(java.lang.reflect.Method) SuppressionFilter(com.puppycrawl.tools.checkstyle.filters.SuppressionFilter) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) AbstractCheck(com.puppycrawl.tools.checkstyle.api.AbstractCheck) TestFileSetCheck(com.puppycrawl.tools.checkstyle.internal.testmodules.TestFileSetCheck) CommonUtil(com.puppycrawl.tools.checkstyle.utils.CommonUtil) AbstractFileSetCheck(com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck) Set(java.util.Set) LineNumberReader(java.io.LineNumberReader) ExternalResourceHolder(com.puppycrawl.tools.checkstyle.api.ExternalResourceHolder) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) TokenTypes(com.puppycrawl.tools.checkstyle.api.TokenTypes) List(java.util.List) TempDir(org.junit.jupiter.api.io.TempDir) DebugAuditAdapter(com.puppycrawl.tools.checkstyle.internal.testmodules.DebugAuditAdapter) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) HiddenFieldCheck(com.puppycrawl.tools.checkstyle.checks.coding.HiddenFieldCheck) TestUtil(com.puppycrawl.tools.checkstyle.internal.utils.TestUtil) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EXCEPTION_MSG(com.puppycrawl.tools.checkstyle.Checker.EXCEPTION_MSG) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TestBeforeExecutionFileFilter(com.puppycrawl.tools.checkstyle.internal.testmodules.TestBeforeExecutionFileFilter) MessageDispatcher(com.puppycrawl.tools.checkstyle.api.MessageDispatcher) Violation(com.puppycrawl.tools.checkstyle.api.Violation) TranslationCheck(com.puppycrawl.tools.checkstyle.checks.TranslationCheck) Filter(com.puppycrawl.tools.checkstyle.api.Filter) Properties(java.util.Properties) Files(java.nio.file.Files) IOException(java.io.IOException) AUDIT_FINISHED_MESSAGE(com.puppycrawl.tools.checkstyle.DefaultLogger.AUDIT_FINISHED_MESSAGE) Field(java.lang.reflect.Field) InputStreamReader(java.io.InputStreamReader) File(java.io.File) FileText(com.puppycrawl.tools.checkstyle.api.FileText) IOError(java.io.IOError) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) NewlineAtEndOfFileCheck(com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck) BufferedReader(java.io.BufferedReader) DebugFilter(com.puppycrawl.tools.checkstyle.internal.testmodules.DebugFilter) Collections(java.util.Collections) InputStream(java.io.InputStream) InputStreamReader(java.io.InputStreamReader) CloseAndFlushTestByteArrayOutputStream(com.puppycrawl.tools.checkstyle.internal.utils.CloseAndFlushTestByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LineNumberReader(java.io.LineNumberReader) ByteArrayInputStream(java.io.ByteArrayInputStream) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

AuditListener (com.puppycrawl.tools.checkstyle.api.AuditListener)3 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)3 Filter (com.puppycrawl.tools.checkstyle.api.Filter)3 AuditEvent (com.puppycrawl.tools.checkstyle.api.AuditEvent)2 AutomaticBean (com.puppycrawl.tools.checkstyle.api.AutomaticBean)2 BeforeExecutionFileFilter (com.puppycrawl.tools.checkstyle.api.BeforeExecutionFileFilter)2 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)2 Context (com.puppycrawl.tools.checkstyle.api.Context)2 ExternalResourceHolder (com.puppycrawl.tools.checkstyle.api.ExternalResourceHolder)2 FileSetCheck (com.puppycrawl.tools.checkstyle.api.FileSetCheck)2 FileText (com.puppycrawl.tools.checkstyle.api.FileText)2 FilterSet (com.puppycrawl.tools.checkstyle.api.FilterSet)2 MessageDispatcher (com.puppycrawl.tools.checkstyle.api.MessageDispatcher)2 Violation (com.puppycrawl.tools.checkstyle.api.Violation)2 CommonUtil (com.puppycrawl.tools.checkstyle.utils.CommonUtil)2 File (java.io.File)2 IOException (java.io.IOException)2 Truth.assertWithMessage (com.google.common.truth.Truth.assertWithMessage)1 EXCEPTION_MSG (com.puppycrawl.tools.checkstyle.Checker.EXCEPTION_MSG)1 AUDIT_FINISHED_MESSAGE (com.puppycrawl.tools.checkstyle.DefaultLogger.AUDIT_FINISHED_MESSAGE)1