Java Code Examples for net.sourceforge.pmd.RulesetsFactoryUtils#getRulesetFactory()

The following examples show how to use net.sourceforge.pmd.RulesetsFactoryUtils#getRulesetFactory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PmdProcessor.java    From sputnik with Apache License 2.0 5 votes vote down vote up
/**
 * PMD has terrible design of process configuration. You must use report file with it. I paste this method here and
 * improve it.
 *
 * @throws IllegalArgumentException
 *             if the configuration is not correct
 */
private void doPMD(@NotNull PMDConfiguration configuration) throws IllegalArgumentException {
    // Load the RuleSets
    RuleSetFactory ruleSetFactory = RulesetsFactoryUtils.getRulesetFactory(configuration, new ResourceLoader());

    RuleSets ruleSets = RulesetsFactoryUtils.getRuleSets(configuration.getRuleSets(), ruleSetFactory);
    // this is just double check - we don't get null here
    // instead IllegalArgumentException/RuntimeException is thrown if configuration is wrong
    if (ruleSets == null) {
        return;
    }

    Set<Language> languages = getApplicableLanguages(configuration, ruleSets);
    // this throws RuntimeException when modified file does not exist in workspace
    List<DataSource> files = PMD.getApplicableFiles(configuration, languages);

    long reportStart = System.nanoTime();
    try {
        renderer = configuration.createRenderer();
        List<Renderer> renderers = new LinkedList<>();
        renderers.add(renderer);
        renderer.start();

        Benchmarker.mark(Benchmark.Reporting, System.nanoTime() - reportStart, 0);

        RuleContext ctx = new RuleContext();

        PMD.processFiles(configuration, ruleSetFactory, files, ctx, renderers);

        reportStart = System.nanoTime();
        renderer.end();
    } catch (IOException e) {
        log.error("PMD analysis error", e);
    } finally {
        Benchmarker.mark(Benchmark.Reporting, System.nanoTime() - reportStart, 0);
    }
}
 
Example 2
Source File: Main.java    From diff-check with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * 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);
    RuleSets ruleSets = RulesetsFactoryUtils.getRuleSetsWithBenchmark(configuration.getRuleSets(), ruleSetFactory);
    if (ruleSets == null) {
        return 0;
    }

    Set<Language> languages = getApplicableLanguages(configuration, ruleSets);
    List<DataSource> files = Collections.emptyList();
    if (StringUtils.isNotBlank(configuration.getGitDir())) {
        files = getApplicableGitDiffFiles(configuration, languages);
    } else {
        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);
        DiffLineFilter filter = new DiffLineFilter(Main.DIFF_ENTRY_LIST);
        ctx.getReport().addListener(new ReportListener() {
            @Override
            public void ruleViolationAdded(RuleViolation ruleViolation) {
                if (!filter.accept(ruleViolation)) {
                    return;
                }
                violations.incrementAndGet();
            }
            @Override
            public void metricAdded(Metric metric) {
            }
        });

        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);
    }
}