org.gradle.api.internal.DefaultPolymorphicDomainObjectContainer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-api Show documentation
Show all versions of gradle-api Show documentation
Gradle 6.9.1 API redistribution.
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.api.internal;
import groovy.lang.Closure;
import org.gradle.api.ExtensiblePolymorphicDomainObjectContainer;
import org.gradle.api.InvalidUserDataException;
import org.gradle.api.Named;
import org.gradle.api.NamedDomainObjectFactory;
import org.gradle.api.Namer;
import org.gradle.internal.Cast;
import org.gradle.internal.reflect.Instantiator;
import org.gradle.model.internal.core.NamedEntityInstantiator;
import java.util.Set;
public class DefaultPolymorphicDomainObjectContainer extends AbstractPolymorphicDomainObjectContainer
implements ExtensiblePolymorphicDomainObjectContainer {
protected final DefaultPolymorphicNamedEntityInstantiator namedEntityInstantiator;
private final Instantiator elementInstantiator;
// NOTE: This constructor exists for backwards compatibility
public DefaultPolymorphicDomainObjectContainer(Class type, Instantiator instantiator, Namer super T> namer, CollectionCallbackActionDecorator callbackDecorator) {
this(type, instantiator, instantiator, namer, callbackDecorator);
}
// NOTE: This constructor exists for backwards compatibility
public DefaultPolymorphicDomainObjectContainer(Class type, Instantiator instantiator, CollectionCallbackActionDecorator callbackDecorator) {
this(type, instantiator, instantiator, Named.Namer.forType(type), callbackDecorator);
}
public DefaultPolymorphicDomainObjectContainer(Class type, Instantiator instantiator, Instantiator elementInstantiator, CollectionCallbackActionDecorator callbackDecorator) {
this(type, instantiator, elementInstantiator, Named.Namer.forType(type), callbackDecorator);
}
private DefaultPolymorphicDomainObjectContainer(Class type, Instantiator instantiator, Instantiator elementInstantiator, Namer super T> namer, CollectionCallbackActionDecorator callbackDecorator) {
super(type, instantiator, namer, callbackDecorator);
this.namedEntityInstantiator = new DefaultPolymorphicNamedEntityInstantiator(type, "this container");
this.elementInstantiator = elementInstantiator;
}
@Override
public NamedEntityInstantiator getEntityInstantiator() {
return namedEntityInstantiator;
}
@Override
protected T doCreate(String name) {
try {
return namedEntityInstantiator.create(name, getType());
} catch (InvalidUserDataException e) {
if (e.getCause() instanceof NoFactoryRegisteredForTypeException) {
throw new InvalidUserDataException(String.format("Cannot create a %s named '%s' because this container "
+ "does not support creating elements by name alone. Please specify which subtype of %s to create. "
+ "Known subtypes are: %s", getTypeDisplayName(), name, getTypeDisplayName(), namedEntityInstantiator.getSupportedTypeNames()));
} else {
throw e;
}
}
}
@Override
protected U doCreate(String name, Class type) {
return namedEntityInstantiator.create(name, type);
}
public void registerDefaultFactory(NamedDomainObjectFactory factory) {
Class castType = Cast.uncheckedCast(getType());
registerFactory(castType, factory);
}
@Override
public void registerFactory(Class type, NamedDomainObjectFactory extends U> factory) {
namedEntityInstantiator.registerFactory(type, factory);
}
@Override
public void registerFactory(Class type, final Closure extends U> factory) {
registerFactory(type, new NamedDomainObjectFactory() {
@Override
public U create(String name) {
return factory.call(name);
}
});
}
@Override
public void registerBinding(Class type, final Class extends U> implementationType) {
registerFactory(type, new NamedDomainObjectFactory() {
boolean named = Named.class.isAssignableFrom(implementationType);
@Override
public U create(String name) {
return named ? elementInstantiator.newInstance(implementationType, name)
: elementInstantiator.newInstance(implementationType);
}
});
}
@Override
public Set extends Class extends T>> getCreateableTypes() {
return namedEntityInstantiator.getCreatableTypes();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy