Tips & Tricks

Extract Interface and Extract Superclass refactorings for ActionScript/Flex

ActionScript/Flex development support in IntelliJ IDEA eventually gets closer to the unbeaten level of its Java assistance. Today it’s time to Extract Interface and Superclass.

Let’s illustrate this feature on a simple example…

Assume we’re writing a graphic editor that operates with shapes. We’ve already implemented the first shape:

Also we have a Canvas class that can operates on shapes (let’s ignore the underlines for now):

Eventually we realize that some of the members of RectangleShape are actually valid for any shape, so we’d like to create an interface which RectangleShape will implement.

To do that, we just need to put the caret anywhere in the source class, interface or MXML component (or select the file in Project view) and invoke corresponding action in context menu Refactoring group.

First, we choose if we simply want to

  • extract interface, pull up selecte d members and leave other things as is.

Other (and more powerful) options are:

  • replace all the usages of original class with the usages of super interface (where possible)
  • extract implementation class, and keep all the usages untouched (where required usages will be updated keeping the code green)

Let’s stick to the option 2. Then, we enter the name and package of the interface we want to create.

Now, it’s time to choose which members should reside in there. As with the other refactorings, IDEA will notify us of an unselected member that is used by a moved one with red color.

When extracting an interface, we can contr

ol how to process methods’ ASDoc: move it along with the target, copy, or leave as is.

Our class implements an interface (IVisible), you we pull this inheritance relation up to the super interface as well. IntelliJ IDEA will highlight interface methods with blue.

Before changing our code, IntelliJ IDEA will check for the conflicts (e.g. methods that become inaccessible after move). If found, you can review the problems in advance to be sure that no single line will get broken behind our back.

Now let’s look at the code after we press OK!

Please say welcome to the new IShape interface:

We see that RectangleShape class now implements it:

And what about the usages? Here they are:

Note that usages of original class (blue underline) were replaced with interface usages where possible (orange underline). The only exception is the constructor call (red underline).

Of course, you can Extract superclass quite the same way.

Happy refactoring!

PS. Needless to say, these refactorings are also accessible on UML diagram.

image description