IntelliJ IDEA 2017.3 Help

Generify Refactoring

The Generify refactoring is designed to transform existing code that does not use Generics, into the Generics-aware code. The refactoring analyzes existing code, and for each raw type creates safe and consistent parameter type.

IntelliJ IDEA tries to generate code, which is as correct as possible from the Java point of view. In other words, each context introduces some type restrictions, and the refactoring produces the best possible type, that does not contradict to the existing contexts.

Example

BeforeAfter
public void method() { List list = new LinkedList(); list.add("string"); }
public void method() { List<String> list = new LinkedList<String>(); list.add("string"); }

To generify

  1. Select the level of code transformation, which can be a method, a class, a package or directory, in the Project or Structure view, or place the cursor on the class or method name in the editor. If you want to apply generics to a single code fragment, just select it in the editor.
  2. On the main menu, or on the context menu of the selection, choose Refactor | Generify. The Generify dialog box appears.
    img
  3. Define the refactoring options. Refer to the dialog description for details.
  4. Preview and apply changes.
    img
Last modified: 6 March 2018

See Also

Reference: