IntelliJ IDEA 13.1.0 Web Help

The Change Class Signature refactoring lets you turn a class into a generic and manipulate its type parameters. The refactoring automatically corrects all calls, implementations and overridings of the class.


Before After
// This is the class whose signature will be changed:

public class MyClass {
    // some code here

public class MyOtherClass {

    // Here are the references to MyClass:

    MyClass myClass;
    void myMethod(MyClass myClass) {
        // some code here
    // some code here

// Now we are going to add two formal type parameters
// to MyClass.
// Two formal type parameters have been added:

public class MyClass<Param1, Param2> {
    // some code here

public class MyOtherClass {

    // The references to MyClass have changed accordingly:

    MyClass<String, Integer> myClass;
    void myMethod(MyClass<String, Integer> myClass) {
        // some code here
    // some code here

// When performing the refactoring, String and Integer
// were specified as the default values for Param1 and
// Param2 respectively.

Use cases

  • The type parameters can have bounds extending other constructs including other type parameters from another class. For instance, the code like public class ClassA<T, E> can be easily transformed to public class ClassA<T, E extends T>. However, if the parameter order was initially public class ClassA<E, T>, the Change Class Signature refactoring may be useful for changing the parameter order in the class signature as well as in all its usages.
  • IntelliJ IDEA can successfully deal with rather complicated cases and can alter parameters and corresponding usages if one parameter depends on another.
Changing a class signature
  1. In the editor, place the cursor within the name of the class whose signature you want to change.
  2. Do one of the following:
    • Press Ctrl+F6Ctrl+F6Ctrl+6Ctrl+F6Ctrl+F6Ctrl+F6Ctrl+F6Ctrl+F6Shift+Alt+CCommand F6Command F6Alt Command C.
    • Choose Refactor | Change Signature in the main menu.
    • Select Refactor | Change Signature from the context menu.
  3. In the Change Class Signature dialog, use the available controls to manage the formal type parameters:
    • To add a new parameter, click add or press Alt+InsertAlt+InsertAlt+InsertAlt+InsertAlt+InsertAlt+InsertAlt+InsertAlt+Insert or Ctrl+NAlt+InsertCommand N or Control EnterControl N or Control EnterCommand N or Control Enter. Specify the parameter name and default type in the Name and the Default Value fields respectively.
    • To remove a parameter, select the parameter and click delete (Alt+DeleteAlt+DeleteAlt+DeleteAlt+DeleteAlt+DeleteAlt+DeleteAlt+DeleteAlt+DeleteAlt+DeleteCommand DeleteCommand DeleteCommand Delete).
    • To move a parameter up or down in the list, select the parameter and then use arrowUp (Alt+UpAlt+UpAlt+UpAlt+UpAlt+Up or Ctrl+Alt+AAlt+UpAlt+UpCtrl+Alt+UpCtrl+Shift+UpControl UpControl UpControl Alt Up) or arrowDown (Alt+DownAlt+DownAlt+DownAlt+DownAlt+Down or Ctrl+Alt+EAlt+DownAlt+DownCtrl+Alt+DownCtrl+Shift+DownControl DownControl DownControl Alt Down).
  4. To perform the refactoring right away, click Refactor.

    To see the expected changes and make the necessary adjustments prior to actually performing the refactoring, click Preview.

See Also


Web Resources: