Community
Participate
Working Groups
package p; public class Test { public static void main(String[] args) { for (int i = 0; i < 1; i++) { int idx = 0; for (int j = 0; j < 3; j++) { idx++; System.out.println(idx); } // for-loop to extract: for (int j = 0; j < 3; j++) { idx++; System.out.println(idx); } } } } Extracting the second for loop does not consider the first for loop as a valid duplicate as the first loop should return an 'int' whereas the second for loop returns 'void'. This can be improved by creating an extracted method that returns 'int': public class Test { public static void main(String[] args) { for (int i = 0; i < 1; i++) { int idx = 0; idx = extracted(idx); idx = extracted(idx); } } private static int extracted(int idx) { for (int j = 0; j < 3; j++) { idx++; System.out.println(idx); } return idx; } } See bug 479559 comment #9: > For the example in comment 0, it would actually be nice to have the > refactoring find the duplicate code. If you add another > System.out.println(idx); after to for-loop to extract, or if you extract the > first for-loop, the refactoring already works fine and uses the return value > of the extracted method to update the idx variable. a() and b() in the > snippet from comment 6 are the same case. > > Implementing that is a bit trickier and should probably left as a separate > enhancement request: In case the extracted snippet returns void but a > structural duplicate returns a value, we could create an extracted method > that returns that value. That would also mean the return type of the > extracted method would have to change depending on whether duplicates should > be extracted or not. It should also consider the case where multiple duplicates are present and have different return values. See bug 479559 comment #6 attachment 265525 [details] for example. In this case, we can either extract only the selected snippet returning 'void' without any duplicates (the current behavior) or, choose a return value (random or one being returned the max no. of times) from the duplicate snippets to create the extracted method.
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug. If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant. -- The automated Eclipse Genie.