use of net.sourceforge.pmd.RuleSets in project Gargoyle by callakrsos.
the class PMDGargoyleThreadProcessor method processFiles.
public void processFiles(RuleSetFactory ruleSetFactory, List<DataSource> files, RuleContext ctx, List<Renderer> renderers) {
// single threaded execution
RuleSets rs = createRuleSets(ruleSetFactory);
SourceCodeProcessor processor = new SourceCodeProcessor(configuration);
for (DataSource dataSource : files) {
String niceFileName = filenameFrom(dataSource);
Report report = PMD.setupReport(rs, ctx, niceFileName);
// overtake the listener
//bug fix 2016-10-05 by kyj. 결과가 중복되서 출력됨.
// report.addSynchronizedListeners(ctx.getReport().getSynchronizedListeners());
// ctx.setReport(report);
// ctx.setSourceCodeFilename(niceFileName);
// if (LOG.isLoggable(Level.FINE)) {
// LOG.fine("Processing " + ctx.getSourceCodeFilename());
// }
rs.start(ctx);
for (Renderer r : renderers) {
r.startFileAnalysis(dataSource);
}
try {
InputStream stream = new BufferedInputStream(dataSource.getInputStream());
// ctx.setLanguageVersion(null);
processor.processSourceCode(stream, rs, ctx);
} catch (PMDException pmde) {
// LOGGER.error(ValueUtil.toString(pmde));
// if (LOG.isLoggable(Level.FINE)) {
// LOG.log(Level.FINE, "Error while processing file: " + niceFileName, pmde.getCause());
// }
report.addError(new Report.ProcessingError(pmde.getMessage(), niceFileName));
} catch (IOException ioe) {
// LOGGER.error(ValueUtil.toString(ioe));
// unexpected exception: log and stop executor service
addError(report, "Unable to read source file", ioe, niceFileName);
} catch (RuntimeException re) {
// LOGGER.error(ValueUtil.toString(re));
// unexpected exception: log and stop executor service
addError(report, "RuntimeException while processing file", re, niceFileName);
} catch (Exception e) {
LOGGER.error(ValueUtil.toString(e));
}
rs.end(ctx);
super.renderReports(renderers, ctx.getReport());
}
}
use of net.sourceforge.pmd.RuleSets in project Gargoyle by callakrsos.
the class RulesetsFactoryUtils method getRuleSetsWithBenchmark.
/**
* See {@link #getRuleSets(String, RuleSetFactory)}. In addition, the loading of the rules is benchmarked.
*
* @param rulesets
* the string with the rulesets to load
* @param factory
* the ruleset factory
* @return the rulesets
* @throws IllegalArgumentException
* if rulesets is empty (means, no rules have been found) or if a ruleset couldn't be found.
*/
public static RuleSets getRuleSetsWithBenchmark(String rulesets, RuleSetFactory factory) {
long loadRuleStart = System.nanoTime();
RuleSets ruleSets = null;
try {
ruleSets = getRuleSets(rulesets, factory);
} finally {
long endLoadRules = System.nanoTime();
Benchmarker.mark(Benchmark.LoadRules, endLoadRules - loadRuleStart, 0);
}
return ruleSets;
}
use of net.sourceforge.pmd.RuleSets in project Gargoyle by callakrsos.
the class DoPMD method doPMD.
public int doPMD(GargoylePMDConfiguration configuration, List<ReportListener> listeners) {
renerderWriter = RendererWriterFactory.get(WRITER_TYPE.StringWriter);
// Load the RuleSets
RuleSetFactory ruleSetFactory = RulesetsFactoryUtils.getRulesetFactory(configuration);
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 = RendererFactory.createRenderer(configuration.getReportFormat(), configuration.getReportProperties()); //configuration.createRenderer();//createDefaultRenderer();
List<Renderer> renderers = new LinkedList<>();
//configuration.createRenderer();
Renderer renderer = new DatabaseXmlRenderer();
renderer.setWriter(renerderWriter.getWriter());
renderer.start();
renderers.add(renderer);
Benchmarker.mark(Benchmark.Reporting, System.nanoTime() - reportStart, 0);
RuleContext ctx = new RuleContext();
final AtomicInteger violations = new AtomicInteger(0);
ctx.getReport().addListener(new ReportListener() {
@Override
public void ruleViolationAdded(RuleViolation ruleViolation) {
violations.incrementAndGet();
}
@Override
public void metricAdded(Metric metric) {
}
});
if (listeners != null && !listeners.isEmpty()) {
for (ReportListener l : listeners) ctx.getReport().addListener(l);
}
processFiles(configuration, ruleSetFactory, files, ctx, renderers);
reportStart = System.nanoTime();
// renderer.renderFileReport();
renderer.end();
renderer.flush();
return violations.get();
} catch (Exception e) {
LOGGER.error(ValueUtil.toString(e));
return 0;
} finally {
Benchmarker.mark(Benchmark.Reporting, System.nanoTime() - reportStart, 0);
try {
close();
} catch (IOException e) {
LOGGER.error(ValueUtil.toString(e));
}
}
}
use of net.sourceforge.pmd.RuleSets in project Gargoyle by callakrsos.
the class RulesetsFactoryUtils method getRuleSets.
/**
* Creates a new rulesets with the given string. The resulting rulesets will contain all referenced rulesets.
*
* @param rulesets
* the string with the rulesets to load
* @param factory
* the ruleset factory
* @return the rulesets
* @throws IllegalArgumentException
* if rulesets is empty (means, no rules have been found) or if a ruleset couldn't be found.
*/
public static RuleSets getRuleSets(String rulesets, RuleSetFactory factory) {
RuleSets ruleSets = null;
try {
factory.setWarnDeprecated(true);
ruleSets = factory.createRuleSets(rulesets);
factory.setWarnDeprecated(false);
printRuleNamesInDebug(ruleSets);
if (ruleSets.ruleCount() == 0) {
String msg = "No rules found. Maybe you mispelled a rule name? (" + rulesets + ")";
LOG.log(Level.SEVERE, msg);
throw new IllegalArgumentException(msg);
}
} catch (RuleSetNotFoundException rsnfe) {
LOG.log(Level.SEVERE, "Ruleset not found", rsnfe);
throw new IllegalArgumentException(rsnfe);
}
return ruleSets;
}
Aggregations