Multi-language Refactorings


IntelliJ IDEA was the first Java™ IDE to extensively implement many of the refactorings worked out and recommended in the ground-breaking book Refactoring: Improving the Design of Existing Code by Martin Fowler et al. Since then, code refactoring has been a major focus of development, with every release to date adding new and/or expanded refactorings. This page details some of the more significant Java refactoring features of IntelliJ IDEA (you will actually find many more in the product, plus code refactorings for JSP, XML, CSS and HTML, and JavaScript.


Only Java and Groovy refactorings are fully available in Community Edition. Basic XML, HTML and .property refactorings are available, too. Other languages are not supported unless refactoring features get added using plug-ins.


Live Demo: IntelliJ IDEA 8 New Refactorings


Extract Method Object

When you are extracting methods with multiple return values, IntelliJ IDEA automatically offers you to invoke the Extract Method Object refactoring, instead of giving up and resorting to manual refactoring. This new refactoring moves method into an ad-hoc created new class, converting all the local variables to its fields, so letting you further decompose the method into other methods on the same object.

Type Migration

Type Migration refactoring allows you to automatically change a member type (e.g. from integer to string), and data flow dependent type entries, like method return types, local variables, parameters etc. across the entire project. It also lets automatically convert variable or method return type between arrays and collections.

Extract Class

Enables extracting some of the fields and methods of a class into a separate, newly created class. This refactoring is useful, when a class has grown too large, too incoherent, or does too many things.

Inline Superclass

Inline Superclass is another extension to IntelliJ IDEA Inline refactorings family. When class A extends class B, you can use the Inline Superclass refactoring to push members from class B down to A and remove the class B. All usages of class B and its members are then replaced with class A and its members.

Introduce Parameter Object

When you have a large group of parameters, which go a long way being passed through a chain of delegating method calls, use the Introduce Parameter Object refactoring. It will create a wrapper class for those parameters, which you can then use instead.

Remove Middleman

The Remove Middleman refactoring replaces all calls to delegating methods with the equivalent direct calls. It also includes the option to automatically remove the delegating methods, which are so becoming obsolete.

Wrap Return Value

If you need to append some data to a method return value, use the Wrap Return Value refactoring. It creates a wrapper class, which already includes current method return value. You can modify the class to include the extra data you need.

XML-aware dedicated refactorings

Multiple new refactorings, including more than 50 dedicated XML-aware:

  • Rename tags and attributes
  • Wrap and unwrap tags
  • Replace attributes with tags and vice versa
  • Convert tags to attributes
  • Safely add and remove subtags and attributes
  • And more

Drag-and-drop items in the Project View bar

Support for drag-and-drop dir moving and copying classes and other items in the Project View

Invert Boolean

The Invert Boolean refactoring allows you to quickly change the sense of the Boolean method or variable to the opposite one.

Safe Delete

You can safely remove classes, interfaces, methods, fields, and parameters, keeping the code working and error-free. The Safe Delete Refactoring finds all the usages of the selected symbol within the open project or simply delete the symbol if no usages found.

Move Instance Method

This refactoring, separate from Move Static Method, safely moves non-static methods from one class to another. It correctly handles inner classes and honors method overriding.

Inline Constant

When you invoke this refactoring, IntelliJ IDEA replaces all usages of constant field with its initializer

Extract Subclass

The option Rename original class and use superclass where possible in Extract Superclass refactoring allows you to rename the original class and make it an implementation of the newly created superclass. IntelliJ IDEA will alter all original class usages to the usages of the implementing class only where it is still necessary.

Replace Method Code Duplicates

When this refactoring is called on a method, IntelliJ IDEA will locate all places in the current file where the selected method code is fully repeated and turn all such code blocks into the corresponding method calls.

Convert To Instance Method

This refactoring is used to convert a static method into an instance method with an initial method call argument being a prototype of newly created instance method call qualifier.

Renaming of packages, classes, methods, fields, method parameters and local variables with reference correction

The renaming functionality allows you to rename any package, class, method or variable and automatically finds and corrects all references to it. Before renaming, you can preview all references in a convenient tree-view, where you have an opportunity to select usages/references to be renamed. For any item being refactored, it is also possible to search for its name occurrences in strings and comments, so that the commented code is also changed and kept up-to-date. The Rename refactoring can be invoked from any view (Project, Source, Commander and Structure), as well as directly from the editor.

Moving classes and packages with reference correction

You may move classes or packages between packages with automatic reference correction. This allows you to easily refactor the package structure of your project.

Moving static members with reference correction

You may move static methods, fields or inner classes between classes with automatic reference correction.

Move Inner Class to Upper Level

Use this refactoring to move an inner class to the same level with its enclosing class. In case the inner class uses members of the enclosing class, there is a possibility to pass a reference to the enclosing class as an argument to the moved class’ constructors. It is especially useful to apply this refactoring to large inner classes so that the code becomes more readable.

Change Method Signature

Now with new "Propagate Parameters/Exceptions" feature, this refactoring combines several different modifications that can be made to a method signature. The possible modifications are:

  • Add parameter
  • Remove parameter
  • Reorder parameters
  • Change return type
  • Change parameter type

Together with changing the signature itself, IntelliJ IDEA will search for all usages of the method and will change all calls, implementations and overridings of the method to reflect the change. In case of an added parameter, a default value for it has to be supplied to be used for method calls.
You can also specify how deep the new parameter or thrown exception should be propagated through the method call hierarchy.

Make Method Static

This refactoring is used to convert a non-static method into a static one to make such method functionality available to other classes without creating a class instance. Also it is useful as a preparatory step for the Move Members refactoring to move this method to another class. After refactoring, the target method will be declared as static and necessary parameters, if any, will be added to its call sites.

Copy/Clone Class

This refactoring allows you to copy an entire class from one package to another or to clone such class within the same package. The package statement and the class name are adjusted automatically.

Extract Method

Select a block of code and invoke Extract Method to turn it into a method. It will automatically analyze the code to detect which variables of the original method must be passed to the extracted method as parameters, which variables being modified by this code fragment are used afterwards. In the displayed dialog you will be able to set the name for the new method, as well as re-order and possibly specify better names for its parameters.

Live Demo

Introduce Variable

Select some expression inside the code and invoke Introduce Variable. This will declare a local variable and assign the expression’s value to it. The original expression will be replaced with the name of the variable. If there are multiple occurrences of the original expression in your code, you will be given an option to replace all of them with the newly generated variable.

Introduce Field

Select some expression inside the code and invoke «Introduce Field». This will declare a field and assign a value of the expression to it. The original expression will be replaced with the name of the field. It is possible to initialize the field:

  • At the position of the expression being refactored
  • In the field's declaration
  • In constructors of the class

Same as for Introduce Variable, there is an option to replace all occurrences of the selected expression in the class.

Live Demo

Introduce Constant

Use the refactoring Introduce Field as a «shortcut» to rapidly create static final fields. As a particular case, this refactoring covers the Replace Magic Number With Constant refactoring.

It's capable of replacing all occurrences of an expression with the constant.
Before:

After:

Introduce Parameter

This refactoring is used to add a parameter to a method declaration if you need some value to be passed in a method call. The method usage(s) are updated as well. References in the parameter are resolved unlike in the Change Method Signature refactoring (where an introduced statement is simply inserted as text).

Extract Interface

Invoke Extract Interface on a class or interface. Then select methods and/or static final fields. A new interface, containing the selected members will be created and the class will be changed to implement this interface. During the second stage of the refactoring there is an option to search for all places where the interface can be used instead of the original class. When all such places are found, you may review them and approve the suggested changes for all places or just for selected ones.

Live Demo

Extract Superclass

The refactoring allows you to create a superclass for a given class and move some of the class’ functionality to the superclass. Invoke Extract Superclass on a class, then select methods and fields to be moved into a superclass. A new class will be created and the original class will be changed to extend the newly created class. All selected members from the original class will be moved to the superclass. Same as for Extract Interface, there is an option to search for all places where the superclass can be used instead of the original class, and perform the replacements.

Use Interface Where Possible

With this refactoring your code is generalized by replacing particular class usages with references to its superclass or interface. It is especially useful after the Pull Members Up refactoring.

Pull Members Up

This refactoring allows you to move class members to a superclass or interface.

Push Members Down

This refactoring helps you to clean up your class hierarchy by moving class members to a subclass or a subinterface.

Replace Inheritance with Delegation

This refactoring allows you to delegate the execution of specified methods derived from the base class/interface to an instance of the ancestor class or an inner class implementing the same interface.

Inline Local Variable

This refactoring is opposite to Introduce Variable. It will replace all references to a variable with the variable’s initializer expression.

Inline Method

This refactoring is opposite to Extract Method. It will inline all invocations of a certain method and remove the method declaration. When invoked on a particular method’s usage, there is an option to inline only this usage and keep the method. This refactoring is often useful as a part of complex refactorings.

Convert Anonymous Class to Inner

Invoke this refactoring on an anonymous class to convert it to a named inner class. If the anonymous class accesses local variables, a constructor will be created for the inner class and values of the accessed variables will be passed to it. If the anonymous class is located in a non-static method, you may also choose an option to pass a reference to the outer class to the constructor. It is especially useful to apply this refactoring to large anonymous classes, so that the code becomes more readable. Additional benefit is the possibility to share the functionality provided by the inner class.

Live Demo

Encapsulate Fields

Select some class and invoke Encapsulate Fields. This refactoring allows you to replace direct read/write access to the selected fields with corresponding accessor methods. The visibility of the selected fields may also be changed. In case the required accessors do not exist, they will be generated, otherwise existing getters and setters will be used. Also, it is possible to encapsulate read and write access to the selected fields together or separately.

Replace Temp With Query

Select some local variable in the editor and invoke Replace Temp With Query. This will extract the variable’s initializer expression into a method, and replace all references to the variable with calls to the extracted method. The declaration of the variable will be removed.

Replace Constructor With Factory Method

This refactoring allows you to replace all explicit class instantiations that use a particular constructor with a call to a static factory method.

to the top