use of com.puppycrawl.tools.checkstyle.api.FilterSet in project checkstyle by checkstyle.
the class FilterSetTest method testGetFilters2.
@Test
public void testGetFilters2() {
final FilterSet filterSet = new FilterSet();
filterSet.addFilter(new SeverityMatchFilter());
assertEquals("size is the same", 1, filterSet.getFilters().size());
}
use of com.puppycrawl.tools.checkstyle.api.FilterSet in project checkstyle by checkstyle.
the class SuppressionsLoaderTest method loadFilterSet.
private static FilterSet loadFilterSet(String url) throws Exception {
FilterSet filterSet = null;
if (isUrlReachable(url)) {
int attemptCount = 0;
final int attemptLimit = 5;
while (attemptCount <= attemptLimit) {
try {
filterSet = SuppressionsLoader.loadSuppressions(url);
break;
} catch (CheckstyleException ex) {
// for some reason Travis CI failed some times(unstable) on reading this file
if (attemptCount < attemptLimit && ex.getMessage().contains("Unable to read")) {
attemptCount++;
// wait for bad/disconnection time to pass
Thread.sleep(1000);
} else {
throw ex;
}
}
}
}
return filterSet;
}
use of com.puppycrawl.tools.checkstyle.api.FilterSet in project checkstyle by checkstyle.
the class SuppressionsLoaderTest method testNoSuppressions.
@Test
public void testNoSuppressions() throws Exception {
final FilterSet fc = SuppressionsLoader.loadSuppressions(getPath("InputSuppressionsLoaderNone.xml"));
final FilterSet fc2 = new FilterSet();
assertWithMessage("No suppressions should be loaded, but found: " + fc.getFilters().size()).that(fc.getFilters()).isEqualTo(fc2.getFilters());
}
use of com.puppycrawl.tools.checkstyle.api.FilterSet in project checkstyle by checkstyle.
the class SuppressionsLoaderTest method testNoCheckYesId.
@Test
public void testNoCheckYesId() throws Exception {
final String fn = getPath("InputSuppressionsLoaderId.xml");
final FilterSet set = SuppressionsLoader.loadSuppressions(fn);
assertWithMessage("Invalid number of filters").that(set.getFilters()).hasSize(1);
}
use of com.puppycrawl.tools.checkstyle.api.FilterSet in project maven-plugins by apache.
the class DefaultCheckstyleExecutor method executeCheckstyle.
public CheckstyleResults executeCheckstyle(CheckstyleExecutorRequest request) throws CheckstyleExecutorException, CheckstyleException {
if (getLogger().isDebugEnabled()) {
getLogger().debug("executeCheckstyle start headerLocation : " + request.getHeaderLocation());
}
MavenProject project = request.getProject();
configureResourceLocator(locator, request, null);
configureResourceLocator(licenseLocator, request, request.getLicenseArtifacts());
// Config is less critical than License, locator can still be used.
// configureResourceLocator( configurationLocator, request, request.getConfigurationArtifacts() );
List<File> files;
try {
files = getFilesToProcess(request);
} catch (IOException e) {
throw new CheckstyleExecutorException("Error getting files to process", e);
}
final String suppressionsFilePath = getSuppressionsFilePath(request);
FilterSet filterSet = getSuppressionsFilterSet(suppressionsFilePath);
Checker checker = new Checker();
// setup classloader, needed to avoid "Unable to get class information for ..." errors
List<String> classPathStrings = new ArrayList<>();
List<String> outputDirectories = new ArrayList<>();
// stand-alone
Collection<File> sourceDirectories = null;
Collection<File> testSourceDirectories = request.getTestSourceDirectories();
// aggregator
Map<MavenProject, Collection<File>> sourceDirectoriesByProject = new HashMap<>();
Map<MavenProject, Collection<File>> testSourceDirectoriesByProject = new HashMap<>();
if (request.isAggregate()) {
for (MavenProject childProject : request.getReactorProjects()) {
sourceDirectories = new ArrayList<>(childProject.getCompileSourceRoots().size());
List<String> compileSourceRoots = childProject.getCompileSourceRoots();
for (String compileSourceRoot : compileSourceRoots) {
sourceDirectories.add(new File(compileSourceRoot));
}
sourceDirectoriesByProject.put(childProject, sourceDirectories);
testSourceDirectories = new ArrayList<>(childProject.getTestCompileSourceRoots().size());
List<String> testCompileSourceRoots = childProject.getTestCompileSourceRoots();
for (String testCompileSourceRoot : testCompileSourceRoots) {
testSourceDirectories.add(new File(testCompileSourceRoot));
}
testSourceDirectoriesByProject.put(childProject, testSourceDirectories);
prepareCheckstylePaths(request, childProject, classPathStrings, outputDirectories, sourceDirectories, testSourceDirectories);
}
} else {
sourceDirectories = request.getSourceDirectories();
prepareCheckstylePaths(request, project, classPathStrings, outputDirectories, sourceDirectories, testSourceDirectories);
}
final List<URL> urls = new ArrayList<>(classPathStrings.size());
for (String path : classPathStrings) {
try {
urls.add(new File(path).toURI().toURL());
} catch (MalformedURLException e) {
throw new CheckstyleExecutorException(e.getMessage(), e);
}
}
for (String outputDirectoryString : outputDirectories) {
try {
if (outputDirectoryString != null) {
File outputDirectoryFile = new File(outputDirectoryString);
if (outputDirectoryFile.exists()) {
URL outputDirectoryUrl = outputDirectoryFile.toURI().toURL();
getLogger().debug("Adding the outputDirectory " + outputDirectoryUrl.toString() + " to the Checkstyle class path");
urls.add(outputDirectoryUrl);
}
}
} catch (MalformedURLException e) {
throw new CheckstyleExecutorException(e.getMessage(), e);
}
}
URLClassLoader projectClassLoader = AccessController.doPrivileged(new PrivilegedAction<URLClassLoader>() {
public URLClassLoader run() {
return new URLClassLoader(urls.toArray(new URL[urls.size()]), null);
}
});
checker.setClassLoader(projectClassLoader);
checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
if (filterSet != null) {
checker.addFilter(filterSet);
}
Configuration configuration = getConfiguration(request);
checker.configure(configuration);
AuditListener listener = request.getListener();
if (listener != null) {
checker.addListener(listener);
}
if (request.isConsoleOutput()) {
checker.addListener(request.getConsoleListener());
}
CheckstyleCheckerListener checkerListener = new CheckstyleCheckerListener(configuration);
if (request.isAggregate()) {
for (MavenProject childProject : request.getReactorProjects()) {
sourceDirectories = sourceDirectoriesByProject.get(childProject);
testSourceDirectories = testSourceDirectoriesByProject.get(childProject);
addSourceDirectory(checkerListener, sourceDirectories, testSourceDirectories, childProject.getResources(), request);
}
} else {
addSourceDirectory(checkerListener, sourceDirectories, testSourceDirectories, request.getResources(), request);
}
checker.addListener(checkerListener);
int nbErrors = checker.process(files);
checker.destroy();
if (projectClassLoader instanceof Closeable) {
try {
((Closeable) projectClassLoader).close();
} catch (IOException ex) {
// Nothing we can do - and not detrimental to the build (save running out of file handles).
getLogger().info("Failed to close custom Classloader - this indicated a bug in the code.", ex);
}
}
if (request.getStringOutputStream() != null) {
String message = request.getStringOutputStream().toString().trim();
if (message.length() > 0) {
getLogger().info(message);
}
}
if (nbErrors > 0) {
StringBuilder message = new StringBuilder("There ");
if (nbErrors == 1) {
message.append("is");
} else {
message.append("are");
}
message.append(" ");
message.append(nbErrors);
message.append(" error");
if (nbErrors != 1) {
message.append("s");
}
message.append(" reported by Checkstyle");
String version = getCheckstyleVersion();
if (version != null) {
message.append(" ");
message.append(version);
}
message.append(" with ");
message.append(request.getConfigLocation());
message.append(" ruleset.");
if (request.isFailsOnError()) {
// work regardless of config), but should record this information
throw new CheckstyleExecutorException(message.toString());
} else {
getLogger().info(message.toString());
}
}
return checkerListener.getResults();
}
Aggregations