Search in sources :

Example 1 with RuleViolation

use of net.sourceforge.pmd.RuleViolation in project maven-plugins by apache.

the class PmdReport method removeExcludedViolations.

private void removeExcludedViolations(List<RuleViolation> violations) {
    getLog().debug("Removing excluded violations. Using " + excludeFromFile.countExclusions() + " configured exclusions.");
    int violationsBefore = violations.size();
    Iterator<RuleViolation> iterator = violations.iterator();
    while (iterator.hasNext()) {
        RuleViolation rv = iterator.next();
        if (excludeFromFile.isExcludedFromFailure(rv)) {
            iterator.remove();
        }
    }
    int numberOfExcludedViolations = violationsBefore - violations.size();
    getLog().debug("Excluded " + numberOfExcludedViolations + " violations.");
}
Also used : RuleViolation(net.sourceforge.pmd.RuleViolation)

Example 2 with RuleViolation

use of net.sourceforge.pmd.RuleViolation in project maven-plugins by apache.

the class PmdReportGenerator method processViolations.

// PMD might run the analysis multi-threaded, so the violations might be reported
// out of order. We sort them here by filename and line number before writing them to
// the report.
private void processViolations() throws IOException {
    fileCount = files.size();
    ArrayList<RuleViolation> violations2 = new ArrayList<>(violations);
    Collections.sort(violations2, new Comparator<RuleViolation>() {

        /** {@inheritDoc} */
        public int compare(RuleViolation o1, RuleViolation o2) {
            int filenames = o1.getFilename().compareTo(o2.getFilename());
            if (filenames == 0) {
                return o1.getBeginLine() - o2.getBeginLine();
            } else {
                return filenames;
            }
        }
    });
    boolean fileSectionStarted = false;
    String previousFilename = null;
    for (RuleViolation ruleViolation : violations2) {
        String currentFn = ruleViolation.getFilename();
        File canonicalFilename = new File(currentFn).getCanonicalFile();
        PmdFileInfo fileInfo = files.get(canonicalFilename);
        if (fileInfo == null) {
            log.warn("Couldn't determine PmdFileInfo for file " + currentFn + " (canonical: " + canonicalFilename + "). XRef links won't be available.");
        }
        if (!currentFn.equalsIgnoreCase(previousFilename) && fileSectionStarted) {
            endFileSection();
            fileSectionStarted = false;
        }
        if (!fileSectionStarted) {
            startFileSection(currentFn, fileInfo);
            fileSectionStarted = true;
        }
        processSingleRuleViolation(ruleViolation, fileInfo);
        previousFilename = currentFn;
    }
    if (fileSectionStarted) {
        endFileSection();
    }
}
Also used : ArrayList(java.util.ArrayList) RuleViolation(net.sourceforge.pmd.RuleViolation) File(java.io.File)

Example 3 with RuleViolation

use of net.sourceforge.pmd.RuleViolation 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 4 with RuleViolation

use of net.sourceforge.pmd.RuleViolation in project Gargoyle by callakrsos.

the class DatabaseXmlRenderer method renderFileViolations.

/**
	 * {@inheritDoc}
	 */
@Override
public void renderFileViolations(Iterator<RuleViolation> violations) throws IOException {
    Writer writer = getWriter();
    StringBuilder buf = new StringBuilder(500);
    String filename = null;
    // rule violations
    while (violations.hasNext()) {
        buf.setLength(0);
        RuleViolation rv = violations.next();
        if (!rv.getFilename().equals(filename)) {
            // New File
            if (filename != null) {
                // Not first file ?
                buf.append("</file>").append(PMD.EOL);
            }
            filename = rv.getFilename();
            buf.append("<file name=\"");
            StringUtil.appendXmlEscaped(buf, filename, useUTF8);
            buf.append("\">").append(PMD.EOL);
        }
        buf.append("<violation beginline=\"").append(rv.getBeginLine());
        buf.append("\" endline=\"").append(rv.getEndLine());
        buf.append("\" begincolumn=\"").append(rv.getBeginColumn());
        buf.append("\" endcolumn=\"").append(rv.getEndColumn());
        buf.append("\" rule=\"");
        StringUtil.appendXmlEscaped(buf, rv.getRule().getName(), useUTF8);
        buf.append("\" ruleset=\"");
        StringUtil.appendXmlEscaped(buf, rv.getRule().getRuleSetName(), useUTF8);
        buf.append('"');
        maybeAdd("package", rv.getPackageName(), buf);
        maybeAdd("class", rv.getClassName(), buf);
        maybeAdd("method", rv.getMethodName(), buf);
        maybeAdd("variable", rv.getVariableName(), buf);
        maybeAdd("externalInfoUrl", rv.getRule().getExternalInfoUrl(), buf);
        buf.append(" priority=\"");
        buf.append(rv.getRule().getPriority().getPriority());
        buf.append("\">").append(PMD.EOL);
        StringUtil.appendXmlEscaped(buf, rv.getDescription(), useUTF8);
        buf.append(PMD.EOL);
        buf.append("</violation>");
        buf.append(PMD.EOL);
        writer.write(buf.toString());
    }
    if (filename != null) {
        // Not first file ?
        writer.write("</file>");
        writer.write(PMD.EOL);
    }
}
Also used : RuleViolation(net.sourceforge.pmd.RuleViolation) Writer(java.io.Writer)

Aggregations

RuleViolation (net.sourceforge.pmd.RuleViolation)4 File (java.io.File)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)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 Language (net.sourceforge.pmd.lang.Language)1 Renderer (net.sourceforge.pmd.renderers.Renderer)1 Metric (net.sourceforge.pmd.stat.Metric)1 DataSource (net.sourceforge.pmd.util.datasource.DataSource)1 ReaderDataSource (net.sourceforge.pmd.util.datasource.ReaderDataSource)1