Using Replace Method Code Duplicates
IDEA really shines when you can use Locate Duplicates to find repeated code fragments and then do Extract Method to extract the fragement into a new method, and IDEA replaces all the fragments with the method call.
I hadn't used the Replace Method Code Duplicates before, but recently started to try it out, but I am having some problems.
For example, I did Analyze->Locate Duplicates on a file. The top duplicate found had 3 duplicates of the following code:
I highlight one of the code fragments, and invoke ExtractMethod.
(BTW Would be great if you can invoke Extract Method directly from Find - Duplicates Tool Window.
http://www.jetbrains.net/jira/browse/IDEABKL-2623. Extract Method seems much more common an
operation than the Send to Left, Send to Right buttons on the Find - Duplicates results window.)
Enter new method name "buildMonitorGroupMap" and Hit OK.
Process Duplicates Dialog pops up.
"IDEA has detected 2 code fragments in this file that can be replaced with a call to extracted method. Would you like to review and replace them?"
Yes/No
If I say No, and finish the Extract Method, the code has changed to:
and I have a new method:
Now if I put my cursor on the new method and invoke
Replace Method Code Duplicates, IDEA doesn't find any:
"IDEA has not found any code that can be replaced with method call?"
Shouldn't Replace Method Code Duplicates find the same thing that Extract Method found?
Seems like a bug.
Please sign in to leave a comment.
I just found out that even if the caret is not on a method when I invoke Replace Method Code Duplicates, IDEA reports:
"IDEA has not found any code that can be replaced with method call."
It should give the same error that Change Signature gives if the caret is not on a method name:
"Cannot perform refactoring.
The caret should be positioned at the name of the method or class to be refactored. Ok/Help"
What I really want to do with the Replace Method Code Duplicates is to select a method and then set an analysis scope just like Locate Duplicates, so I can search a module for all places where the call to the selected method can replace existing code.
That would be a very powerful feature. But it looks like right now, it only searches in the existing file?