Multi-level DSL for plugin extension?

There is nothing wrong with such a nested extension and @Chris_Dore’s recommendations were spot-on.

There’s a slight difference, because most plugins are self-contained and don’t add extension to other plugins like you wanted.

When you just want to have a nested object inside an extension, you can have a field and methods like the following:

class Outer {
  Inner inner = new Inner()

  //for property access syntax
  Inner getInner() {
    return inner;
  }

  // for curly-brace syntax
  void inner(Closure configuration) {
    ConfigureUtil.configure(inner, configuration)
  }

  //same as above, but better for Java/Kotlin clients
  void inner(Action<? super Inner> configuration) {
    configuration.execute(inner)
  }
}

The benefit being a more discoverable and type-safe API.

2 Likes