use of org.gradle.api.internal.plugins.DslObject in project gradle by gradle.
the class BridgedCollections method registration.
public static <I, C extends NamedDomainObjectCollection<I>> ModelRegistrations.Builder registration(final ModelReference<C> containerReference, final Transformer<? extends C, ? super MutableModelNode> containerFactory, final Namer<? super I> namer, String descriptor, final Transformer<String, String> itemDescriptorGenerator) {
final ModelPath containerPath = containerReference.getPath();
final ModelType<C> containerType = containerReference.getType();
assert containerPath != null : "container reference path cannot be null";
return ModelRegistrations.of(containerPath).action(ModelActionRole.Create, new Action<MutableModelNode>() {
public void execute(final MutableModelNode containerNode) {
final C container = containerFactory.transform(containerNode);
containerNode.setPrivateData(containerType, container);
container.all(new Action<I>() {
public void execute(final I item) {
final String name = namer.determineName(item);
// For now, ignore elements added after the container has been closed
if (!containerNode.isMutable()) {
LOGGER.debug("Ignoring element '{}' added to '{}' after it is closed.", containerPath, name);
return;
}
if (!containerNode.hasLink(name)) {
ModelRegistration itemRegistration = ModelRegistrations.unmanagedInstanceOf(ModelReference.of(containerPath.child(name), new DslObject(item).getDeclaredType()), new ExtractFromParentContainer<I, C>(name, containerType)).descriptor(new SimpleModelRuleDescriptor(new Factory<String>() {
@Override
public String create() {
return itemDescriptorGenerator.transform(name);
}
})).build();
containerNode.addLink(itemRegistration);
}
}
});
container.whenObjectRemoved(new Action<I>() {
public void execute(I item) {
String name = namer.determineName(item);
containerNode.removeLink(name);
}
});
}
}).descriptor(descriptor);
}
use of org.gradle.api.internal.plugins.DslObject in project gradle by gradle.
the class AsmBackedClassGeneratorTest method generatesDslObjectCompatibleObject.
@Test
public void generatesDslObjectCompatibleObject() throws Exception {
DslObject dslObject = new DslObject(generator.generate(Bean.class).newInstance());
assertEquals(Bean.class, dslObject.getDeclaredType());
assertNotNull(dslObject.getConventionMapping());
assertNotNull(dslObject.getConvention());
assertNotNull(dslObject.getExtensions());
assertNotNull(dslObject.getAsDynamicObject());
}
use of org.gradle.api.internal.plugins.DslObject in project gradle by gradle.
the class AsmBackedClassGeneratorTest method canAddDynamicPropertiesAndMethodsToGroovyObject.
@Test
public void canAddDynamicPropertiesAndMethodsToGroovyObject() throws Exception {
TestDecoratedGroovyBean bean = generator.generate(TestDecoratedGroovyBean.class).newInstance();
DynamicObjectAware dynamicObjectAware = (DynamicObjectAware) bean;
ConventionObject conventionObject = new ConventionObject();
new DslObject(dynamicObjectAware).getConvention().getPlugins().put("plugin", conventionObject);
call("{ it.conventionProperty = 'value' }", bean);
assertThat(conventionObject.getConventionProperty(), equalTo("value"));
assertThat(call("{ it.hasProperty('conventionProperty') }", bean), notNullValue());
assertThat(call("{ it.conventionProperty }", bean), equalTo((Object) "value"));
assertThat(call("{ it.conventionMethod('value') }", bean), equalTo((Object) "[value]"));
assertThat(call("{ it.invokeMethod('conventionMethod', 'value') }", bean), equalTo((Object) "[value]"));
}
use of org.gradle.api.internal.plugins.DslObject in project gradle by gradle.
the class GroovyBasePlugin method addSourceSetExtension.
private void addSourceSetExtension(org.gradle.api.tasks.SourceSet sourceSet, DefaultGroovySourceSet groovySourceSet) {
new DslObject(sourceSet).getConvention().getPlugins().put("groovy", groovySourceSet);
sourceSet.getExtensions().add(GroovySourceDirectorySet.class, "groovy", groovySourceSet.getGroovy());
}
use of org.gradle.api.internal.plugins.DslObject in project gradle by gradle.
the class AsmBackedClassGeneratorTest method canAddDynamicPropertiesAndMethodsToGroovyObject.
@Test
public void canAddDynamicPropertiesAndMethodsToGroovyObject() throws Exception {
TestDecoratedGroovyBean bean = newInstance(TestDecoratedGroovyBean.class);
DynamicObjectAware dynamicObjectAware = (DynamicObjectAware) bean;
ConventionObject conventionObject = new ConventionObject();
new DslObject(dynamicObjectAware).getConvention().getPlugins().put("plugin", conventionObject);
call("{ it.conventionProperty = 'value' }", bean);
assertThat(conventionObject.getConventionProperty(), equalTo("value"));
assertThat(call("{ it.hasProperty('conventionProperty') }", bean), notNullValue());
assertThat(call("{ it.conventionProperty }", bean), equalTo((Object) "value"));
assertThat(call("{ it.conventionMethod('value') }", bean), equalTo((Object) "[value]"));
assertThat(call("{ it.invokeMethod('conventionMethod', 'value') }", bean), equalTo((Object) "[value]"));
}
Aggregations