smart-duplicate-detector 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +156 -0
- package/index.js +26 -0
- package/package.json +16 -0
- package/target/smart-duplicate-detector.jar +0 -0
package/README.md
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# Smart Duplicate Detector
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
A Java tool that scans a codebase and flags methods with duplicate or highly similar logic before they become technical debt.
|
|
8
|
+
|
|
9
|
+
Developers often reuse or regenerate business logic without realizing an equivalent method already exists elsewhere in the codebase. Over time these duplicates drift apart, and bugs get fixed in one copy but not the other. Smart Duplicate Detector scans a project, compares every method, and reports matches above a similarity threshold—with file, line number, and score—so duplication is caught before it's merged.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## The "Why": Technical Approach
|
|
14
|
+
|
|
15
|
+
Traditional duplicate detectors often rely on raw string matching or tokenization. This leads to a massive amount of "false positive" noise—flagging standard boilerplate, imports, or basic getters and setters as duplicated code.
|
|
16
|
+
|
|
17
|
+
**Smart Duplicate Detector** solves this using a two-step semantic approach:
|
|
18
|
+
|
|
19
|
+
1. **AST Method Parsing:** By leveraging JavaParser, the engine parses the codebase into an Abstract Syntax Tree (AST) and isolates functional methods. It completely ignores class-level boilerplate, fields, and imports.
|
|
20
|
+
|
|
21
|
+
2. **Weighted Levenshtein Algorithm:** Instead of basic string comparison, SDD compares the logic and structure of the methods using a custom Weighted Levenshtein distance algorithm. This accurately calculates similarity percentages, ensuring that only genuine logic clones are flagged while ignoring minor whitespace or variable renaming differences.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Features
|
|
26
|
+
|
|
27
|
+
- **Global CLI Tool:** Wrapped in NPM for a frictionless 1-step installation and a global `sdd` command.
|
|
28
|
+
- **Modern Desktop App:** Sleek, dark-themed Java Swing GUI powered by FlatLaf with real-time background scanning (`SwingWorker`).
|
|
29
|
+
- **High Precision:** Structural extraction of every method into a comparable AST model.
|
|
30
|
+
- **Smart Scoring:** Highly accurate similarity scoring via `LevenshteinSimilarity`.
|
|
31
|
+
- Custom exception handling for invalid or empty project paths.
|
|
32
|
+
- *(Planned)* Scan a public GitHub repository directly by URL instead of a local path.
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Tech Stack
|
|
37
|
+
|
|
38
|
+
| Layer | Technology |
|
|
39
|
+
| :--- | :--- |
|
|
40
|
+
| **Language** | Java 17 |
|
|
41
|
+
| **Build & Distribution** | Maven, Node.js / NPM (CLI Wrapper) |
|
|
42
|
+
| **Parsing** | JavaParser |
|
|
43
|
+
| **Desktop GUI** | Java Swing + FlatLaf (Modern Dark Theme) |
|
|
44
|
+
| **Backend / API** | Javalin (REST, JSON via Jackson) |
|
|
45
|
+
| **Testing** | JUnit 5 |
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## Architecture
|
|
50
|
+
|
|
51
|
+
```text
|
|
52
|
+
smart-duplicate-detector/
|
|
53
|
+
├── package.json & index.js # NPM Bridge for global 'sdd' command
|
|
54
|
+
├── model/ # MethodModel, DuplicatePair
|
|
55
|
+
├── core/ # ProjectScanner, AstMethodParser, SimilarityAlgorithm...
|
|
56
|
+
├── gui/ # MainFrame (Swing Desktop App)
|
|
57
|
+
├── report/ # AbstractReport, ConsoleReport
|
|
58
|
+
├── exceptions/ # InvalidProjectPathException, NoJavaFilesFoundException
|
|
59
|
+
├── api/ # ApiServer (REST endpoints)
|
|
60
|
+
├── cli/ # CliRunner
|
|
61
|
+
└── resources/ # (web frontend / static files)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## 🚀 Getting Started (End Users)
|
|
67
|
+
|
|
68
|
+
The easiest way to use Smart Duplicate Detector is via our NPM package, which automatically links the `sdd` command to your system.
|
|
69
|
+
|
|
70
|
+
### Prerequisites
|
|
71
|
+
|
|
72
|
+
- Java 17+
|
|
73
|
+
- Node.js & NPM
|
|
74
|
+
|
|
75
|
+
### Install Globally
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
npm install -g smart-duplicate-detector
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Launch the UI
|
|
82
|
+
|
|
83
|
+
To launch the modern FlatLaf desktop dashboard, open your terminal anywhere and type:
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
sdd
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Run via CLI (Headless)
|
|
90
|
+
|
|
91
|
+
To run a silent scan in your terminal without opening the GUI:
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
sdd --path ./path/to/project --threshold 0.80
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## 🛠️ Developer Setup (Build from Source)
|
|
100
|
+
|
|
101
|
+
If you want to modify the Java engine or build the Fat-JAR yourself:
|
|
102
|
+
|
|
103
|
+
### 1. Clone the Repository
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
git clone https://github.com/fetehadin/smart-duplicate-detector.git
|
|
107
|
+
cd smart-duplicate-detector
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### 2. Build the Engine
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
mvn clean package
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### 3. Run Locally
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
java -jar target/smart-duplicate-detector.jar
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
*Alternatively, run `npm link` in the root folder to test the global `sdd` command locally!*
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
## Usage Example (CLI & GUI Output)
|
|
127
|
+
|
|
128
|
+
```text
|
|
129
|
+
Scanning project files...
|
|
130
|
+
Found 27 valid methods.
|
|
131
|
+
Running Levenshtein comparison engine...
|
|
132
|
+
|
|
133
|
+
DUPLICATE FOUND (92% similar)
|
|
134
|
+
→ EngineTest.java:14 -> testIdenticalSequenceLevenshteinScore
|
|
135
|
+
→ EngineTest.java:29 -> testDifferentSequenceLevenshteinScore
|
|
136
|
+
|
|
137
|
+
DUPLICATE FOUND (100% similar)
|
|
138
|
+
→ TestScenarios.java:6 -> calculateTaxUSA
|
|
139
|
+
→ TestScenarios.java:14 -> calculateTaxUK
|
|
140
|
+
|
|
141
|
+
Scan complete. 2 duplicate pairs found.
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
---
|
|
145
|
+
|
|
146
|
+
## Known Limitations
|
|
147
|
+
|
|
148
|
+
- Comparison is all-pairs (`O(n²)`): Fine for a single project, but not intended for massive, multi-gigabyte monorepos.
|
|
149
|
+
- Only structural/token similarity is considered: No control-flow or semantic analysis yet.
|
|
150
|
+
- GitHub-repo scanning (clone + scan by URL) is planned for a future release.
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
## License
|
|
155
|
+
|
|
156
|
+
MIT — see `LICENSE`.
|
package/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const { spawnSync } = require('child_process');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
|
|
6
|
+
// 1. Find the exact location of the JAR file relative to this script
|
|
7
|
+
const jarPath = path.join(__dirname, 'target', 'smart-duplicate-detector.jar');
|
|
8
|
+
|
|
9
|
+
// 2. Capture any arguments the user typed (e.g., --path . --threshold 0.8)
|
|
10
|
+
const args = process.argv.slice(2);
|
|
11
|
+
|
|
12
|
+
// 3. Check if Java is installed
|
|
13
|
+
const javaCheck = spawnSync('java', ['-version']);
|
|
14
|
+
if (javaCheck.error) {
|
|
15
|
+
console.error('❌ Error: Java is not installed or not in your PATH.');
|
|
16
|
+
console.error('Please install Java 17 or higher to run Smart Duplicate Detector.');
|
|
17
|
+
process.exit(1);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// 4. Execute the JAR file
|
|
21
|
+
const result = spawnSync('java', ['-jar', jarPath, ...args], {
|
|
22
|
+
stdio: 'inherit' // This ensures the terminal output AND the GUI window work perfectly
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// 5. Exit with the same status code the Java app returned
|
|
26
|
+
process.exit(result.status);
|
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "smart-duplicate-detector",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A high-precision static analysis engine to find structural code clones in Java.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"sdd": "index.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"index.js",
|
|
11
|
+
"target/smart-duplicate-detector.jar"
|
|
12
|
+
],
|
|
13
|
+
"keywords": ["java", "duplicate-code", "static-analysis", "ast", "cli"],
|
|
14
|
+
"author": "Fetehadin Negash",
|
|
15
|
+
"license": "MIT"
|
|
16
|
+
}
|
|
Binary file
|