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

org.gradle.model.internal.inspect.UnmanagedModelCreationRuleExtractor Maven / Gradle / Ivy

There is a newer version: 8.11.1
Show newest version
/*
 * Copyright 2015 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.model.internal.inspect;

import org.gradle.internal.Cast;
import org.gradle.model.internal.core.*;
import org.gradle.model.internal.core.rule.describe.ModelRuleDescriptor;
import org.gradle.model.internal.type.ModelType;

import java.util.List;

public class UnmanagedModelCreationRuleExtractor extends AbstractModelCreationRuleExtractor {

    @Override
    public boolean isSatisfiedBy(MethodRuleDefinition element) {
        return super.isSatisfiedBy(element) && !isVoidMethod(element);
    }

    @Override
    protected  ExtractedModelRule buildRule(ModelPath modelPath, MethodRuleDefinition ruleDefinition) {
        return new ExtractedUnmanagedCreationRule(modelPath, ruleDefinition);
    }

    @Override
    public String getDescription() {
        return String.format("%s and returning a model element", super.getDescription());
    }

    private static class UnmanagedElementCreationAction implements MethodRuleAction {
        private final ModelRuleDescriptor descriptor;
        private final ModelReference subject;
        private final ModelType type;
        private final List> inputs;

        private UnmanagedElementCreationAction(ModelRuleDescriptor descriptor, ModelReference subject, List> inputs, ModelType type) {
            this.subject = subject;
            this.inputs = inputs;
            this.descriptor = descriptor;
            this.type = type;
        }

        @Override
        public ModelReference getSubject() {
            return subject;
        }

        @Override
        public List> getInputs() {
            return inputs;
        }

        @Override
        public void execute(ModelRuleInvoker ruleInvoker, MutableModelNode modelNode, List> inputs) {
            Object instance;
            if (inputs.size() == 0) {
                instance = ruleInvoker.invoke();
            } else {
                Object[] args = new Object[inputs.size()];
                for (int i = 0; i < inputs.size(); i++) {
                    args[i] = inputs.get(i).getInstance();
                }

                instance = ruleInvoker.invoke(args);
            }
            if (instance == null) {
                throw new ModelRuleExecutionException(descriptor, "rule returned null");
            }
            T value = Cast.uncheckedCast(instance);
            modelNode.setPrivateData(type, value);
        }
    }

    private static class ExtractedUnmanagedCreationRule extends ExtractedCreationRule {
        public ExtractedUnmanagedCreationRule(ModelPath modelPath, MethodRuleDefinition ruleDefinition) {
            super(modelPath, ruleDefinition);
        }

        @Override
        protected void buildRegistration(MethodModelRuleApplicationContext context, ModelRegistrations.Builder registration) {
            MethodRuleDefinition ruleDefinition = getRuleDefinition();
            ModelType modelType = ruleDefinition.getReturnType();
            List> inputs = ruleDefinition.getReferences();
            ModelRuleDescriptor descriptor = ruleDefinition.getDescriptor();
            ModelReference subjectReference = ModelReference.of(modelPath);
            registration.action(ModelActionRole.Create,
                    context.contextualize(new UnmanagedElementCreationAction(descriptor, subjectReference, inputs, modelType)));
            registration.withProjection(new UnmanagedModelProjection(modelType));
            registration.withProjection(new ModelElementProjection(modelType));
        }
    }
}