Matthew Stopa
Matthew Stopa

Reputation: 3875

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?

Upvotes: 16

Views: 7314

Answers (5)

VonC
VonC

Reputation: 1329232

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)

Upvotes: 9

sondlerd
sondlerd

Reputation: 1025

Eclipse (Luna 4.4.0) now has "Extract Method..." as an option under the Refactor context menu.

Upvotes: 0

In Eclipse 3.7.1 there is an option to move methods and fields out of a class. To do so:

  1. Make sure the destination class exists (empty class is fine, just as long as it exists in the project).
  2. In the source class, select the methods that you want to remove (the outline view works great for this), right click on the selection, and choose Move
  3. Select the destination class in the drop down/Browse

Your members are now extracted. Fix any visibility issues (Source > Generate Getters and Setters is very useful for this) and you are all set.

Upvotes: 2

kc2001
kc2001

Reputation: 5249

Right now, I have resorted to using "copy class and delete methods" as described above or using IntelliJ's Extract Class, which does move both methods and classes, albeit buggily.

I'm hoping that others will vote for fixing bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=312347. I'd really like Eclipse to have a fully functioning extract class.

Upvotes: 0

jprete
jprete

Reputation: 3759

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.

Upvotes: 6

Related Questions