Extracting Existing Logic and Creating a New Method

When editing a JADE method, you can select logic within a method and create a new method in the currently selected class with that logic.

To extract existing logic and create a new method

  1. In the editor pane, select the logic to be extracted.

  2. Right-click and then select the Extract Method command from the Refactor submenu of the Edit menu. If the start of the selection includes a whole expression, the Refactor Extract Method for Class class-name dialog is then displayed. If it does not include a whole expression, an error message is displayed.

    The Generated Method editor pane contains the source of the new method, consisting of the parameter signature determined by the analysis of the selected logic, the method template expansion if you have a template defined in your text templates preferences, and the selected logic, as shown in the following image.

    The status line initially displays:

    Replace newMethod with your method Name in the method source
  3. The Enter and Esc keys initially control the dialog cancel and commit functions.

    If you change the signature of the method, the text of the call that will replace the original selected text in the source method is not changed. You should also change this text in the editor pane of the Class Browser to meet your requirements.

    If the text prior to the original text that you selected is a replacement statement and you selected one statement only, the return type of the method is set to the replacement type; for example, if the original method has the statement int := (a * b + c); and you select (a * b + c), the result is a method return type of Integer.

  4. In the Schema combo box, select the schema in which the method is to be created. This combo box, in which the current schema is selected by default, lists all of the schemas in which the method could be created.

  5. If you want to ensure that self. prefixes the method call, check the Include ‘self.’ in method call check box. If self. exists in the method call, uncheck this check box to remove self. from the method call (the check box is checked if self. exists in the method call).

    The value of the Include ‘self.’ in method call check box is set automatically when the method name in the generated method is changed, based on whether self. prefixes the method call text.

  6. Click the Add Method button, to add the new method and to replace the selected source with a call to the new method. Alternatively, click the Cancel button to cancel the current action and return focus to the original editor pane in the Class Browser.

If the calling method text is changed to self.method-name, the change is accepted, provided that the method-name in the call matches the generated method name when you click the Add Method button.

When you click the Add Method button, the new method is added to the currently selected class in the selected schema, using the method name specified in the Generated Method edit pane, and the selected content of the original source method is replaced with the contents of the Generated Method Call editor pane. This last change is not committed, so you can undo it by using the Ctrl+Z shortcut keys.

If the new method compiles successfully, the Refactor Extract Method for Class class-name dialog is closed and focus returns to the Class Browser editor pane of the original method.

If the new method fails to compile:

The following is an example of a code fragment selected in the original source (Generated Method) editor pane.

d := (a * b + c);

The following example is the generated method in which the whole line is selected (where doCalc has been specified as the method name).

doCalc(d: Integer io; a: Integer; b: Integer; c: Integer);
vars
begin
    d := (a * b + c);
end;

The original statement then becomes:

doCalc(d, a, b, c);

If (a * b + c) were selected, the method would be:

doCalc(a: Integer; b: Integer; c: Integer): Integer;
vars
begin
    return (a * b + c);
end;