org.gradle.api.internal.plugins.DefaultConvention 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 2009 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.plugins;
import groovy.lang.Closure;
import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.plugins.Convention;
import org.gradle.api.plugins.ExtraPropertiesExtension;
import org.gradle.api.reflect.HasPublicType;
import org.gradle.api.reflect.TypeOf;
import org.gradle.internal.metaobject.AbstractDynamicObject;
import org.gradle.internal.metaobject.BeanDynamicObject;
import org.gradle.internal.metaobject.DynamicObject;
import org.gradle.internal.metaobject.GetPropertyResult;
import org.gradle.internal.metaobject.InvokeMethodResult;
import org.gradle.internal.metaobject.SetPropertyResult;
import org.gradle.internal.reflect.Instantiator;
import org.gradle.util.ConfigureUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static java.lang.String.format;
import static org.gradle.api.reflect.TypeOf.typeOf;
import static org.gradle.internal.Cast.uncheckedCast;
public class DefaultConvention implements Convention, ExtensionContainerInternal {
private final Map plugins = new LinkedHashMap();
private final DefaultConvention.ExtensionsDynamicObject extensionsDynamicObject = new ExtensionsDynamicObject();
private final ExtensionsStorage extensionsStorage = new ExtensionsStorage();
private final ExtraPropertiesExtension extraProperties = new DefaultExtraPropertiesExtension();
private final Instantiator instantiator;
/**
* This method should not be used in runtime code proper as means that the convention cannot create
* dynamic extensions.
*
* It's here for backwards compatibility with our tests and for convenience.
*
* @see #DefaultConvention(org.gradle.internal.reflect.Instantiator)
*/
public DefaultConvention() {
this(null);
}
public DefaultConvention(Instantiator instantiator) {
this.instantiator = instantiator;
add(ExtraPropertiesExtension.class, ExtraPropertiesExtension.EXTENSION_NAME, extraProperties);
}
@Override
public Map getPlugins() {
return plugins;
}
@Override
public DynamicObject getExtensionsAsDynamicObject() {
return extensionsDynamicObject;
}
private Instantiator getInstantiator() {
if (instantiator == null) {
throw new GradleException("request for DefaultConvention.instantiator when the object was constructed without a convention");
}
return instantiator;
}
@Override
public T getPlugin(Class type) {
T value = findPlugin(type);
if (value == null) {
throw new IllegalStateException(
format("Could not find any convention object of type %s.", type.getSimpleName()));
}
return value;
}
@Override
public T findPlugin(Class type) throws IllegalStateException {
List values = new ArrayList();
for (Object object : plugins.values()) {
if (type.isInstance(object)) {
values.add(type.cast(object));
}
}
if (values.isEmpty()) {
return null;
}
if (values.size() > 1) {
throw new IllegalStateException(
format("Found multiple convention objects of type %s.", type.getSimpleName()));
}
return values.get(0);
}
@Override
public void add(String name, Object extension) {
if (extension instanceof Class) {
create(name, (Class>) extension);
} else {
addWithDefaultPublicType(extension.getClass(), name, extension);
}
}
@Override
public void add(Class publicType, String name, T extension) {
add(typeOf(publicType), name, extension);
}
@Override
public void add(TypeOf publicType, String name, T extension) {
extensionsStorage.add(publicType, name, extension);
}
@Override
public T create(String name, Class instanceType, Object... constructionArguments) {
T instance = instantiate(instanceType, constructionArguments);
addWithDefaultPublicType(instanceType, name, instance);
return instance;
}
@Override
public T create(Class publicType, String name, Class extends T> instanceType, Object... constructionArguments) {
return create(typeOf(publicType), name, instanceType, constructionArguments);
}
@Override
public T create(TypeOf publicType, String name, Class extends T> instanceType, Object... constructionArguments) {
T instance = instantiate(instanceType, constructionArguments);
add(publicType, name, instance);
return instance;
}
@Override
public ExtraPropertiesExtension getExtraProperties() {
return extraProperties;
}
@Override
public Map> getSchema() {
return extensionsStorage.getSchema();
}
@Override
public T getByType(Class type) {
return getByType(typeOf(type));
}
@Override
public T getByType(TypeOf type) {
return extensionsStorage.getByType(type);
}
@Override
public T findByType(Class type) {
return findByType(typeOf(type));
}
@Override
public T findByType(TypeOf type) {
return extensionsStorage.findByType(type);
}
@Override
public Object getByName(String name) {
return extensionsStorage.getByName(name);
}
@Override
public Object findByName(String name) {
return extensionsStorage.findByName(name);
}
@Override
public void configure(Class type, Action super T> action) {
configure(typeOf(type), action);
}
@Override
public void configure(TypeOf type, Action super T> action) {
extensionsStorage.configureExtension(type, action);
}
@Override
public Map getAsMap() {
return extensionsStorage.getAsMap();
}
public Object propertyMissing(String name) {
return getByName(name);
}
public void propertyMissing(String name, Object value) {
checkExtensionIsNotReassigned(name);
add(name, value);
}
private void addWithDefaultPublicType(Class> defaultType, String name, Object extension) {
add(preferredPublicTypeOf(extension, defaultType), name, extension);
}
private TypeOf
© 2015 - 2025 Weber Informatics LLC | Privacy Policy