How to refactor a static inner class to a top level class in Eclipse?
Asked Answered
M

6

34

I am having trouble finding the correct refactor option for the following scenario:

I have code like this (in Outer.java):

public class Outer {

    // ... class stuff

    public static class Inner {
        // ... inner class stuff   
    }
}

I am looking for a way to select Inner, and have it converted to a top level class, in it's own .java source file. I know this is pretty easy to do manually with copy/paste etc., but the inner class is referenced in a lot of places, and I would like the refactor option to handle the change everywhere it is referenced.

I have tried the option Refactor -> Extract Class... but that does something weird with a field called data that I don't quite understand.

Eclipse version is 3.5.1 Galileo.

How do I refactor a static inner class to be a top level class?


Edit: Can't believe I overlooked the option to do this. Thanks for all your correct answers, +1 to you all. I'll still need to accept an answer, so if there is any more useful info, e.g. gotchas with the script, there is still a purpose to leaving an answer.

Magnetism answered 22/1, 2010 at 14:43 Comment(0)
M
51

This is so easy I can't believe I missed it:

With the cursor anywhere within the inner type, right click and select:
Refactor -> Convert Member Type to Top Level...

(There is no shortcut in the default settings)

This automatically extracts the inner type, and places it in it's own file, in the same package and directory as the outer type.

Update

In later versions of Eclipse this refactoring has been renamed "Move Type to New File"

Magnetism answered 22/1, 2010 at 14:45 Comment(4)
It seems like in the Helios release the functionality has been renamed to "Move Type to New File...".Clevie
And make sure to select only the class/interface, not all of its content, too.Pol
This also works for android studio, with one additional step (refactor -> move -> ...), and can also be achieved with shortcut F6.Vanzant
What if there are so many inner classes? Do I have to do it all one-by-one?Bollix
E
6

For completeness, version 4.x of Eclipse has changed terminology and now they call that operation as Move Type to New File...

Move type to new file

Epode answered 29/6, 2015 at 12:10 Comment(0)
E
3

Extract Class is not what you want.

I just did this using Eclipse 3.4.2

  1. Select Inner
  2. Right click 'Refactor->Convert Member Type To Top Level...'
Ennis answered 22/1, 2010 at 14:51 Comment(0)
C
2

I have 3.4.1, and if I right-click on Inner, >refactor, I have the option "Covert member Type to top level..", which does exactly what you want.

Cain answered 22/1, 2010 at 14:48 Comment(0)
S
2

Use Refactor -> Convert Member to Top Level Type

Sally answered 22/1, 2010 at 14:51 Comment(0)
E
2

For Intellj2018.1.

  1. right click in the inner class
  2. Refactor
  3. Move
  4. Move to upper level

enter image description here

enter image description here

Epidiascope answered 9/5, 2018 at 2:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.