Uncover the Hidden Gems: Exploring IntelliJ IDEA’s ‘Refactor This’ Feature

Yeldos Tanikin
Javarevisited
Published in
5 min readJan 4, 2023

IntelliJ IDEA is a popular IDE used by developers around the world. It has a wide range of features that make coding and refactoring easier and more efficient, but there are some hidden gems that are not always familiar to developers. One of these is the “Refactor This” feature, which is invoked by the shortcut Ctrl + Shift + Alt + T or by accessing it from the Refactor menu

that opens this menu

While it is not a feature that developers use daily, “Refactor This” has a range of options that can be incredibly useful in certain situations. In this article, we will explore what the “Refactor This” feature is capable of, and why it is worth familiarizing oneself with it.

Overview of the different refactoring options in “Refactor This”

“Refactor This” has several options for refactoring code, some of which may not be familiar to many developers. These options include:

  • Introduce Parameter Object: This option creates an object and extracts selected parameters in a method into the class. The class can be either an inner class, an existing class, or a normal class. It is useful when a method has many parameters, and it is easier to manage them in an object rather than as individual parameters. You can go with a nested class or just a regular class.
ParamaterObject class is created with selected parameters. Replaced parameters with the new class
  • Extract Delegate: This option creates a delegate for selected members, including fields or methods. The class you are refactoring will hold the new class and act as a proxy to invoke the delegate. This option is useful when you move a group of related methods or fields to a separate class, to make the code easier to read and maintain.
getName method is delegated to the newly created Delegate class
  • Extract Superclass: As the name suggests, this option creates a new abstract class. You can select the methods that you want to move to the abstract class, either by providing their definition or by making them abstract and leaving the definition in the class you are refactoring. It is helpful when you extract common methods from multiple classes and move them to a single abstract class.
The new Abstract Class named ExtractSuperClass
Moved calculateValue’s definition into the new abstract class
  • Extract Interface: This option is similar to the previous one, but it creates a new interface instead of an abstract class, with selected methods extracted from the class you are refactoring. The new interface will contain abstract methods with the @Override annotation in the class you are refactoring. It comes in handy when you extract common methods from multiple classes and move them to a single interface.
The new interface
Override annotations are added and it implemented the new interface named ExtractInterface
  • Pull Members Up: If you invoke “Refactor This” on a method that is not an implementation of an interface, and the class you are refactoring implements an interface or extends another class, this option will create an abstract definition of the method in the interface or abstract class. It will then refactor the class by adding the @Override annotation. It is useful when you move a method from a subclass to an abstract class or interface.
The selected method moved to the interface you have chosen
It adds appropriate annotation

Changes to the “Refactor This” feature in the past year

In the past, “Refactor This” had two additional options: Replace Constructor with Builder and Replace Constructor with Factory Method. However, these options have been moved to the dialog that can be invoked by pressing Alt + Enter on a constructor or a method.

Alt + Enter on a constructor

The Replace Constructor with Builder option is to create a builder class for a constructor with many parameters, it then replaces the usages of the constructor with the builder.

The Replace Constructor with Factory Method option is to move the creation of an object to a separate factory method.

Conclusion

In conclusion, the “Refactor This” feature in IntelliJ IDEA is a powerful tool that can help developers clean up and organize their code. While it may not be used on a daily basis, it is worth familiarizing oneself with the different refactoring options that it offers. From extracting common methods to creating parameter objects and delegates, “Refactor This” can help developers make their code more readable, maintainable, and efficient.

Overall, the “Refactor This” feature is one of the hidden gems of IntelliJ IDEA, and it is worth taking the time to learn about it and how it can be used in different situations. Whether you are a seasoned developer or just starting out, “Refactor This” is a valuable tool that can help you improve your code and make your work more efficient.

--

--

Yeldos Tanikin
Javarevisited

Software engineer Simplifying tech, exploring architectures & productivity. Making tech more accessible.