Extracting a class in Eclipse

It should be possible, but I just can't figure it out in Eclipse. I have

/** ClassA.java */ class A { ... } class B { ... } 

and I would like to select class B and extract it to my file to get:

 /** ClassA.java */ class A { ... } 

and

 /** ClassB.java */ class B { ... } 

How to do it in Eclipse?

+7
java eclipse
source share
5 answers

Here's how you do it (works in Eclipse 3.5):

  • Select your code to retrieve:

     /** ClassB.java */ class B { ... } 
  • To cut

  • Right-click the package in which you want to place it

  • Choose a paste. (Then arrange the import if necessary.)

+11
source share

In my Eclipse (3.6 - Helios SR1), I highlighted the type name ( B ) and then right-click on the popup menu and then choose Refactor > Move Type to New File ...

Edit: In previous versions, it was called "Convert Member Type to Upper Level", but has been updated to work with several types:

http://download.eclipse.org/eclipse/downloads/drops/R-3.6-201006080911/eclipse-news-part2.html

Move type to new file refactoring.

Converting a member type to a higher level refactoring has been renamed Move Enter into a new file and now allow the secondary type in the moved file to your own file. The action continues to work for member types.

+6
source share

If there is no specific function, then create a new class, copy and paste the contents of B, and then press CTRL - SHIFT - O on both files (or at least A.java) to clear the import should do this.

This is not a very common thing to ask for, and it is fairly easy to do manually.

Edit: You can also create B.java and then use Refactoring-> Move on B in A.java to move it to B.java. It seems that he did not copy the import, but when I did, while when copying and pasting manually, he automatically imported the import.

+1
source share

There is no functionality that I know of. You will need to create a java file of your choice and cut-paste the class into a new file. Correct import names and package declarations (if necessary).


EDIT The best way to do this: just create an empty B.java file, go back to A.java , select B.java and right-click and select Refactor -> Move ( Alt+Shift+V for short). He will move class B to B.java.

I tested and works. You may need to manage the import.

+1
source share

Place the caret somewhere inside class B (maybe it should be inside the class name). Select Refactor -> Move type to new File .

(This is the name of the action in Eclipse 3.6. I assume that in earlier versions it was Move member type to top level )

Reference:

0
source share

All Articles