Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 576
Grundlefleck Avatar asked Jan 22 '10 14:01

Grundlefleck


People also ask

How do I refactor in Eclipse?

Refactoring using EclipseRight clicking on a Java element in the Package Explorer view and selecting Refactor menu item. Right clicking on a Java element in the Java editor and selecting Refactor menu item. Selecting a Java element in either the Package Explorer view or Java Editor and clicking Shift + Alt + T.

Is it possible to override a inner classes?

No, you cannot override private methods in Java, private methods are non-virtual in Java and access differently than non-private one. Since method overriding can only be done on derived class and private methods are not accessible in a subclass, you just can not override them.

Does Eclipse support refactoring?

Compositional RefactoringThis Eclipse bundle provides new Quick Assist actions to support refactorings in a compositional paradigm. In the compositional paradigm, the tool automates small, predictable steps of a refactorin...

How do you access a static inner class?

And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference. They are accessed using the enclosing class name. To instantiate an inner class, you must first instantiate the outer class.


2 Answers

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"

like image 197
Grundlefleck Avatar answered Sep 22 '22 08:09

Grundlefleck


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

like image 42
Tom Avatar answered Sep 22 '22 08:09

Tom