views:

39

answers:

2

I'm using Eclipse 3.something and would like to know if it is possible to create a new method from selecting a block of code? Obviously the method's signature would contain the necessary existing references and we can't return more than one variable from a method.

I have various methods where code related to presentation is mixed with code related to the logic. TY

+2  A: 

The refactorings are Extract Method (Alt-Shift-m) and Extract Local Variable (Alt-Shift-l).

http://beust.com/weblog/archives/000315.html

I'm getting the error message "Not all selected statements are enclosed by the same parent statement." or "The beginning of the selection contains characters that do not belong to a statement.", however. So some further refactoring is required as mentioned in the original question.

ian_scho
A: 

Note: in the upcoming Helios (eclipse3.6), the extract method is enhanced ("is", because it is available since September 2009, in the M1 release):

The Extract Method refactoring now handles selections that contain continue statements. To preserve the semantics of the existing code, the selection needs to include the last statement of the loop. In the extracted method, continue statements are changed to return:

Extract method refactoring with continue

For a selection that would need multiple return values in the extracted method, Eclipse now lists the conflicting variables in the error message:

alt text

VonC