diff --git a/docs/pages/pmd/userdocs/tools/java-api.md b/docs/pages/pmd/userdocs/tools/java-api.md index 5347e7b56a..e3388c137d 100644 --- a/docs/pages/pmd/userdocs/tools/java-api.md +++ b/docs/pages/pmd/userdocs/tools/java-api.md @@ -2,6 +2,7 @@ title: PMD Java API tags: [userdocs, tools] permalink: pmd_userdocs_tools_java_api.html +last_updated: August 2023 (7.0.0) --- The easiest way to run PMD is to just use a build plugin in your favorite build tool @@ -33,3 +34,7 @@ This will transitively pull in the artifact `pmd-core` which contains the API. ## Running PMD programmatically The programmatic API for PMD is centered around {% jdoc core::PmdAnalysis %}, please see the javadocs for usage information. + +## Running CPD programmatically + +The programmatic API for CPD is centered around {% jdoc core::CpdAnalysis %}, please see the javadocs for usage information. diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/PmdAnalysis.java b/pmd-core/src/main/java/net/sourceforge/pmd/PmdAnalysis.java index 41b13af887..6a1b013776 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/PmdAnalysis.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/PmdAnalysis.java @@ -59,7 +59,7 @@ import net.sourceforge.pmd.util.log.MessageReporter; * *
Create and configure a {@link PMDConfiguration}, * then use {@link #create(PMDConfiguration)} to obtain an instance. * You can perform additional configuration on the instance, e.g. adding * files to process, or additional rulesets and renderers. Then, call @@ -70,7 +70,7 @@ import net.sourceforge.pmd.util.log.MessageReporter; *
{@code * PMDConfiguration config = new PMDConfiguration(); * config.setDefaultLanguageVersion(LanguageRegistry.findLanguageByTerseName("java").getVersion("11")); - * config.setInputPaths("src/main/java"); + * config.addInputPath(Path.of("src/main/java")); * config.prependClasspath("target/classes"); * config.setMinimumPriority(RulePriority.HIGH); * config.addRuleSet("rulesets/java/quickstart.xml"); @@ -92,7 +92,7 @@ import net.sourceforge.pmd.util.log.MessageReporter; * *Rendering reports
* - * If you just want to render a report to a file like with the CLI, you + *If you just want to render a report to a file like with the CLI, you * should use a {@link Renderer}. You can add a custom one with {@link PmdAnalysis#addRenderer(Renderer)}. * You can add one of the builtin renderers from its ID using {@link PMDConfiguration#setReportFormat(String)}. * @@ -116,7 +116,7 @@ import net.sourceforge.pmd.util.log.MessageReporter; * *
Specifying the Java classpath
* - * Java rules work better if you specify the path to the compiled classes + *Java rules work better if you specify the path to the compiled classes * of the analysed sources. See {@link PMDConfiguration#prependAuxClasspath(String)}. * *
Customizing message output
diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CpdAnalysis.java b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CpdAnalysis.java index c012312126..fc1e131825 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CpdAnalysis.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CpdAnalysis.java @@ -31,6 +31,40 @@ import net.sourceforge.pmd.lang.document.TextFile; import net.sourceforge.pmd.properties.PropertyDescriptor; import net.sourceforge.pmd.util.log.MessageReporter; +/** + * Main programmatic API of CPD. This is not a CLI entry point, see module + * {@code pmd-cli} for that. + * + *Usage overview
+ * + *Create and configure a {@link CPDConfiguration}, then use {@link #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 {@link #performAnalysis()} or {@link #performAnalysis(Consumer)} + * in order to get the report directly. + * + *
Simple example
+ * + *{@code + * 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(); + * } + * }+ */ public final class CpdAnalysis implements AutoCloseable { private static final Logger LOGGER = LoggerFactory.getLogger(CpdAnalysis.class);