AddField

addField:
  field: "private int x"
  target: "parentClass"

Description

Sensei provides the addField action to add a field to a class.

Configuration options

field

This is simply the code for the field to add. When the action is executed, Sensei does a quick parse to see if a field with the same name already exists. If that is the case, the action will have no effects.

A trailing semicolon does not have to be included in this code.

position

The position option allows configuring the place where the field 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 field"
  actions:
  - addField:
      field: "private int example;"
      position:
        after:
          field:
            name: "logger"
        fallback:
          top: {}

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 2 sets of defaults, based on the detected type of the field to insert.

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

  1. after the last static field

  2. if none, before the first static method

  3. if none, before the first field

  4. if none, before the first method or constructor

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

  1. If the field to insert does not have a static modifier:

  1. after the last non-static field

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

  3. if none, after the last static field

  4. if none, after the last static method

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

target

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