Project: JDT
Bug 109280 [refactoring] [extract method] Extract Method fails to send a required parameter
Reported: 2005-09-12 07:45:11 | Status: resolved fixed

Applying the Extract Method refactoring to the following selection results in a new method that fails to compile. Original program: private int f(boolean b1, boolean b2) { int n = 0; int i = 0; // Extract Method from here if (b1) i = 1; if (b2) n = n + i; // to here return n; } Applying Extract Method on the selected code (signalled by the comments) results with the following code: private int f(boolean b1, boolean b2) { int n = 0; int i = 0; n = newMethod(b1, b2, n); return n; } private int newMethod(boolean b1, boolean b2, int n) { int i; if (b1) i = 1; if (b2) n = n + i; return n; } Problem: Eclipse did not identify that the local variable i should be sent as a parameter. The program doesn't compile anymore, because in the statement n = n + i; in the new method, "the local variable i may not have been initialized". I expected the refactored source to look like this: private int f(boolean b1, boolean b2) { int n = 0; int i = 0; n = newMethod(b1, b2, n, i); return n; } private int newMethod(boolean b1, boolean b2, int n, int i) { if (b1) i = 1; if (b2) n = n + i; return n; }

Files changed in fix:
org.eclipse.jdt.ui.tests.refactoring/resources/ExtractMethodWorkSpace/ExtractMethodTests/locals_in/A_test578.java org.eclipse.jdt.ui.tests.refactoring/resources/ExtractMethodWorkSpace/ExtractMethodTests/locals_out/A_test578.java org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/ExtractMethodTests.java org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/ExtractMethodAnalyzer.java org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/flow/FlowInfo.java