IntelliJ IDEA 2024.3 Help

Wrap return value

The Wrap Return Value refactoring allows you to select a method, and either create a wrapper class for its return values, or use an existing, compatible wrapper class. All returns from the method selected will be appropriately wrapped, and all calls to the method will have their returns unwrapped.

Wrapping a method's returns are useful, if your design changes in such a way that you want a method to return more information than originally planned. After wrapping, the wrapper class can be extended, allowing more data to be returned from the method. Also, it is common to wrap primitive return values, thus allowing interface and implementation to be decoupled as needed.

Install the Additional Java Refactorings plugin

This functionality relies on the Additional Java Refactorings plugin, which you need to install and enable.

  1. Press Ctrl+Alt+S to open settings and then select Plugins.

  2. Open the Marketplace tab, find the Additional Java Refactorings plugin, and click Install (restart the IDE if prompted).

Run the Wrap Return Value refactoring

  1. Open the desired class in the editor and place the caret at the method whose returns you wish to wrap.

  2. In the main menu or in the context menu, go to Refactor | Wrap Return Value.

  3. In the dialog that opens, specify the name and package for the new wrapper class, or select an existing compatible wrapper class.

  4. Preview and apply changes.

Example

Before

After

class Order { String customer; String getCustomer() { return customer; } }
class Order { String customer; Wrapper getCustomer() { return new Wrapper(customer); } } public class Wrapper { private final String value; public Wrapper(String value) { this.value = value; } public String getValue() { return value; } }

Wrap Return Value dialog

Use this refactoring to create a wrapper class around the return values of a method, or use a compatible existing class as a wrapper.

Item

Description

Method to wrap returns from

This read-only field shows the name of the selected method.

Create new class

Click this radio-button to create a new wrapper class. If this option is selected, specify the class and destination package name in the fields below.

Class name

Type the name of the new wrapper class.

Package name

By default, the current package name is displayed. You can type a different package name in the text field, or click the ellipsis button and select the destination package from the tree view. If the desired package doesn't exist, click to create a new one.

Target destination directory

Use this field to select the target destination directory. By default, the current destination directory is displayed. You usually choose the target destination based on a current package. If this package exists in multiple roots, you can click arrow button and select Leave in same source root from the list. In this case, wrapper would be placed near the initial class.

You can click the ellipsis button to open Choose Destination Directory window.

You can choose Directory Structure tab to select another destination directory or choose Choose By Neighbor Class tab to place a wrapper near the neighbor class if, for example, you want to put the wrapper in util directory near your Pair or Triple classes, and you do not remember the exact package, putting wrapper near the Pair class would save you time.

Use existing class

Click this radio-button to use an existing class of your choice as a wrapper.

Name

Specify the name of the desired wrapper class. Note that such class should contain a constructor with a parameter of the same type as the return value in question.

You can type the fully-qualified class name in the text field, or click the ellipsis button and choose the desired class in the Select parameter class dialog. Note that you can select the desired wrapper class both from the project and non-project classes.

Wrapper field

Select the field that will store the return value, from the list of fields, encountered in the specified wrapper class.

Inner class

Click this radio-button to create an inner class. You might want to do that if, for example, you have a private method. In this case you can leave everything in the same class.

Name

Specify the name of the inner class.

>

Last modified: 11 February 2024