IntelliJ IDEA 2016.2 Help

Find and Replace Code Duplicates

The Find and Replace Code Duplicates action allows you to find code repetitions similar to the selected method or constant field, and replace them with calls to the original method or constant.

Example

BeforeAfter
public void method() { int a = 1; int b = 2; int c = a+b; int d = b+c; } ... private int add(int a, int b) { return a+b; }
public void method() { int a = 1; int b = 2; int c = add(a,b); int d = add(b,c); } ... private int add(int a, int b) { return a+b; }

To find and replace code duplicates

  1. Position the cursor within the method or a constant field whose duplicates you want to search for.
  2. Select Refactor | Find and Replace Code Duplicates from the main or context menu.
  3. In the dialog that opens, select the scope where IntelliJ IDEA shall look for code duplicates.
  4. For each found code duplicate, IntelliJ IDEA will prompt you to confirm the replacement.

See Also

Last modified: 5 September 2016