Eine Einführung in das Refactoring mit IntelliJ IDEA

1. Übersicht

Es ist nicht immer einfach, den Code sauber zu halten. Zum Glück sind unsere IDEs heutzutage ziemlich intelligent und können uns dabei helfen, dies zu erreichen. In diesem Tutorial konzentrieren wir uns auf IntelliJ IDEA, den JetBrains Java-Code-Editor.

Wir werden einige Funktionen sehen, die der Editor zum Umgestalten von Code anbietet, vom Umbenennen von Variablen bis zum Ändern einer Methodensignatur.

2. Umbenennen

2.1. Grundlegende Umbenennung

Beginnen wir zunächst mit den Grundlagen: Umbenennen. IntelliJ bietet uns die Möglichkeit, verschiedene Elemente unseres Codes umzubenennen: Typen, Variablen, Methoden und sogar Pakete.

Um ein Element umzubenennen, müssen Sie die folgenden Schritte ausführen:

  • Klicken Sie mit der rechten Maustaste auf das Element
  • Lösen Sie die Option Refactor> Rename aus
  • Geben Sie den neuen Elementnamen ein
  • Drücken Sie die Eingabetaste

Übrigens können wir die ersten beiden Schritte ersetzen, indem wir das Element auswählen und Umschalt + F6 drücken .

Beim Auslösen durchsucht die Umbenennungsaktion den Code nach jeder Verwendung des Elements und ändert sie dann mit dem angegebenen Wert .

Stellen wir uns eine SimpleClass- Klasse mit einer schlecht benannten Additionsmethode, someAdditionMethod , vor, die in der Hauptmethode aufgerufen wird:

public class SimpleClass { public static void main(String[] args) { new SimpleClass().someAdditionMethod(1, 2); } public int someAdditionMethod(int a, int b) { return a + b; } }

Wenn wir diese Methode in add umbenennen , erzeugt IntelliJ den folgenden Code:

public class SimpleClass() { public static void main(String[] args) { new SimpleClass().add(1, 2); } public int add(int a, int b) { return a + b; } }

2.2. Erweitertes Umbenennen

IntelliJ sucht jedoch nicht nur nach Code-Verwendungen unserer Elemente und benennt sie um. Tatsächlich stehen einige weitere Optionen zur Verfügung. IntelliJ kann auch in Kommentaren und Zeichenfolgen und sogar in Dateien, die keinen Quellcode enthalten, nach Vorkommen suchen . Parameter können bei überschriebenen Methoden in der Klassenhierarchie umbenannt werden.

Diese Optionen sind verfügbar, indem Sie erneut Umschalt + F6 drücken, bevor Sie unser Element umbenennen. Ein Popup wird angezeigt:

Die Option In Kommentaren und Zeichenfolgen suchen ist für jede Umbenennung verfügbar. Die Option Nach Textvorkommen suchen ist für Methodenparameter und lokale Variablen nicht verfügbar. Schließlich ist die Option Parameter in Hierarchie umbenennen nur für Methodenparameter verfügbar.

Also, wenn eine Übereinstimmung mit einer dieser beiden Optionen gefunden wird, IntelliJ ihnen zeigen, und bieten uns die Möglichkeit , einige der Änderungen Opt - out (sagen wir, für den Fall , passt es etwas in keinem Zusammenhang mit unserer Umbenennung).

Fügen wir unserer Methode etwas Javadoc hinzu und benennen dann den ersten Parameter um: a :

/** * Adds a and b * @param a the first number * @param b the second number */ public int add(int a, int b) {...}

Durch Aktivieren der ersten Option im Bestätigungs-Popup stimmt IntelliJ mit jeder Erwähnung der Parameter im Javadoc-Kommentar der Methode überein und bietet an, diese ebenfalls umzubenennen:

/** * Adds firstNumber and b * @param firstNumber the first number * @param b the second number */ public int add(int firstNumber, int b) {...}

Schließlich müssen wir beachten, dass IntelliJ intelligent ist und hauptsächlich nach Verwendungen im Bereich des umbenannten Elements sucht. In unserem Fall würde dies bedeuten, dass ein Kommentar außerhalb der Methode (mit Ausnahme des Javadoc), der eine Erwähnung von a enthält, für die Umbenennung nicht berücksichtigt worden wäre.

3. Extrahieren

Lassen Sie uns nun über die Extraktion sprechen. Durch die Extraktion können wir einen Code abrufen und in eine Variable, eine Methode oder sogar eine Klasse einfügen. IntelliJ handhabt dies ziemlich intelligent, da es nach ähnlichen Codeteilen sucht und anbietet, diese auf die gleiche Weise zu extrahieren.

In diesem Abschnitt erfahren Sie, wie Sie die von IntelliJ angebotene Extraktionsfunktion nutzen können.

3.1. Variablen

Beginnen wir zunächst mit der Extraktion von Variablen. Das bedeutet lokale Variablen, Parameter, Felder und Konstanten. Um eine Variable zu extrahieren, müssen wir die folgenden Schritte ausführen:

  • Wählen Sie einen Ausdruck aus, der in eine Variable passt
  • Klicken Sie mit der rechten Maustaste auf den ausgewählten Bereich
  • Lösen Sie die Option Refactor> Extract> Variable / Parameter / Field / Constant aus
  • Wählen Sie zwischen den Optionen Nur dieses Vorkommen ersetzen oder Alle x Vorkommen ersetzen , falls vorgeschlagen
  • Geben Sie einen Namen für den extrahierten Ausdruck ein (falls der ausgewählte nicht zu uns passt)
  • Drücken Sie die Eingabetaste

Beim Umbenennen können Tastaturkürzel anstelle des Menüs verwendet werden. Die Standardkombinationen sind jeweils Strg + Alt + V, Strg + Alt + P, Strg + Alt + F und Strg + Alt + C .

IntelliJ wird versuchen, einen Namen für unseren extrahierten Ausdruck zu erraten, basierend auf der Rückgabe des Ausdrucks. Wenn es nicht unseren Anforderungen entspricht, können wir es ändern, bevor wir die Extraktion bestätigen.

Lassen Sie uns anhand eines Beispiels veranschaulichen. Wir könnten uns vorstellen, unserer SimpleClass- Klasse eine Methode hinzuzufügen , die uns sagt, ob das aktuelle Datum zwischen zwei angegebenen Daten liegt:

public static boolean isNowBetween(LocalDate startingDate, LocalDate endingDate) { return LocalDate.now().isAfter(startingDate) && LocalDate.now().isBefore(endingDate); }

Let's say we want to change our implementation because we use LocalDate.now() twice and we would like to make sure it has the exact same value in both evaluations. Let's just select the expression and extract it in a local variable, now:

Then, our LocalDate.now() call is captured in a local variable:

public static boolean isNowBetween(LocalDate startingDate, LocalDate endingDate) { LocalDate now = LocalDate.now(); return now.isAfter(startingDate) && now.isBefore(endingDate); }

By checking the Replace all option, we made sure that both expressions have been replaced at once.

3.2. Methods

Let's now check how to extract methods using IntelliJ:

  • Select the expression or lines of code fitting the method we want to create
  • Right-click the selected area
  • Trigger the Refactor > Extract > Method option
  • Type in the method information: its name, its visibility and its parameters
  • Press Enter

Hitting Ctrl + Alt + M after selecting the method body works as well.

Let's reuse our previous example and say we want to have a method checking if any date is between to other dates. Then, we would just have to select our last line in the isNowBetween method and trigger the method extraction feature.

In the opened dialog, we can see that IntelliJ has already spotted the three needed parameters: startingDate, endingDate and now. As we want this method to be as generic as possible, we rename the now parameter into date. And for cohesion purposes, we place it as the first parameter.

Finally, we give our method a name, isDateBetween, and finalize the extraction process:

We would then obtain the following code:

public static boolean isNowBetween(LocalDate startingDate, LocalDate endingDate) { LocalDate now = LocalDate.now(); return isDateBetween(now, startingDate, endingDate); } private static boolean isDateBetween(LocalDate date, LocalDate startingDate, LocalDate endingDate) { return date.isBefore(endingDate) && date.isAfter(startingDate); }

As we can see, the action triggered the creation of the new isDateBetween method, which is also called in the isNowBetween method. The method is private, by default. Of course, this could have been changed using the visibility option.

3.3. Classes

After all that, we might want to get our date-related methods in a specific class, focused on dates management, let's say: DateUtils. Again, this is pretty easy:

  • Right-click in the class that has our elements we want to move
  • Trigger the Refactor > Extract > Delegate option
  • Type in the class information: its name, its package, the elements to delegate, the visibility of those elements
  • Press Enter

By default, no keyboard shortcut is available for this feature.

Let's say, before triggering the feature, that we call our date-related methods in the main method:

isNowBetween(LocalDate.MIN, LocalDate.MAX); isDateBetween(LocalDate.of(2019, 1, 1), LocalDate.MIN, LocalDate.MAX);

Then we delegate those two methods to a DateUtils class using the delegate option:

Trigger the feature would produce the following code:

public class DateUtils { public static boolean isNowBetween(LocalDate startingDate, LocalDate endingDate) { LocalDate now = LocalDate.now(); return isDateBetween(now, startingDate, endingDate); } public static boolean isDateBetween(LocalDate date, LocalDate startingDate, LocalDate endingDate) { return date.isBefore(endingDate) && date.isAfter(startingDate); } }

We can see that the isDateBetween method has been made public. That is the result of the visibility option, that is set to escalate by default. Escalate means that visibility will be changed in order toensure current calls to the delegated elements are still compiling.

In our case, isDateBetween is used in the main method of SimpleClass:

DateUtils.isNowBetween(LocalDate.MIN, LocalDate.MAX); DateUtils.isDateBetween(LocalDate.of(2019, 1, 1), LocalDate.MIN, LocalDate.MAX);

Thus, when moving the method it's necessary to make it not private.

However, it's possible to give specific visibility to our elements by selecting the other options.

4. Inlining

Now that we covered extraction, let's talk about its counterpart: inlining. Inlining is all about taking a code element and replace it with what it's made of. For a variable, this would be the expression it has been assigned. For a method, it would be its body.Earlier, we saw how to create a new class and delegate some of our code elements to it. But there are times we might want to delegate a method to an existing class. That's what this section is about.

In order to inline an element, we must right-click this element – either its definition or a reference to it – and trigger the Refactor >Inline option. We can also achieve this by selecting the element and hitting the Ctrl + Alt + N keys.

At this point, IntelliJ will offer us multiple options, whether we wish to inline a variable or a method, whether we selected a definition or a reference. These options are:

  • Inline all the references and remove the element
  • Inline all the references, but keep the element
  • Only inline the selected reference and keep the element

Let's take our isNowBetween method and get rid of the now variable, which now seems a bit overkill:

By inlining this variable, we would obtain the following result:

public static boolean isNowBetween(LocalDate startingDate, LocalDate endingDate) { return isDateBetween(LocalDate.now(), startingDate, endingDate); }

In our case, the only option was to remove all the references and remove the element. But let's imagine we also want to get rid of the isDateBetween call and choose to inline it. Then, IntelliJ would offer us the three possibilities we spoke about before:

Choosing the first one would replace all calls with the method body and delete the method. As for the second one, it would replace all calls with the method body but keep the method. And finally, the last one would only replace the current call with the method body:

public class DateUtils { public static boolean isNowBetween(LocalDate startingDate, LocalDate endingDate) { LocalDate date = LocalDate.now(); return date.isBefore(endingDate) && date.isAfter(startingDate); } public static boolean isDateBetween(LocalDate date, LocalDate startingDate, LocalDate endingDate) { return date.isBefore(endingDate) && date.isAfter(startingDate); } }

Our main method in the SimpleClass remains untouched as well.

5. Moving

Earlier, we saw how to create a new class and delegate some of our code elements to it. But there are times we might want to delegate a method to an existing class. That's what this section is about.

In order to move an element, we must follow these steps:

  • Select the element to move
  • Right-click the element
  • Trigger the Refactor > Move option
  • Choose the recipient class and the method visibility
  • Press Enter

We can also achieve this by pressing F6 after selecting the element.

Let's say we add a new method to our SimpleClass, isDateOutstide(), that will tell us if a date is situated outside an interval of dates:

public static boolean isDateOutside(LocalDate date, LocalDate startingDate, LocalDate endingDate) { return !DateUtils.isDateBetween(date, startingDate, endingDate); }

We then realize that its place should be in our DateUtils class. So we decide to move it:

Our method is now in the DateUtils class. We can see that the reference to DateUtils inside the method has disappeared as it's no longer needed:

public static boolean isDateOutside(LocalDate date, LocalDate startingDate, LocalDate endingDate) { return !isDateBetween(date, startingDate, endingDate); }

The example we just did works well as it concerns a static method. However, in the case of an instance method, things are not so straight-forward.

If we want to move an instance method, IntelliJ will search for classes referenced in the fields of the current class and offer to move the method to one of those classes (provided they are ours to modify).

If no modifiable class is referenced in the fields, then IntelliJ proposes making the method static before moving it.

6. Changing a Method Signature

Finally, we'll talk about a feature allowing us to change a method signature. The purpose of this feature is to manipulate every aspect of a method signature.

As usual, we must go through a few steps to trigger the feature:

  • Select the method to change
  • Right-click the method
  • Trigger the Refactor > Change signature option
  • Bring changes to the method signature
  • Press Enter

If we prefer to use keyboards shortcut, it's possible to use Ctrl + F6 as well.

This feature will open a dialog very similar to the method extraction feature. And so we have the same possibilities that when we extract a method: changing its name, its visibility and also adding/removing parameters and fine-tuning them.

Let's imagine we want to change our implementation of isDateBetween to either consider the date bounds as inclusive or exclusive. In order to do that we want to add a boolean parameter to our method:

By changing the method signature we can add this parameter, name it and give it a default value:

public static boolean isDateBetween(LocalDate date, LocalDate startingDate, LocalDate endingDate, boolean inclusive) { return date.isBefore(endingDate) && date.isAfter(startingDate); }

After that, we just have to adapt the method body according to our needs.

If we wanted, we could have checked the Delegate via overloading method option in order to create another method with the parameter instead of modifying the current one.

7. Pull Up & Push Down

Our Java code typically has class hierarchies – derived classes extend a base class.

Sometimes we want to move members (methods, fields, and constants) between these classes. That is where the last refactoring comes in handy: it allows us to pull up members from a derived class into the base class or push them down from a base class into each derived class.

7.1. Pull Up

First, let's pull up a method to the base class:

  • Select a member of a derived class to pull up
  • Right-click the member
  • Trigger the Refactor > Pull Members Up… option
  • Push the Refactor button

By default, no keyboard shortcut is available for this feature.

Let's say we have a derived class called Derived. It uses a private doubleValue() method:

public class Derived extends Base { public static void main(String[] args) { Derived subject = new Derived(); System.out.println("Doubling 21. Result: " + subject.doubleValue(21)); } private int doubleValue(int number) { return number + number; } }

The base class Base is empty.

So, what happens when we pull up doubleValue() into Base?

Two things happen to doubleValue() when we push “Refactor” in the above dialog:

  • it moves to the Base class
  • its visibility changes from private to protected so that the Derived class can still use it

The Base class afterward now has the method:

public class Base { protected int doubleValue(int number) { return number + number; } }

The dialog for pulling up members (pictured above) gives us some more options:

  • we can select other members and pull them up all at once
  • we can preview our changes with the “Preview” button
  • only methods have a checkbox in the “Make abstract” column. If checked, this option will give the base class an abstract method definition during the pull-up. The actual method will remain in the derived class but gain an @Override annotation. Consequently, other derived classes won't compile anymore then because they're missing implementation of that new, abstract base method

7.2. Push Down

Lastly, let's push down a member to the derived class. This is the opposite of the pulling up we just performed:

  • Select a member of a base class to push down
  • Right-click the member
  • Trigger the Refactor > Push Members Down… option
  • Push the Refactor button

As with pulling members up, no keyboard shortcut is available for this feature by default.

Let's push down the method again that we just pulled up. The Base class looked like this at the end of the previous section:

public class Base { protected int doubleValue(int number) { return number + number; } }

Now, let's push doubleValue() down to the Derived class:

This is the Derived class after pushing “Refactor” in the above dialog. The doubleValue() method is back:

public class Derived extends Base { private int theField = 5; public static void main(String[] args) { Derived subject = new Derived(); System.out.println( "Doubling 21. Result: " + subject.doubleValue(21)); } protected int doubleValue(int number) { return number + number; } }

Now both the Base class and the Derived class are back to where they started in the previous “Pull Up” section. Nearly, that is – doubleValue() kept the protected visibility it had in Base (it was private originally).

IntelliJ 2019.3.4 actually brings up a warning when pushing down doubleValue(): “Pushed members will not be visible from certain call sites”. But as we can see in the Derived class above, doubleValue() is visible indeed to the main() method.

The dialog for pushing down members (pictured above) also gives us some more options:

  • if we have multiple derived classes, then IntelliJ will push members into each derived class
  • we can push multiple members down
  • we can preview our changes with the “Preview” button
  • only methods have a checkbox in the “Keep abstract” column – That is similar to pulling up members: If checked, this option will leave an abstract method in the base class. Unlike pulling up members, this option will put method implementations into all derived classes. These methods will also gain an @Override annotation

8. Conclusion

In this article, we had the chance to deep dive into some of the refactoring features offered by IntelliJ. Of course, we didn't cover all the possibilities as IntelliJ is a very powerful tool. To learn more about this editor, we can always refer to its documentation.

Wir haben einige Dinge gesehen, wie wir unsere Codeelemente umbenennen und einige Verhaltensweisen in Variablen, Methoden oder Klassen extrahieren. Wir haben auch gelernt, wie man einige Elemente einbindet, wenn wir sie nicht alleine benötigen, Code an eine andere Stelle verschieben oder sogar eine vorhandene Methodensignatur vollständig ändern.