Search in sources :

Example 1 with ClasspathClassLoader

use of net.sourceforge.pmd.util.ClasspathClassLoader 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 2 with ClasspathClassLoader

use of net.sourceforge.pmd.util.ClasspathClassLoader in project Gargoyle by callakrsos.

the class DoPMD method processFiles.

/**
	 * Run PMD on a list of files using multiple threads - if more than one is available
	 *
	 * @param configuration
	 *            Configuration
	 * @param ruleSetFactory
	 *            RuleSetFactory
	 * @param files
	 *            List<DataSource>
	 * @param ctx
	 *            RuleContext
	 * @param renderers
	 *            List<Renderer>
	 */
public void processFiles(final GargoylePMDConfiguration configuration, final RuleSetFactory ruleSetFactory, final List<DataSource> files, final RuleContext ctx, final List<Renderer> renderers) {
    sortFiles(configuration, files);
    /*
		 * Check if multithreaded support is available. ExecutorService can also
		 * be disabled if threadCount is not positive, e.g. using the
		 * "-threads 0" command line option.
		 */
    //			if (SystemUtils.MT_SUPPORTED && configuration.getThreads() > 0) {
    //				MultiThreadProcessor multiThreadProcessor = new MultiThreadProcessor(configuration);
    //				multiThreadProcessor.processFiles(ruleSetFactory, files, ctx, renderers);
    //			} else {
    pmdGargoyleThreadProcessor = new PMDGargoyleThreadProcessor(configuration);
    pmdGargoyleThreadProcessor.processFiles(ruleSetFactory, files, ctx, renderers);
    if (configuration.getClassLoader() instanceof ClasspathClassLoader) {
        IOUtil.tryCloseClassLoader(configuration.getClassLoader());
    }
}
Also used : PMDGargoyleThreadProcessor(com.kyj.fx.voeditor.visual.component.pmd.PMDGargoyleThreadProcessor) ClasspathClassLoader(net.sourceforge.pmd.util.ClasspathClassLoader)

Example 3 with ClasspathClassLoader

use of net.sourceforge.pmd.util.ClasspathClassLoader in project pmd by pmd.

the class PMDTaskImpl method execute.

public void execute() throws BuildException {
    final Handler antLogHandler = new AntLogHandler(project);
    final ScopedLogHandlersManager logManager = new ScopedLogHandlersManager(Level.FINEST, antLogHandler);
    try {
        doTask();
    } finally {
        logManager.close();
        // exceptions
        if (configuration.getClassLoader() instanceof ClasspathClassLoader) {
            IOUtil.tryCloseClassLoader(configuration.getClassLoader());
        }
    }
}
Also used : ClasspathClassLoader(net.sourceforge.pmd.util.ClasspathClassLoader) AntLogHandler(net.sourceforge.pmd.util.log.AntLogHandler) Handler(java.util.logging.Handler) AntLogHandler(net.sourceforge.pmd.util.log.AntLogHandler) ScopedLogHandlersManager(net.sourceforge.pmd.util.log.ScopedLogHandlersManager)

Aggregations

ClasspathClassLoader (net.sourceforge.pmd.util.ClasspathClassLoader)3 PMDGargoyleThreadProcessor (com.kyj.fx.voeditor.visual.component.pmd.PMDGargoyleThreadProcessor)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 SQLException (java.sql.SQLException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Handler (java.util.logging.Handler)1 Language (net.sourceforge.pmd.lang.Language)1 Renderer (net.sourceforge.pmd.renderers.Renderer)1 Metric (net.sourceforge.pmd.stat.Metric)1 ResourceLoader (net.sourceforge.pmd.util.ResourceLoader)1 DataSource (net.sourceforge.pmd.util.datasource.DataSource)1 ReaderDataSource (net.sourceforge.pmd.util.datasource.ReaderDataSource)1 AntLogHandler (net.sourceforge.pmd.util.log.AntLogHandler)1 ScopedLogHandlersManager (net.sourceforge.pmd.util.log.ScopedLogHandlersManager)1