use of net.sourceforge.pmd.stat.Metric 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.stat.Metric in project pmd by pmd.
the class ReportTest method testListener.
@Test
public void testListener() {
Report rpt = new Report();
rpt.addListener(this);
violationSemaphore = false;
RuleContext ctx = new RuleContext();
ctx.setSourceCodeFilename("file");
Node s = getNode(5, 5);
Rule rule1 = new MockRule("name", "desc", "msg", "rulesetname");
rpt.addRuleViolation(new ParametricRuleViolation<>(rule1, ctx, s, rule1.getMessage()));
assertTrue(violationSemaphore);
metricSemaphore = false;
rpt.addMetric(new Metric("test", 0, 0.0, 0.0, 0.0, 0.0, 0.0));
assertTrue("no metric", metricSemaphore);
}
use of net.sourceforge.pmd.stat.Metric 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.stat.Metric in project pmd by pmd.
the class StatisticalRuleHelper method apply.
public void apply(RuleContext ctx) {
double deviation;
double minimum = 0.0;
if (rule.getProperty(SIGMA_DESCRIPTOR) != null) {
// TODO - need to come
// up with a good
// default value
deviation = getStdDev();
double sigma = rule.getProperty(SIGMA_DESCRIPTOR);
minimum = getMean() + (sigma * deviation);
}
if (rule.getProperty(MINIMUM_DESCRIPTOR) != null) {
// TODO - need to
// come up with a
// good default
// value
double mMin = rule.getProperty(MINIMUM_DESCRIPTOR);
if (mMin > minimum) {
minimum = mMin;
}
}
SortedSet<DataPoint> newPoints = applyMinimumValue(dataPoints, minimum);
if (rule.getProperty(TOP_SCORE_DESCRIPTOR) != null) {
// TODO - need to
// come up with a
// good default
// value
int topScore = rule.getProperty(TOP_SCORE_DESCRIPTOR);
if (newPoints.size() >= topScore) {
newPoints = applyTopScore(newPoints, topScore);
}
}
makeViolations(ctx, newPoints);
double low = 0.0d;
double high = 0.0d;
if (!dataPoints.isEmpty()) {
low = dataPoints.first().getScore();
high = dataPoints.last().getScore();
}
ctx.getReport().addMetric(new Metric(rule.getName(), count, total, low, high, getMean(), getStdDev()));
dataPoints.clear();
}
use of net.sourceforge.pmd.stat.Metric in project pmd by pmd.
the class ReportTest method testMetric1.
@Test
public void testMetric1() {
Report r = new Report();
assertFalse("Default report shouldn't contain metrics", r.hasMetrics());
r.addMetric(new Metric("m1", 0, 0.0, 1.0, 2.0, 3.0, 4.0));
assertTrue("Expected metrics weren't there", r.hasMetrics());
Iterator<Metric> ms = r.metrics();
assertTrue("Should have some metrics in there now", ms.hasNext());
Object o = ms.next();
assertTrue("Expected Metric, got " + o.getClass(), o instanceof Metric);
Metric m = (Metric) o;
assertEquals("metric name mismatch", "m1", m.getMetricName());
assertEquals("wrong low value", 1.0, m.getLowValue(), 0.05);
assertEquals("wrong high value", 2.0, m.getHighValue(), 0.05);
assertEquals("wrong avg value", 3.0, m.getAverage(), 0.05);
assertEquals("wrong std dev value", 4.0, m.getStandardDeviation(), 0.05);
}
Aggregations