IntelliJ IDEA
IntelliJ IDEA – the Leading Java and Kotlin IDE, by JetBrains
Change Method Signature Refactoring for ActionScript and Flex
Hi all ActionScript/Flex devs! I hope you enjoy using IntelliJ IDEA and I’m sure you’ll like it even more after you try Change Method Signature refactoring! It’s amazing how it allows you to quickly restyle your methods not only by renaming them, but also by adding, reordering, renaming, and deleting parameters, changing their types and default values.
All usages and overriding method declarations will be updated automagically, so your application compiles and works the same way as before with no extra care from your side!
Let me show you how it all works.
In my code, the second print() call has an extra argument. To resolve the problem, I invoke quick-fix that suggests to change method signature so that it would match usage:
The dialog opens with the expected parameter list, so all I need is just to give new parameter meaningful name and press Refactor:
Voila! Both usages are correct!
Now time for a deeper look. Assume I work on a simple Painter application, where I have Shapes
Canvas that holds and paints shapes, and main Application component that controls everything:
At some point I may decide to introduce wireframe mode to improve performance on complex drawings. Consequently, I’ll need to have the flag in paint() method that comes from main component. Also, to draw outline of dragged shape I need to provide an offset parameter. Finally, I’d like to change existing parameter name to a more meaningful, like graphics.
I place the caret at IShape.paint() method declaration (so that implementing methods will also be handled) and invoke Refactor | Change Signature action. Dialog will open, showing current method name, return type, parameters list and signature preview:
When specifying/changing parameter type and default value, IntelliJ IDEA offers context-aware completion and error highlighting:
…so it’s easy to refactor your code, but hard to make a mistake.
After playing with parameters I came up to the following result:
Note that I don’t need to specify default value for an optional parameter offset.
Now I need to propagate newly added parameters up to the main component class. For this I just need to click Propagate Parameters button and tick needed methods in call tree:
Done! Let’s invoke the refactoring and look at the code:
You see that IntelliJ IDEA updated methods declarations and usages exactly how I needed. Note that my main component has got only one additional argument (for wireframe parameter), since I haven’t provided default value for optional offset.
As usual, this refactoring is available from UML diagram.
Improve your code with pleasure!