This message was deleted.
# community-support
s
This message was deleted.
p
I have a plugin
FooPlugin
creating an extension:
Copy code
interface FooExtension {
  val foos: NamedDomainObjectContainer<Foo>
}
abstract class Foo(private val name: String): Named {
  override fun getName() = name
}
class FooPlugin : Plugin<Project> {
  override fun apply(project: Project) {
    val fooExt = project.extensions.create("foo", FooExtension::class.java)
    fooExt.foos.register("testFoo")
  }
}

// build.gradle.kts
foo.foos.testFoo // Unresolved reference.
e
Copy code
interface FooExtension : NamedDomainObjectContainer<Foo>

class FooPlugin : Plugin<Project> {
    override fun apply(project: Project) {
        project.extensions.create<FooExtension>("foo", ...)
p
Ohhhh, let me try this
e
your original might work if https://github.com/gradle/gradle/issues/9264 is resolved (although I think you will still need a unique type for the container), but it won't currently
p
Thanks for the link! But now I am unable to create an instance of the extension
Copy code
Could not create an instance of type FooExtension.
      > Could not generate a decorated class for type FooExtension.
         > Cannot have abstract method NamedDomainObjectCollection.getAsMap().
e
you might have to do it the long-winded way,
Copy code
internal class DefaultFooExtension @Inject constructor(
    private val objects: ObjectFactory,
) : AbstractNamedDomainObjectContainer(), FooExtension {
    override fun doCreate(name: String): Foo = objects.newInstance(name)
}

val fooExtension = project.objects.newInstance<DefaultFooExtension>()
project.extensions.add<FooExtension>("foo", fooExtension)
hmm AbstractNamedDomainObjectContainer is internal. I'm not entirely sure what the correct public way to do this is
maybe you could
Copy code
project.extensions.add("foo", project.objects.domainObjectContainer<Foo>())
but you wouldn't be able to add your own functions to it
p
Yeah, that works, nice. As a workaround, I add two extensions, one typed holder containing the elements and another for my functions. Thank you!