Duplicated Java code is a semantic problem, not a textual one, because the copies usually differ in variable names. Atlas finds them with codebase_search, which searches by meaning rather than by literal string, then creates the shared helper with its write tool and swaps each copy for a call using apply_patch, one file per patch. After every swap you run JUnit 5 via mvn test through Atlas's bash tool, so a Maven project never sits in a half-collapsed state. Spotless keeps the new helper class formatted to the project's rules.
How do I find duplicated logic in a Java codebase when grep cannot see it?
Grep cannot find duplicated Java logic because the copies differ in variable names, parameter order, and formatting. Atlas uses codebase_search instead, which queries a semantic index built from AST declarations using tree-sitter, so a copy-pasted validation method in OrderService.java surfaces alongside its twin in InvoiceService.java, 2 files that share no literal tokens.
The first Atlas step is to ask codebase_search for the behavior, not the exact code. Describe what the duplicated Java logic does, for example normalizing a currency amount before persistence, and the semantic index returns ranked declarations even when your search words appear nowhere in the source. Atlas searches code with hybrid semantic and keyword retrieval fused by reciprocal rank fusion, so a literal token like `BigDecimal.setScale` still pulls exact hits while the semantic side pulls the paraphrased copies. Because Atlas indexes code by AST declarations using tree-sitter rather than blind line windows, a hit is a whole Java method or class, not a truncated window across a brace boundary. That is what makes the result reviewable: you get `src/main/java/com/example/order/OrderService.java` with the method that owns the logic, not line 214 out of context.
How do I confirm two Java methods are really duplicates before merging them?
Atlas reads each hit in full before collapsing anything, because 2 Java methods that look alike can differ in null handling, checked exceptions, or rounding mode. The documented step is explicit: read each hit and confirm the copies are genuinely equivalent, and only then create the shared helper.
Confirming equivalence in Java is where a careless refactor breaks production. Atlas opens each candidate with its read tool and puts the full method bodies side by side so you can compare the parts that matter: the exception signature, whether one copy swallows a `NumberFormatException` and the other rethrows it, whether one applies `RoundingMode.HALF_UP` and the other does not, whether generics differ in bounds. A near-duplicate is not a duplicate. If the copies diverge in behavior, the correct move is either to parameterize the difference in the helper or to leave them alone. Atlas can also fan this work out: Atlas fans out work to subagents that can run in the foreground or in parallel background sessions, which is useful when codebase_search returns eight candidate Java classes across several Maven modules and you want each one read before you decide.
How do I swap every duplicate for a call to the new Java helper safely?
Atlas replaces each duplicated Java method with a call to the helper using apply_patch, one file per patch, so each swap is independently reviewable and revertible. A patch to OrderService.java and a patch to InvoiceService.java are 2 separate approvals, not one large uncontrolled rewrite.
The one-file-per-patch rule is the safety property. Atlas's apply_patch is context-anchored: it seeks the surrounding lines it expects to find, which means a Java file that has drifted since Atlas read it causes the patch to fail rather than land in the wrong place. Atlas computes a unified diff for every file edit and surfaces it for approval before writing, and Atlas snapshots file changes as git patches so edits can be diffed and rolled back. After each swap, run the suite with bash: JUnit 5 via mvn test tells you immediately whether that one class still behaves. Running the full `mvn test` once at the end instead would tell you only that something broke, not which of the five swaps did it. Finish by grepping for any surviving copy of the old logic, since a duplicate that codebase_search missed is still a duplicate.
How does Atlas keep a Java refactor reviewable and revertible?
Atlas keeps a Java helper extraction reviewable through three mechanisms in 2026: every tool call is permission-gated against allow, ask, and deny rules, every file edit arrives as a unified diff for approval before writing, and every change is snapshotted as a git patch so it can be rolled back.
Extracting a shared helper touches a new class plus every caller, which is exactly the shape of change that goes wrong quietly. Atlas's guardrails are per-file and per-tool rather than per-session. The write that creates `AmountUtils.java` shows its full diff in the permission prompt. Each apply_patch against a caller is its own diff and its own approval. Atlas reads git branches, status, and diffs, and can stage and create commits on your behalf, so the finished refactor can be staged as a commit only once `mvn test` is green. If a swap turns out to be wrong, Atlas snapshots file changes as git patches, so the bad patch is diffed and rolled back rather than hand-reverted. Run Spotless before you commit so the helper and every edited caller carry the project's formatting.
Step by step
- 01Ask Atlas's codebase_search for the behavior, not the exact code, for example the logic that normalizes a currency amount, so near-duplicate Java methods surface even where grep would miss them.
- 02Open each hit with Atlas's read tool and confirm the Java copies are genuinely equivalent, checking exception signatures, null handling, and generics before collapsing them.
- 03Decide where the helper belongs in the Maven source tree, for example src/main/java/com/example/common/AmountUtils.java, in a package the duplicated classes can already reach through pom.xml.
- 04Create the shared helper with Atlas's write tool, which shows the full diff in the permission prompt before the file is created.
- 05Add JUnit 5 tests for the new helper under src/test/java so the collapsed logic is tested once, directly, and run them with JUnit 5 via mvn test.
- 06Replace each duplicate with a call to the helper using apply_patch, one file per patch, so each swap is independently reviewable and revertible.
- 07Run the suite with bash after every swap, using JUnit 5 via mvn test, rather than running Maven once at the end.
- 08Run Spotless over the helper and every edited caller so the refactor matches the project's formatting rules.
- 09Finish by grepping for any surviving copy of the old logic, and only then let Atlas stage and create the commit.
Frequently asked questions
- how to find duplicate code in a java project without grep
- Use Atlas's codebase_search and describe the behavior rather than the literal code. Java duplicates usually differ in variable names, so a semantic index built from AST declarations with tree-sitter finds them where grep cannot.
- how do I extract a shared helper class in java safely
- Create the helper with Atlas's write tool, which shows the full diff in the permission prompt before the file is created, then replace each duplicate with apply_patch one file per patch and run JUnit 5 via mvn test after every swap.
- should I run mvn test after every file or once at the end
- Run JUnit 5 via mvn test after every swap. Running Maven once at the end tells you something broke but not which of the callsite patches broke it, which is exactly the information you need during an extraction.
- does atlas edit java files without showing me the diff
- No. Atlas computes a unified diff for every file edit and surfaces it for approval before writing, and every tool call is permission-gated against allow, ask, and deny rules before it runs.
- can atlas undo a bad refactor in a maven module
- Yes. Atlas snapshots file changes as git patches so edits can be diffed and rolled back. Because apply_patch works one file per patch, a single bad callsite swap can be reverted without touching the other Java classes.
- what if a java file changed while atlas was working on it
- Atlas's apply_patch is context-anchored: it seeks the hunk's surrounding lines, so a drifted Java file causes the patch to fail rather than apply in the wrong place. You re-read the file and re-issue the patch.
- how do I keep spotless formatting when atlas adds a new class
- Run Spotless over the new helper class and every edited caller after the patches land, then review the resulting diff. Atlas surfaces the formatting changes as a unified diff for approval before writing, like any other edit.
- how do I prove no duplicated copies survived in java
- Finish by grepping for the old logic's distinctive tokens after every apply_patch has landed and JUnit 5 via mvn test is green. A surviving copy that codebase_search missed is still a duplicate, and grep is the last check.
Try Atlas in your terminal
The terminal-native AI coding agent. Free core, single binary.
Install AtlasRelated guides
Extract a Shared Helper from Duplicated Code with Atlas (2026 Workflow)
How to extract a shared helper from duplicated code with Atlas in 2026: codebase_search finds the copies by meaning, write creates the module, apply_patch swaps each call.
Atlas for Java in 2026
Adopt Atlas, the terminal-native AI coding agent, for Java development in 2026. Enhance your workflow with intelligent code search, refactoring, and robust safety features for Maven and Gradle projects.
Locate Where a Behavior Is Implemented in Java with Atlas (2026)
Atlas pinpoints the Java class and method behind a behavior with codebase_search, grep through ripgrep, and the lsp tool's findReferences across your Maven modules.
Document a Java Module with a README using Atlas in 2026
In 2026, Atlas helps Java developers create accurate, up-to-date README documentation for modules. It leverages Maven, JUnit 5, and Spotless to reflect current code behavior.
Rename a Symbol Across the Repo in Java with Atlas (2026)
How to rename a Java class, method, or constant across a repo with Atlas in 2026: lsp findReferences for callsites, grep for strings, edit replaceAll, then mvn test.
Review a Pull Request in Java with Atlas (2026)
Atlas reviews a Java pull request by pulling the raw diff with bash, reading changed classes in full, and running the lsp tool's findReferences on every changed signature.
Add a Regression Test for a Java Bug Fix with Atlas (2026)
Lock a Java defect down in 2026 with a test that goes red before the fix and green after. Atlas proves both states from the real exit code, then patches with edit.
Run the Test Suite and Triage the Failures in Java with Atlas (2026)
Turn a wall of red Maven output into a ranked list of root causes. Atlas runs JUnit 5 via mvn test in 2026, saves the full log, and triages Java failures by cause.