Community
Participate
Working Groups
Build Identifier: M20100211-1343 The following code is to be refactored: public class TestRefactoring { public void test() { boolean head = false; for (int i = 0; i < 40; i++) { } for (int k = 0; k < 10; k++) { // Extract Method from here if (!head) { head = true; } // to here } } } Extract a method in the marked part. Extracted is: public class TestRefactoring { public void test() { boolean head = false; for (int i = 0; i < 40; i++) { } for (int k = 0; k < 10; k++) { extractedMethod(head); } } private void extractedMethod(boolean head) { // Extract Method from here if (!head) { head = true; } // to here } } This is obviously wrong, as the statement "head = true;" is now called multiple times instead of once. It does not matter, if more statements (like generating output etc. are added). In my opinion this is quite serious, as refactored code might just not work as expected without the user knowing. Reproducible: Always Steps to Reproduce: 1. write the above code 2. refactor it with "extract method" 3. verify it produced an error
Forgot to mention: this only happends with a first for loop (even if it does not do anything). Without the first for loop the behaviour is correct. Correct refactoring: public class TestRefactoring { public void test() { boolean head = false; for (int i = 0; i < 40; i++) { } for (int k = 0; k < 10; k++) { head = extractedMethod(head); } } private boolean extractedMethod(boolean head) { // Extract Method from here if (!head) { head = true; } // to here return head; } }
This works with 3.6, can you please try with that. *** This bug has been marked as a duplicate of bug 213519 ***