Skip to main content
Moderne OnlyThis recipe is proprietary to Moderne and runs on the Moderne platform or CLI — it isn’t part of the open-source catalog. Available with a Moderne subscription.Contact Sales

Find duplicate code

Recipe IDio.moderne.prethink.quality.FindDuplicateCode
Artifactio.moderne.recipe:rewrite-prethink

Detect duplicate and likely-duplicate code across the codebase by AST-subtree fingerprinting and MinHash/LSH method alignment (zero AI). Reports Type-1 (exact), Type-2 (renamed), and Type-3 (gapped: inserted/removed/edited statements) clone groups ranked by the volume of code that would collapse if the duplication were refactored away.

Single recipeOpenRewriteModerne Proprietary License
Try in PlatformTry this recipe in the Moderne platform. Not a user yet? You’ll get a no-setup demo environment, with nothing to install or configure.

Options

12 parameters
12 optional
ParameterExampleTypeRequired
detectSimilarMethodstrueBooleanOptional
Whether to also detect gapped (Type-3) clones between whole methods that share no long run of identical statements, by aligning MinHash/LSH-similar method pairs. Catches copy-edited methods the statement-run detector misses because it needs a run of identical statements to anchor. Defaults to true.
detectStatementSequencestrueBooleanOptional
Whether to detect duplicated runs of consecutive statements (in addition to whole-subtree clones). Defaults to true.
detectType2trueBooleanOptional
Whether to detect renamed (Type-2) clones in addition to exact (Type-1) clones. Defaults to true.
gapBudget1IntegerOptional
Maximum number of statement edits (insertions, deletions, or substitutions) tolerated inside a statement-sequence clone (Type-3). 0 detects only exact/renamed runs; 1 (the default) also catches copy-paste-then-edit/insert/remove-one-statement. Higher values catch more divergent copies.
includeTestSourcesfalseBooleanOptional
Whether to include test sources (paths under src/test). Defaults to false.
maxOccurrencesPerGroup200IntegerOptional
Cap on the number of individual occurrences reported per clone group. The true count is always reported; only the enumerated locations/rows are capped. Defaults to 200.
minNodes75IntegerOptional
Minimum number of AST nodes a subtree must contain to be considered for exact (Type-1) duplication. Larger values reduce noise. Defaults to 75.
minNodesType2110IntegerOptional
Minimum number of AST nodes a subtree must contain to be considered for renamed (Type-2) duplication. Should be at least the Type-1 minimum because anonymization is noisier. Defaults to 110.
minOccurrences2IntegerOptional
Minimum number of occurrences for a fragment to be reported as a clone group. Defaults to 2.
minStatements5IntegerOptional
Minimum number of consecutive statements that must match to seed a statement-sequence clone. Defaults to 5.
snippetMaxLines40IntegerOptional
Maximum number of lines of the representative fragment to include in the snippet. Defaults to 40.
suppressBoilerplatetrueBooleanOptional
Whether to structurally suppress low-value boilerplate: getters/setters, equals/hashCode/toString, runs or blocks composed entirely of trivial data statements (constant declarations, map/collection population), and subtrees dominated by a large string-literal concatenation (embedded doc/JSON/prompt blobs). Suppressed fragments are still descended into, so genuine clones nested inside them are still found. Defaults to true.

Usage

Run this recipe

This recipe has no required configuration options. Users of Moderne can run it via the Moderne CLI.

You will need to have configured the Moderne CLI on your machine before you can run the following command.

shell
mod run . --recipe FindDuplicateCode

If the recipe is not available locally, then you can install it using:

mod config recipes jar install io.moderne.recipe:rewrite-prethink:0.9.0

Data tables

Duplicate code
io.moderne.prethink.table.DuplicateCode

Groups of duplicated code detected across the codebase by AST-subtree fingerprinting. One row per clone group; sort by redundant volume to surface the highest-value refactoring targets.

ColumnDescription
Clone group idStable identifier for the clone group (hex of the anonymized structural fingerprint). Join key to the duplicate code occurrences data table.
Clone typeTYPE_1 (identical modulo whitespace and comments) or TYPE_2 (identical structure with renamed identifiers and literals).
Duplicate countTotal number of occurrences of this fragment across the codebase.
Source pathPath of the representative (lexicographically-first) occurrence of the clone group.
SnippetThe duplicated code, printed from the representative occurrence and truncated for large fragments.
LinesLine count of the representative fragment.
Node massNumber of AST nodes in the representative fragment; the internal precision gate.
Redundant volumeLines multiplied by (duplicate count minus one): the number of lines that would collapse if the duplication were refactored to a single copy. Sort by this to surface the highest-value targets.
Duplicate locationsNewline-separated list of every occurrence's source path with enclosing type and member. Capped for very large groups, with a true-count line.
Duplicate code occurrences
io.moderne.prethink.table.DuplicateCodeOccurrences

One row per individual occurrence of a duplicated fragment, with the machine coordinates a remediation recipe needs to locate and converge each clone. Join to the duplicate code data table on clone group id.

ColumnDescription
Clone group idIdentifier of the clone group this occurrence belongs to. Join key to the duplicate code data table.
Clone typeTYPE_1 or TYPE_2.
Occurrence roleREPRESENTATIVE (the canonical copy reported in the duplicate code table) or DUPLICATE.
Source pathPath of the source file containing this occurrence.
Enclosing typeFully qualified name of the class enclosing this occurrence, if any.
Enclosing memberName of the method enclosing this occurrence, if any.
Node idThe AST node's stable identifier (UUID). The primary machine coordinate a remediation recipe uses to locate this occurrence within the LST.
Start lineBest-effort absolute start line of the occurrence; -1 when not available.
End lineBest-effort absolute end line of the occurrence; -1 when not available.
Line countNumber of lines in the occurrence fragment.
Node massNumber of AST nodes in the occurrence fragment.
Strict fingerprintHex of the 128-bit exact (Type-1) structural fingerprint.
Anon fingerprintHex of the 128-bit anonymized (Type-2) structural fingerprint.
Source files that had results
org.openrewrite.table.SourcesFileResults

Source files that were modified by the recipe run.

ColumnDescription
Source path before the runThe source path of the file before the run. null when a source file was created during the run.
Source path after the runA recipe may modify the source path. This is the path after the run. null when a source file was deleted during the run.
Parent of the recipe that made changesIn a hierarchical recipe, the parent of the recipe that made a change. Empty if this is the root of a hierarchy or if the recipe is not hierarchical at all.
Recipe that made changesThe specific recipe that made a change.
Estimated time savingAn estimated effort that a developer to fix manually instead of using this recipe, in unit of seconds.
CycleThe recipe cycle in which the change was made.
Source files that had search results
org.openrewrite.table.SearchResults

Search results that were found during the recipe run.

ColumnDescription
Source path of search result before the runThe source path of the file with the search result markers present.
Source path of search result after run the runA recipe may modify the source path. This is the path after the run. null when a source file was deleted during the run.
ResultThe trimmed printed tree of the LST element that the marker is attached to.
DescriptionThe content of the description of the marker.
Recipe that added the search markerThe specific recipe that added the Search marker.
Source files that errored on a recipe
org.openrewrite.table.SourcesFileErrors

The details of all errors produced by a recipe run.

ColumnDescription
Source pathThe file that failed to parse.
Recipe that made changesThe specific recipe that made a change.
Stack traceThe stack trace of the failure.
Recipe performance
org.openrewrite.table.RecipeRunStats

Statistics used in analyzing the performance of recipes.

ColumnDescription
The recipeThe recipe whose stats are being measured both individually and cumulatively.
Source file countThe number of source files the recipe ran over.
Source file changed countThe number of source files which were changed in the recipe run. Includes files created, deleted, and edited.
Cumulative scanning time (ns)The total time spent across the scanning phase of this recipe.
Max scanning time (ns)The max time scanning any one source file.
Cumulative edit time (ns)The total time spent across the editing phase of this recipe.
Max edit time (ns)The max time editing any one source file.