This feature is only supported in the Ultimate edition.
On this page:
IntelliJ IDEA helps you find repetitive blocks of code in a certain range. This range can be a single file, a project, a module, or a custom scope. Results of analysis display in the dedicated tab of the Duplicates tool window.
Searching for duplicates
To search for duplicates
- Do one of the following:
- On the main menu, choose .
- Choose the Project Tool Window. command on the context menu of the editor, or
- In the Specify Code Duplication Analysis Scope dialog, specify the analysis scope (whole project, current file, uncommitted files (for the projects under version control), or some custom scope). In addition, you can include test sources into the analysis too.
Click OK, when ready.
- In the Code Duplication Analysis Settings dialog, do the following:
- Select languages to perform analysis in.
- For each language, check the options to define your preferences for the analysis.
For example, you can opt to request identical match for code fragments to be considered duplicates, or specify a certain limit below which the code constructs are not considered duplicates (to avoid reporting about each
ifconstruct in the source code).
- In the Duplicates tool window, explore search results.
- View the list of duplicates in the left pane of the tool window.
- View differences between the found duplicates in the right pane. Use the arrow buttons to place the selected duplicate in one of the sections of the differences viewer and compare fragments of the code.
- Navigate to the duplicates in the editor, using Jump to Source or Show Source commands of the duplicates context menu.
- Eliminate duplicates from the source code by clicking and specifying the method name and parameters in the Extract Method dialog. This procedure is similar to the Extract method refactoring, with the only difference that in case of duplicates analysis the repetitive blocks of code are found automatically.
Detecting duplicates on-the-fly
IntelliJ IDEA enables spotting duplicates on-the-fly. This is done by the inspection General | Duplicated Code.
If you stumble upon an existing duplicate, or somehow create one either by writing or pasting code, you will know it instantly:
Inspection is accompanied by quick fixes, which enable you to navigate to the detected duplicates, or view all of them in the Find tool window:
Note that IntelliJ IDEA helps avoid locating duplicates in the generated sources.
To do that, select the checkbox Ignore duplicated code in sources marked as generated in the inspection settings page: