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;
}
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");
}
}
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();
}
Aggregations