Class CpdAnalysis

java.lang.Object
net.sourceforge.pmd.cpd.CpdAnalysis
All Implemented Interfaces:
AutoCloseable

public final class CpdAnalysis extends Object implements AutoCloseable
Main programmatic API of CPD. This is not a CLI entry point, see module pmd-cli for that.

Usage overview

Create and configure a CPDConfiguration, then use create(CPDConfiguration) to obtain an instance. You can perform additional configuration on the instance, e.g. adding files to process or add a listener. Then call performAnalysis() or performAnalysis(Consumer) in order to get the report directly.

Simple example


   CPDConfiguration config = new CPDConfiguration();
   config.setMinimumTileSize(100);
   config.setOnlyRecognizeLanguage(config.getLanguageRegistry().getLanguageById("java"));
   config.setSourceEncoding(StandardCharsets.UTF_8);
   config.addInputPath(Path.of("src/main/java")

   config.setIgnoreAnnotations(true);
   config.setIgnoreLiterals(false);

   config.setRendererName("text");

   try (CpdAnalysis cpd = CpdAnalysis.create(config)) {
      // note: don't use `config` once a CpdAnalysis has been created.
      // optional: add more files
      cpd.files().addFile(Paths.get("src", "main", "more-java", "ExtraSource.java"));

      cpd.performAnalysis();
   }
 
  • Method Details

    • create

      public static CpdAnalysis create(CPDConfiguration config)
      Create a new instance from the given configuration. The configuration should not be modified after this.
      Parameters:
      config - Configuration
      Returns:
      A new analysis instance
    • files

      public FileCollector files()
    • setCpdListener

      public void setCpdListener(@Nullable CPDListener cpdListener)
    • performAnalysis

      public void performAnalysis()
    • performAnalysis

      public void performAnalysis(Consumer<CPDReport> consumer)
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException