Class PmdAnalysis

  • All Implemented Interfaces:
    AutoCloseable

    public final class PmdAnalysis
    extends Object
    implements AutoCloseable
    Main programmatic API of PMD. Create and configure a PMDConfiguration, then use create(PMDConfiguration) to obtain an instance. You can perform additional configuration on the instance, eg adding files to process, or additional rulesets and renderers. Then, call performAnalysis(). Example:
    
       PMDConfiguration config = new PMDConfiguration();
       config.setDefaultLanguageVersion(LanguageRegistry.findLanguageByTerseName("java").getVersion("11"));
       config.setInputPaths("src/main/java");
       config.prependClasspath("target/classes");
       config.setMinimumPriority(RulePriority.HIGH);
       config.addRuleSet("rulesets/java/quickstart.xml");
       config.setReportFormat("xml");
       config.setReportFile("target/pmd-report.xml");
    
       try (PmdAnalysis pmd = PmdAnalysis.create(config)) {
         // note: don't use `config` once a PmdAnalysis has been created.
         // optional: add more rulesets
         pmd.addRuleSet(pmd.newRuleSetLoader().loadFromResource("custom-ruleset.xml"));
         // optional: add more files
         pmd.files().addFile(Paths.get("src", "main", "more-java", "ExtraSource.java"));
         // optional: add more renderers
         pmd.addRenderer(renderer);
    
         pmd.performAnalysis();
       }
     
    • Method Detail

      • create

        public static PmdAnalysis create​(PMDConfiguration config)
        Constructs a new instance from a configuration.
        • The files paths (input files, filelist, exclude list, etc) are explored and the files to analyse are collected into the file collector (files()). More can be added programmatically using the file collector.
        • The rulesets given in the configuration are loaded (PMDConfiguration.getRuleSets())
        • A renderer corresponding to the parameters of the configuration is created and added (but not started).
      • files

        public FileCollector files()
        Returns the file collector for the analysed sources.
      • newRuleSetLoader

        public RuleSetLoader newRuleSetLoader()
        Returns a new ruleset loader, which can be used to create new rulesets (add them then with addRuleSet(RuleSet)).
        
         try (PmdAnalysis pmd = create(config)) {
             pmd.addRuleSet(pmd.newRuleSetLoader().loadFromResource("custom-ruleset.xml"));
         }
         
      • addRenderer

        public void addRenderer​(Renderer renderer)
        Add a new renderer. The given renderer must not already be started, it will be started by performAnalysis().
        Throws:
        NullPointerException - If the parameter is null
      • addRenderers

        public void addRenderers​(Collection<Renderer> renderers)
        Add several renderers at once.
        Throws:
        NullPointerException - If the parameter is null, or any of its items is null.
      • addRuleSets

        public void addRuleSets​(Collection<RuleSet> ruleSets)
        Add several rulesets at once.
        Throws:
        NullPointerException - If the parameter is null, or any of its items is null.
      • performAnalysis

        public void performAnalysis()
        Run PMD with the current state of this instance. This will start and finish the registered renderers. All files collected in the file collector are processed. This does not return a report, for compatibility with PMD 7. Note that this does not throw, errors are instead accumulated into a MessageReporter.
      • performAnalysisAndCollectReport

        public Report performAnalysisAndCollectReport()
        Run PMD with the current state of this instance. This will start and finish the registered renderers. All files collected in the file collector are processed. Returns the output report. Note that this does not throw, errors are instead accumulated into a MessageReporter.