With Gradle 7.2, I’ve been trying to create a custom plugin Extension with an ExtensiblePolymorphicDomainObjectContainer member to do most of the heavy lifting of my DSL, but cannot figure out how to trigger an action after the execution of the configuration closure for a named type object added to the container.
Take for example the following DSL:
myCustomExtension {
"container" (SubType) { //configure this SubType object
baseTypeProperty = "xyz" //override the default value of this property
subTypeProperty = "xyz" //override the default value of this property
}
}
class MyCustomExtension {
ExtensiblePolymorphicDomainObjectContainer container
MyCustomExtension(final Project project){
container = project.objects.polymorphicDomainObjectContainer(BaseType)
container.registerFactory(SubType, { name ->
return project.objects.newInstance(SubType, name)
})
}
}
class BaseType implements Named {
final String name
String baseTypeProperty
BaseType(final String name){
this.@name = name
baseTypeProperty = "abc"
}
}
class SubType extends BaseType {
String subTypeProperty
SubType(final String name){
super(name)
subTypeProperty = "abc"
}
}
If I execute the following action on the container for when new objects are added, it appears that the action is invoked by the container just after the object is created by the factory and added to the inner collection, but before configure it run on it. What I’m left with is the instantiated object in the Action callback w/o it being configured. How can I “listen” and defer further actions to when the BaseType object has been fully configured?
myExtension.container.all( (BaseType baseType) -> {
String propertyValue = baseType.baseTypeProperty
//it's "abc" here, not the overriden value "xyz" as configured in the closure
})
For reference from AbstractPolymorphicDomainObjectContainer where configure is invoked after the object is added to the collection.
public <U extends T> U create(String name, Class<U> type, Action<? super U> configuration) {
this.assertMutable("create(String, Class, Action)");
this.assertCanAdd(name);
U object = this.doCreate(name, type);
this.add(object);
if (configuration != null) {
configuration.execute(object);
}
return object;
}