Setting property convention with managed properties

In my extensions, I’m using the “managed properties” feature, where my Property getters are all abstract. When using this pattern, how can I set conventions for the abstract properties? Is it safe to call those abstract getters in my constructor, such as:

public abstract class TestExtension {
  public abstract Property<Integer> getFoo();

  public TestExtension() {
    getFoo().convention(2);
  }
}

Is this the canonical way, or should I be setting conventions elsewhere?

1 Like

My opinion is that the conventions should be set by the plugin after creating the extension. I think it works well as a best practice for maximizing the re-usability of the extension class. For example, some time in the future you make another variant of the plugin that uses the same extension class but with different conventions. I also believe it is best to try and contain these types of “business logic” in the plugin class instead of distributing it across the entity classes (extension classes, task classes, etc).
So, I would do the following in the plugin’s apply():

TestExtension te = project.extensions.create("testExt", TestExtension.class);
te.getFoo().convention(2);