Unexpected behavior using a custom extension with a NamedDomainObjectContainer


(Garrett L. Ward) #1

I’ve created a plugin that has a set of nested extensions, using a NamedDomainObjectContainer on the parent extension to hold the child extensions, like so:

class TestExtension {
  final NamedDomainObjectContainer<NestedExtension> data
  public TestExtension(Project project) {
    data = project.container(NestedExtension)

  public data(Closure config) {

Where “NestedExtension” is just:

class NestedExtension {
  final String name
  String data

However, using a String literal as the name without a configuration closure does not allow the object to be created. Using the following example, only “foo” and “foo bar baz” are created.

test {
    data {
        "bar baz"
        "foo bar baz" {}

I’ve tested this in 2.14.1 and 3.3 and it presents in both places. I’m not sure if this is a bug or just a misunderstanding on my part of how the named objects are created when a NamedDomainObjectContainer is configured–figured I’d ask here first to see if there’s something silly I’m doing.

(Mark Vieira) #2

I think this is because Groovy is just treating “bar” like a literal expression. That is, there is no context here to indicate that you are attempting to reference an existing symbol or call a method. Doing “bar”() should give you the behavior you want.