Search in sources :

Example 1 with Metric

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));
        }
    }
}
Also used : ReportListener(net.sourceforge.pmd.ReportListener) RuleContext(net.sourceforge.pmd.RuleContext) RuleViolation(net.sourceforge.pmd.RuleViolation) IOException(java.io.IOException) LinkedList(java.util.LinkedList) IOException(java.io.IOException) DataSource(net.sourceforge.pmd.util.datasource.DataSource) ReaderDataSource(net.sourceforge.pmd.util.datasource.ReaderDataSource) RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) Language(net.sourceforge.pmd.lang.Language) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RuleSets(net.sourceforge.pmd.RuleSets) Renderer(net.sourceforge.pmd.renderers.Renderer) Metric(net.sourceforge.pmd.stat.Metric)

Example 2 with Metric

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);
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) DummyNode(net.sourceforge.pmd.lang.ast.DummyNode) Metric(net.sourceforge.pmd.stat.Metric) MockRule(net.sourceforge.pmd.lang.rule.MockRule) MockRule(net.sourceforge.pmd.lang.rule.MockRule) Test(org.junit.Test)

Example 3 with Metric

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());
        }
    }
}
Also used : ResourceLoader(net.sourceforge.pmd.util.ResourceLoader) URISyntaxException(java.net.URISyntaxException) SQLException(java.sql.SQLException) IOException(java.io.IOException) DataSource(net.sourceforge.pmd.util.datasource.DataSource) ReaderDataSource(net.sourceforge.pmd.util.datasource.ReaderDataSource) ClasspathClassLoader(net.sourceforge.pmd.util.ClasspathClassLoader) Language(net.sourceforge.pmd.lang.Language) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Renderer(net.sourceforge.pmd.renderers.Renderer) Metric(net.sourceforge.pmd.stat.Metric)

Example 4 with Metric

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();
}
Also used : DataPoint(net.sourceforge.pmd.stat.DataPoint) Metric(net.sourceforge.pmd.stat.Metric) DataPoint(net.sourceforge.pmd.stat.DataPoint)

Example 5 with Metric

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);
}
Also used : Metric(net.sourceforge.pmd.stat.Metric) Test(org.junit.Test)

Aggregations

Metric (net.sourceforge.pmd.stat.Metric)5 IOException (java.io.IOException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Language (net.sourceforge.pmd.lang.Language)2 Renderer (net.sourceforge.pmd.renderers.Renderer)2 DataSource (net.sourceforge.pmd.util.datasource.DataSource)2 ReaderDataSource (net.sourceforge.pmd.util.datasource.ReaderDataSource)2 Test (org.junit.Test)2 URISyntaxException (java.net.URISyntaxException)1 SQLException (java.sql.SQLException)1 LinkedList (java.util.LinkedList)1 ReportListener (net.sourceforge.pmd.ReportListener)1 RuleContext (net.sourceforge.pmd.RuleContext)1 RuleSetFactory (net.sourceforge.pmd.RuleSetFactory)1 RuleSets (net.sourceforge.pmd.RuleSets)1 RuleViolation (net.sourceforge.pmd.RuleViolation)1 DummyNode (net.sourceforge.pmd.lang.ast.DummyNode)1 Node (net.sourceforge.pmd.lang.ast.Node)1 MockRule (net.sourceforge.pmd.lang.rule.MockRule)1 DataPoint (net.sourceforge.pmd.stat.DataPoint)1