[core] Add minimal javadoc for CpdAnalysis

This commit is contained in:
Andreas Dangel
2023-08-24 11:39:59 +02:00
parent 681c52836c
commit 6298d87e71
3 changed files with 43 additions and 4 deletions

View File

@ -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.

View File

@ -59,7 +59,7 @@ import net.sourceforge.pmd.util.log.MessageReporter;
*
* <h3>Usage overview</h3>
*
* Create and configure a {@link PMDConfiguration},
* <p>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;
* <pre>{@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;
*
* <h3>Rendering reports</h3>
*
* If you just want to render a report to a file like with the CLI, you
* <p>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;
*
* <h3>Specifying the Java classpath</h3>
*
* Java rules work better if you specify the path to the compiled classes
* <p>Java rules work better if you specify the path to the compiled classes
* of the analysed sources. See {@link PMDConfiguration#prependAuxClasspath(String)}.
*
* <h3>Customizing message output</h3>

View File

@ -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.
*
* <h3>Usage overview</h3>
*
* <p>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.
*
* <h3>Simple example</h3>
*
* <pre>{@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();
* }
* }</pre>
*/
public final class CpdAnalysis implements AutoCloseable {
private static final Logger LOGGER = LoggerFactory.getLogger(CpdAnalysis.class);