Community
Participate
Working Groups
When trying to use the extract method refactoring on the following codeblock I got an messagebox which told me "Selected statements contain a return statement but not all possible execution flows end in a return" public void foo(List list, Object constraint, int index) { Map m = new HashMap(); if (list instanceof List) { List f= (List) list; if (f.size() == -1) { if (m.isEmpty()) { // do sth } else { for (Iterator iter= m.keySet().iterator(); iter.hasNext();) { // do sth for (Iterator iterator = Collections.EMPTY_LIST.iterator(); iterator.hasNext();) { // do sth if (true == false) { // do sth return; } // do sth } } } } } } What I expected to get was the following: public void foo(List list, Object constraint, int index) { Map m = new HashMap(); if (list instanceof List) { List f= (List) list; doSth(f, m); } } private void doSth(List f, Map m) { if (f.size() == -1) { if (m.isEmpty()) { // do sth } else { for (Iterator iter= m.keySet().iterator(); iter.hasNext();) { // do sth for (Iterator iterator = Collections.EMPTY_LIST.iterator(); iterator.hasNext();) { // do sth if (true == false) { // do sth return; } // do sth } } } } }
This is difficult in the general case. E.g. here, there's no easy way to extract the selection: void m(boolean b) { // -- extract if (b) { return; } // -- extract System.out.println("was here"); }
Bernd has conviced me that the full-blown analysis and handling of the situation is often not required. It would already be helpful if the refactoring would not abort with a fatal error, but would just show an error and then do the extraction without considering the semantic shift of losing early returns. We could make a small change in ExtractMethodAnalyzer#analyzeSelection() and adapt the error message to tell that early returns will be lost: if (fReturnKind == UNDEFINED) { status.addError(RefactoringCoreMessages.FlowAnalyzer_execution_flow, JavaStatusContext.create(fCUnit, getSelection())); fReturnKind= NO; } Dirk, what do you think?
Fixed in HEAD.