use of com.puppycrawl.tools.checkstyle.api.AuditListener 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