AddMethod

addMethod:
  method: "void setField();"
  target: "parentClass"

Description

Sensei provides the addMethod action to add a method to a class.

Configuration options

method

This is the code for the method to add.

position

The position option allows configuring the place where the method will be inserted. This can be top for at the top of the class, or before or after to place it before or after an element that matches a given specification, respectively.

When using before or after, the fallback property can be supplied to configure a fallback behavior that will be used when the given specification does not match. Each fallback can have another fallback.

availableFixes:
- name: "add method"
  actions:
  - addMethod:
      method: "void setField();"
      position:
        after:
          method:
            name:
              matches: "set.*"
        fallback:
          after:
            field: {}
          fallback:
            top: {}
      target: "parentClass"

When the position option is not specified, or none of the given positions and fallbacks are available, the most appropriate location will automatically be determined by sets of predefined default positions as listed below.

Defaults

There are 3 sets of defaults, based on the detected type of the method to insert.

  1. If the method to insert has a static modifier:

  1. after the last static method

  2. if none, before the first field

  3. if none, before the first constructor or non-static method

  4. if none, after the opening brace of the class

  1. If the method to insert is a constructor:

  1. after the last constructor

  2. if none, before the first non-static method

  3. if none, after the last field

  4. if none, after the last static method

  5. if none, after the opening brace of the class

  1. If the method to insert is not a constructor and not static:

  1. after the last non-static method or constructor

  2. if none, after the last field

  3. if none, after the last static method

  4. if none, after the opening brace of the class

target

Methods can only be added to classes, so the target will always be either parentClass or, when the marked element is a class, self.