Java Reference
In-Depth Information
Changing the return type seems easy, but
IDEA
can't handle many changes of
return type. You must fix many of them yourself. For instance, if you change a
method's return type from a
boolean
to an
int
, there is no logical mapping
between the two. As you make changes to your method, you can see a preview of
the new method signature in the dialog. This gives you a good idea of what you're
doing to the method and what the method will look like when you're done. For-
tunately, this situation doesn't arise too often. If you're making such a drastic
change, you're probably better off writing a new method from scratch.
As we alluded to earlier, you can easily add, remove, and reorder the method's
parameters. Use the
Move Up
and
Move Down
buttons to reorganize them as
you prefer. You can also change the name of the parameters by editing the
Name
entry in the
Parameters
table. If you add a new parameter, you need to specify a
default value so that existing method calls have something to place in the calls.
This can be a literal value such as an
int
or a
String
, or the name of a variable ref-
erence. Whatever you type here is placed verbatim in the existing method calls to
fill the new parameter requirement. If it doesn't resolve to something in the
caller's scope, you must go back and fix it; the same is true with setting or chang-
ing types. But be careful! It's possible to create invalid references. With the
Intro-
duce Parameter
refactoring, this isn't the case.
When you're changing a method signature, the refactoring can now propagate
parameter additions through the method call hierarchy. Assume that you decide
to add a new parameter to a method. Previously you could affect only direct
method calls. Now, if the method is called from some other method, the parame-
ter can be added automatically to that containing method, then to the methods
that contain that containing method, and so on. It's a kind of recursive parameter
addition. The same feature is available for method exceptions, as well. To enable
propagation, click the appropriate
Propagate Parameters
or
Propagate Excep-
tions
button in the
Change Signature
dialog.
Another new feature of this refactoring is the ability to modify the exceptions
thrown by the method. You can add and remove exceptions on the signature as
part of the process.
Introducing a new parameter to a method
The
Introduce Parameter
refactoring takes a selected expression inside a
method and converts it to a parameter. For example, suppose you have a method