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

org.gradle.api.internal.plugins.DefaultPluginContainer Maven / Gradle / Ivy

There is a newer version: 8.6
Show newest version
/*
 * Copyright 2010 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 com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import org.gradle.api.Action;
import org.gradle.api.Plugin;
import org.gradle.api.internal.CollectionCallbackActionDecorator;
import org.gradle.api.internal.GeneratedSubclasses;
import org.gradle.api.plugins.PluginCollection;
import org.gradle.api.plugins.PluginContainer;
import org.gradle.api.plugins.UnknownPluginException;
import org.gradle.api.specs.Spec;
import org.gradle.internal.Cast;
import org.gradle.plugin.use.internal.DefaultPluginId;

/**
 * This plugin collection is optimized based on the knowledge we have about how plugins
 * are applied. The plugin manager already keeps track of all plugins and ensures they
 * are only applied once. As a result, we don't need to keep another data structure here,
 * but can just share the one kept by the manager. This class forbids all mutations, as
 * manually adding/removing plugin instances does not make sense.
 */
public class DefaultPluginContainer extends DefaultPluginCollection implements PluginContainer {

    private final PluginRegistry pluginRegistry;
    private final PluginManagerInternal pluginManager;

    public DefaultPluginContainer(PluginRegistry pluginRegistry, final PluginManagerInternal pluginManager, CollectionCallbackActionDecorator callbackActionDecorator) {
        super(Plugin.class, callbackActionDecorator);
        this.pluginRegistry = pluginRegistry;
        this.pluginManager = pluginManager;
    }

    void pluginAdded(Plugin plugin) {
        super.add(plugin);
    }

    @Override
    @Deprecated
    public boolean add(Plugin toAdd) {
        throw new UnsupportedOperationException();
    }

    @Override
    @SuppressWarnings("rawtypes")
    public Plugin apply(String id) {
        PluginImplementation plugin = pluginRegistry.lookup(DefaultPluginId.unvalidated(id));
        if (plugin == null) {
            throw new UnknownPluginException("Plugin with id '" + id + "' not found.");
        }

        if (!Plugin.class.isAssignableFrom(plugin.asClass())) {
            throw new IllegalArgumentException("Plugin implementation '" + plugin.asClass().getName() + "' does not implement the Plugin interface. This plugin cannot be applied directly via the PluginContainer.");
        } else {
            return pluginManager.addImperativePlugin(Cast.>>uncheckedNonnullCast(plugin));
        }
    }

    @Override
    @SuppressWarnings("rawtypes")
    public 

P apply(Class

type) { return pluginManager.addImperativePlugin(type); } @Override public boolean hasPlugin(String id) { return findPlugin(id) != null; } @Override public boolean hasPlugin(Class type) { return findPlugin(type) != null; } private Plugin doFindPlugin(String id) { for (final PluginManagerInternal.PluginWithId pluginWithId : pluginManager.pluginsForId(id)) { Plugin plugin = Iterables.tryFind(DefaultPluginContainer.this, new Predicate() { @Override public boolean apply(Plugin plugin) { Class pluginType = GeneratedSubclasses.unpackType(plugin); return pluginWithId.clazz.equals(pluginType); } }).orNull(); if (plugin != null) { return plugin; } } return null; } @Override public Plugin findPlugin(String id) { return doFindPlugin(id); } @Override public

P findPlugin(Class

type) { for (Plugin plugin : this) { Class pluginType = GeneratedSubclasses.unpackType(plugin); if (pluginType.equals(type)) { return type.cast(plugin); } } return null; } @Override public Plugin getPlugin(String id) { Plugin plugin = findPlugin(id); if (plugin == null) { throw new UnknownPluginException("Plugin with id " + id + " has not been used."); } return plugin; } @Override public Plugin getAt(String id) throws UnknownPluginException { return getPlugin(id); } @Override public

P getAt(Class

type) throws UnknownPluginException { return getPlugin(type); } @Override public

P getPlugin(Class

type) throws UnknownPluginException { P plugin = findPlugin(type); if (plugin == null) { throw new UnknownPluginException("Plugin with type " + type + " has not been used."); } return type.cast(plugin); } @Override public void withId(final String pluginId, final Action action) { Action wrappedAction = new Action() { @Override public void execute(final DefaultPluginManager.PluginWithId pluginWithId) { matching(new Spec() { @Override public boolean isSatisfiedBy(Plugin plugin) { Class pluginType = GeneratedSubclasses.unpackType(plugin); return pluginWithId.clazz.equals(pluginType); } }).all(action); } }; pluginManager.pluginsForId(pluginId).all(wrappedAction); } @Override public PluginCollection withType(Class type) { // runtime check because method is used from Groovy where type bounds are not respected if (!Plugin.class.isAssignableFrom(type)) { throw new IllegalArgumentException(String.format("'%s' does not implement the Plugin interface.", type.getName())); } return super.withType(type); } }