All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.gradle.api.internal.DefaultPolymorphicDomainObjectContainer Maven / Gradle / Ivy

There is a newer version: 8.6
Show newest version
/*
 * 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.*;
import org.gradle.internal.Cast;
import org.gradle.internal.reflect.Instantiator;
import org.gradle.model.internal.core.NamedEntityInstantiator;
import org.gradle.util.DeprecationLogger;

import java.util.Set;

public class DefaultPolymorphicDomainObjectContainer extends AbstractPolymorphicDomainObjectContainer
        implements ExtensiblePolymorphicDomainObjectContainer {
    protected final DefaultPolymorphicNamedEntityInstantiator namedEntityInstantiator;

    public DefaultPolymorphicDomainObjectContainer(Class type, Instantiator instantiator, Namer namer, CollectionCallbackActionDecorator callbackDecorator) {
        super(type, instantiator, namer, callbackDecorator);
        namedEntityInstantiator = new DefaultPolymorphicNamedEntityInstantiator(type, "this container");
    }

    /**
     * This internal constructor is used by the 'nebula.lint' plugin which we test as part of our ci pipeline.
     * */
    @Deprecated
    public DefaultPolymorphicDomainObjectContainer(Class type, Instantiator instantiator) {
        this(type, instantiator, Named.Namer.forType(type), CollectionCallbackActionDecorator.NOOP);
        DeprecationLogger.nagUserOfDeprecated("Internal API constructor DefaultPolymorphicDomainObjectContainer(Class, Instantiator)");
    }

    public DefaultPolymorphicDomainObjectContainer(Class type, Instantiator instantiator, CollectionCallbackActionDecorator callbackDecorator) {
        this(type, instantiator, Named.Namer.forType(type), callbackDecorator);
    }

    @Override
    public NamedEntityInstantiator getEntityInstantiator() {
        return namedEntityInstantiator;
    }

    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;
            }
        }
    }

    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);
    }

    public  void registerFactory(Class type, NamedDomainObjectFactory factory) {
        namedEntityInstantiator.registerFactory(type, factory);
    }

    public  void registerFactory(Class type, final Closure factory) {
        registerFactory(type, new NamedDomainObjectFactory() {
            public U create(String name) {
                return factory.call(name);
            }
        });
    }

    public  void registerBinding(Class type, final Class implementationType) {
        registerFactory(type, new NamedDomainObjectFactory() {
            boolean named = Named.class.isAssignableFrom(implementationType);
            public U create(String name) {
                return named ? getInstantiator().newInstance(implementationType, name)
                        : getInstantiator().newInstance(implementationType);
            }
        });
    }

    public Set> getCreateableTypes() {
        return namedEntityInstantiator.getCreatableTypes();
    }
}