Is this "overload method" refactoring implemented?

Answered

Often I have need to do either of two things
- take an existing method and wrap it with new overloaded method with some
default parameters
- take an existing method, add some extra parameters and wrap it in new
overloaded method matching original method signature
- variations of the two above where wrapped method getas renamed and made
private and its parameters gets rearranged


Right now it is always a manual cut an paste thing for me. As far as I know
there is nothing like this available at the moment or did I miss it? I wish
it could be done as a refactoring.

Would it be useful for you?
What do you think?

Thanks,
Alex


6 comments
Comment actions Permalink

If you do the "Change Signature" refactoring there is a radio button at
the top of the dialog to select "Delegate via overloading method"...

N.

Alex Roytman wrote:

Often I have need to do either of two things
- take an existing method and wrap it with new overloaded method with some
default parameters
- take an existing method, add some extra parameters and wrap it in new
overloaded method matching original method signature
- variations of the two above where wrapped method getas renamed and made
private and its parameters gets rearranged


Right now it is always a manual cut an paste thing for me. As far as I know
there is nothing like this available at the moment or did I miss it? I wish
it could be done as a refactoring.

Would it be useful for you?
What do you think?

Thanks,
Alex

0
Comment actions Permalink

I have a similar, but different, need

I would like to take a given method and create default overloads for all parameters

public Class MyClass {

  public MyClass(Boolean argOne, String argTwo, Integer argThree, int argFour, Object argFive) {

  }

}

 

Given that constructor, i would like to create 4 overloads with defaults

public MyClass(Boolean argOne, String argTwo, Integer argThree, int argFour) {

  this(argOne, argTwo, argThree, argFour, null);

}

public MyClass(Boolean argOne, String argTwo, Integer argThree) {

  this(argOne, argTwo, argThree, 0, null);

}

public MyClass(Boolean argOne, String argTwo) {

  this(argOne, argTwo, null, 0, null);

}

public MyClass(Boolean argOne) {

  this(argOne, "", null, 0, null);

}

 

Is something like this possible?

Is there a way to edit this template?

0
Comment actions Permalink

Hello Troy Stauffer

From the given method:

`public MyClass(Boolean argOne, String argTwo, Integer argThree, int argFour, Object argFive) {}`

you may generate the fields by pressing "Alt+Enter" at parameters and selecting "create field for parameter" fix:

and then, by pressing "cmd+N" at class, generate constructors with different sets of parameters:

You may also use the intention "generate overloaded constructor with default parameter values" that is available by "Alt+Enter" when the cursor is at the parameters:

0
Comment actions Permalink

The issue with the generate is that it doesn't provide the "default"

public MyClass(Boolean argOne) {
this(argOne, , , , );
}

this needs to have all of the defaults added
public MyClass(Boolean argOne) {
this(argOne, "", null, 0, null);
}
0
Comment actions Permalink

yes, the default values are not predefined in this case, the generated method will have the placeholders where you can type the needed value, switching to the next placeholder can be done by Tab pressing:

0
Comment actions Permalink

Not exactly what I am after but I understand that this gets it closer.  Thanks for the time

0

Please sign in to leave a comment.