Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to extract a class with methods in eclipse?

I am trying to refactor a class that I have into 2 classes. Unfortunately eclipse's extract class function seems to only support the variables which really isn't all that helpful. Is there a way to extract the methods or is there a plugin that does it?

like image 556
Matthew Stopa Avatar asked Nov 27 '09 01:11

Matthew Stopa


People also ask

How do you extract a method?

To extract method: Select a code fragment you want to extract to a method. Press Ctrl+Alt+M or from the main menu, select Refactor | Extract | Method. In the dialog that opens, configure a method options, such as visibility, parameters, and so on. You can also change a name of the method if you need.

How do I view a method in Eclipse?

-Or- press Ctrl+Shift+T (for a type) or Ctrl+Shift+M (for a method). The "Open Type/Method" dialog will appear. If a type/method was previously selected in the editor or outline views, it will be displayed.

How do I extract a class in Eclipse?

Highlight a class name and select Refactor→ Extract Interface, or right-click the class name and select Refactor→ Extract Interface. Enter the name of the interface you want to extract, select the members to declare in the interface, and click OK.

What is refractor in Eclipse?

Refactoring refers to the reshuffling of code to improve its style, design, etc. The outcome of running the code generally doesn't change as far as the user is concerned, but the guts under the hood do. I'm not going to deal with refactoring so much as particular refactoring tools in Eclipse.


2 Answers

One other solution is to CTRL C-CTRL V your class into another one, and start removing what you do not want...

You can select multiple elements in the outline view of the new class and select delete.
Crude but effective.

alt text
(source: eclipse.org)

like image 126
VonC Avatar answered Oct 06 '22 23:10

VonC


The "Extract Superclass" refactoring is much more useful for this. It will allow you to pick the members and the methods you want, and then use "Add Required" to add any members of the class that are required to make the methods you chose work.

like image 22
jprete Avatar answered Oct 07 '22 00:10

jprete