Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a new method from selecting existing code block, Eclipse

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

like image 605
ian_scho Avatar asked Nov 26 '09 10:11

ian_scho


People also ask

What is Refactoring in eclipse?

Last modified: Sep 21, 2016. Refactoring is the process of modifying code without altering its behavior and meaning. We do refactoring to make code more understandable, easier to maintain or modify, or to bring it into conformance with our coding organizations standards.

How do I change my Eclipse code?

In Eclipse, go to Windows -> Preference -> Java -> Formatter. In Active Profile drop down box, select any profile, click Edit on right of it.

How do I change all references in eclipse?

To rename all uses of this msg variable, highlight the variable and select Refactor→ Rename, or right-click the variable and select Refactor→ Rename, opening the dialog shown in Figure 4-1.


2 Answers

Source:

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

But I'm getting the error messages:

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.

So some further refactoring is required as mentioned in the original question.

like image 69
ian_scho Avatar answered Oct 21 '22 04:10

ian_scho


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:

http://archive.eclipse.org/eclipse/downloads/drops/R-3.6-201006080911/images/extract-method-continue.png

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

http://archive.eclipse.org/eclipse/downloads/drops/R-3.6-201006080911/images/extract-method-multiple-return-values.png

like image 35
VonC Avatar answered Oct 21 '22 04:10

VonC