use of net.sourceforge.pmd.util.ClasspathClassLoader in project pmd by pmd.
the class PMD method doPMD.
/**
* This method is the main entry point for command line usage.
*
* @param configuration
* the configure to use
* @return number of violations found.
*/
public static int doPMD(PMDConfiguration configuration) {
// Load the RuleSets
RuleSetFactory ruleSetFactory = RulesetsFactoryUtils.getRulesetFactory(configuration, new ResourceLoader());
RuleSets ruleSets = RulesetsFactoryUtils.getRuleSetsWithBenchmark(configuration.getRuleSets(), ruleSetFactory);
if (ruleSets == null) {
return 0;
}
Set<Language> languages = getApplicableLanguages(configuration, ruleSets);
List<DataSource> files = getApplicableFiles(configuration, languages);
long reportStart = System.nanoTime();
try {
Renderer renderer = configuration.createRenderer();
List<Renderer> renderers = Collections.singletonList(renderer);
renderer.setWriter(IOUtil.createWriter(configuration.getReportFile()));
renderer.start();
Benchmarker.mark(Benchmark.Reporting, System.nanoTime() - reportStart, 0);
RuleContext ctx = new RuleContext();
final AtomicInteger violations = new AtomicInteger(0);
ctx.getReport().addListener(new ThreadSafeReportListener() {
@Override
public void ruleViolationAdded(RuleViolation ruleViolation) {
violations.incrementAndGet();
}
@Override
public void metricAdded(Metric metric) {
// ignored - not needed for counting violations
}
});
processFiles(configuration, ruleSetFactory, files, ctx, renderers);
reportStart = System.nanoTime();
renderer.end();
renderer.flush();
return violations.get();
} catch (Exception e) {
String message = e.getMessage();
if (message != null) {
LOG.severe(message);
} else {
LOG.log(Level.SEVERE, "Exception during processing", e);
}
LOG.log(Level.FINE, "Exception during processing", e);
LOG.info(PMDCommandLineInterface.buildUsageText());
return 0;
} finally {
Benchmarker.mark(Benchmark.Reporting, System.nanoTime() - reportStart, 0);
/*
* Make sure it's our own classloader before attempting to close it....
* Maven + Jacoco provide us with a cloaseable classloader that if closed
* will throw a ClassNotFoundException.
*/
if (configuration.getClassLoader() instanceof ClasspathClassLoader) {
IOUtil.tryCloseClassLoader(configuration.getClassLoader());
}
}
}
use of net.sourceforge.pmd.util.ClasspathClassLoader in project Gargoyle by callakrsos.
the class DoPMD method processFiles.
/**
* Run PMD on a list of files using multiple threads - if more than one is available
*
* @param configuration
* Configuration
* @param ruleSetFactory
* RuleSetFactory
* @param files
* List<DataSource>
* @param ctx
* RuleContext
* @param renderers
* List<Renderer>
*/
public void processFiles(final GargoylePMDConfiguration configuration, final RuleSetFactory ruleSetFactory, final List<DataSource> files, final RuleContext ctx, final List<Renderer> renderers) {
sortFiles(configuration, files);
/*
* Check if multithreaded support is available. ExecutorService can also
* be disabled if threadCount is not positive, e.g. using the
* "-threads 0" command line option.
*/
// if (SystemUtils.MT_SUPPORTED && configuration.getThreads() > 0) {
// MultiThreadProcessor multiThreadProcessor = new MultiThreadProcessor(configuration);
// multiThreadProcessor.processFiles(ruleSetFactory, files, ctx, renderers);
// } else {
pmdGargoyleThreadProcessor = new PMDGargoyleThreadProcessor(configuration);
pmdGargoyleThreadProcessor.processFiles(ruleSetFactory, files, ctx, renderers);
if (configuration.getClassLoader() instanceof ClasspathClassLoader) {
IOUtil.tryCloseClassLoader(configuration.getClassLoader());
}
}
use of net.sourceforge.pmd.util.ClasspathClassLoader in project pmd by pmd.
the class PMDTaskImpl method execute.
public void execute() throws BuildException {
final Handler antLogHandler = new AntLogHandler(project);
final ScopedLogHandlersManager logManager = new ScopedLogHandlersManager(Level.FINEST, antLogHandler);
try {
doTask();
} finally {
logManager.close();
// exceptions
if (configuration.getClassLoader() instanceof ClasspathClassLoader) {
IOUtil.tryCloseClassLoader(configuration.getClassLoader());
}
}
}
Aggregations