Java Code Examples for org.gradle.model.internal.registry.ModelRegistry

The following examples show how to use org.gradle.model.internal.registry.ModelRegistry. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
private <R, S extends BinarySpec> void doRegister(MethodRuleDefinition<R> ruleDefinition, ModelRegistry modelRegistry, RuleSourceDependencies dependencies) {
    try {
        RuleMethodDataCollector dataCollector = new RuleMethodDataCollector();
        visitAndVerifyMethodSignature(dataCollector, ruleDefinition);

        final Class<S> binaryType = dataCollector.getParameterType(BinarySpec.class);
        final Class<? extends ComponentSpec> componentType = dataCollector.getParameterType(ComponentSpec.class);
        dependencies.add(ComponentModelBasePlugin.class);
        final ModelReference<BinaryContainer> subject = ModelReference.of(ModelPath.path("binaries"), new ModelType<BinaryContainer>() {
        });

        configureMutationRule(modelRegistry, subject, componentType, binaryType, ruleDefinition);
    } catch (InvalidComponentModelException e) {
        invalidModelRule(ruleDefinition, e);
    }
}
 
Example #2
private <R, S extends BinarySpec> void doRegister(MethodRuleDefinition<R> ruleDefinition, ModelRegistry modelRegistry, RuleSourceDependencies dependencies) {
    try {
        RuleMethodDataCollector dataCollector = new RuleMethodDataCollector();
        verifyMethodSignature(dataCollector, ruleDefinition);

        Class<S> binaryType =  dataCollector.getParameterType(BinarySpec.class);
        dependencies.add(ComponentModelBasePlugin.class);

        final ModelReference<TaskContainer> tasks = ModelReference.of(ModelPath.path("tasks"), new ModelType<TaskContainer>() {
        });

        modelRegistry.mutate(new BinaryTaskRule<R, S>(tasks, binaryType, ruleDefinition, modelRegistry));

    } catch (InvalidComponentModelException e) {
        invalidModelRule(ruleDefinition, e);
    }
}
 
Example #3
Source Project: pushfish-android   Author: PushFish   File: ModelRuleInspector.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
public <T> void inspect(Class<T> source, ModelRegistry modelRegistry, RuleSourceDependencies dependencies) {
    validate(source);
    final Method[] methods = source.getDeclaredMethods();

    // sort for determinism
    Arrays.sort(methods, new Comparator<Method>() {
        public int compare(Method o1, Method o2) {
            return o1.toString().compareTo(o2.toString());
        }
    });

    for (Method method : methods) {
        validate(method);
        MethodRuleDefinition<?> ruleDefinition = DefaultMethodRuleDefinition.create(source, method);
        MethodRuleDefinitionHandler handler = getMethodHandler(ruleDefinition);
        if (handler != null) {
            // TODO catch “strange” exceptions thrown here and wrap with some context on the rule being registered
            // If the thrown exception doesn't provide any “model rule” context, it will be more or less impossible for a user
            // to work out what happened because the stack trace won't reveal any info about which rule was being registered.
            // However, a “wrap everything” strategy doesn't quite work because the thrown exception may already have enough context
            // and do a better job of explaining what went wrong than what we can do at this level.
            handler.register(ruleDefinition, modelRegistry, dependencies);
        }
    }
}
 
Example #4
public <T> void register(MethodRuleDefinition<T> ruleDefinition, ModelRegistry modelRegistry, RuleSourceDependencies dependencies) {

        // TODO validate model name
        String modelName = determineModelName(ruleDefinition);

        try {
            ModelPath.validatePath(modelName);
        } catch (Exception e) {
            throw new InvalidModelRuleDeclarationException(String.format("Path of declared model element created by rule %s is invalid.", ruleDefinition.getDescriptor()), e);
        }

        // TODO validate the return type (generics?)
        ModelType<T> returnType = ruleDefinition.getReturnType();
        ModelPath path = ModelPath.path(modelName);
        List<ModelReference<?>> references = ruleDefinition.getReferences();
        ModelRuleDescriptor descriptor = ruleDefinition.getDescriptor();

        Transformer<T, Inputs> transformer = new ModelRuleInvokerBackedTransformer<T>(ruleDefinition.getRuleInvoker(), descriptor, references);
        modelRegistry.create(
                ModelCreators.of(ModelReference.of(path, returnType), transformer)
                        .descriptor(descriptor)
                        .inputs(references)
                        .build()
        );
    }
 
Example #5
private <R, S extends BinarySpec> void doRegister(MethodRuleDefinition<R> ruleDefinition, ModelRegistry modelRegistry, RuleSourceDependencies dependencies) {
    try {
        RuleMethodDataCollector dataCollector = new RuleMethodDataCollector();
        visitAndVerifyMethodSignature(dataCollector, ruleDefinition);

        final Class<S> binaryType = dataCollector.getParameterType(BinarySpec.class);
        final Class<? extends ComponentSpec> componentType = dataCollector.getParameterType(ComponentSpec.class);
        dependencies.add(ComponentModelBasePlugin.class);
        final ModelReference<BinaryContainer> subject = ModelReference.of(ModelPath.path("binaries"), new ModelType<BinaryContainer>() {
        });

        configureMutationRule(modelRegistry, subject, componentType, binaryType, ruleDefinition);
    } catch (InvalidComponentModelException e) {
        invalidModelRule(ruleDefinition, e);
    }
}
 
Example #6
private <R, S extends BinarySpec> void doRegister(MethodRuleDefinition<R> ruleDefinition, ModelRegistry modelRegistry, RuleSourceDependencies dependencies) {
    try {
        RuleMethodDataCollector dataCollector = new RuleMethodDataCollector();
        verifyMethodSignature(dataCollector, ruleDefinition);

        Class<S> binaryType =  dataCollector.getParameterType(BinarySpec.class);
        dependencies.add(ComponentModelBasePlugin.class);

        final ModelReference<TaskContainer> tasks = ModelReference.of(ModelPath.path("tasks"), new ModelType<TaskContainer>() {
        });

        modelRegistry.mutate(new BinaryTaskRule<R, S>(tasks, binaryType, ruleDefinition, modelRegistry));

    } catch (InvalidComponentModelException e) {
        invalidModelRule(ruleDefinition, e);
    }
}
 
Example #7
Source Project: Pushjet-Android   Author: Pushjet   File: ModelRuleInspector.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
public <T> void inspect(Class<T> source, ModelRegistry modelRegistry, RuleSourceDependencies dependencies) {
    validate(source);
    final Method[] methods = source.getDeclaredMethods();

    // sort for determinism
    Arrays.sort(methods, new Comparator<Method>() {
        public int compare(Method o1, Method o2) {
            return o1.toString().compareTo(o2.toString());
        }
    });

    for (Method method : methods) {
        validate(method);
        MethodRuleDefinition<?> ruleDefinition = DefaultMethodRuleDefinition.create(source, method);
        MethodRuleDefinitionHandler handler = getMethodHandler(ruleDefinition);
        if (handler != null) {
            // TODO catch “strange” exceptions thrown here and wrap with some context on the rule being registered
            // If the thrown exception doesn't provide any “model rule” context, it will be more or less impossible for a user
            // to work out what happened because the stack trace won't reveal any info about which rule was being registered.
            // However, a “wrap everything” strategy doesn't quite work because the thrown exception may already have enough context
            // and do a better job of explaining what went wrong than what we can do at this level.
            handler.register(ruleDefinition, modelRegistry, dependencies);
        }
    }
}
 
Example #8
public <T> void register(MethodRuleDefinition<T> ruleDefinition, ModelRegistry modelRegistry, RuleSourceDependencies dependencies) {

        // TODO validate model name
        String modelName = determineModelName(ruleDefinition);

        try {
            ModelPath.validatePath(modelName);
        } catch (Exception e) {
            throw new InvalidModelRuleDeclarationException(String.format("Path of declared model element created by rule %s is invalid.", ruleDefinition.getDescriptor()), e);
        }

        // TODO validate the return type (generics?)
        ModelType<T> returnType = ruleDefinition.getReturnType();
        ModelPath path = ModelPath.path(modelName);
        List<ModelReference<?>> references = ruleDefinition.getReferences();
        ModelRuleDescriptor descriptor = ruleDefinition.getDescriptor();

        Transformer<T, Inputs> transformer = new ModelRuleInvokerBackedTransformer<T>(ruleDefinition.getRuleInvoker(), descriptor, references);
        modelRegistry.create(
                ModelCreators.of(ModelReference.of(path, returnType), transformer)
                        .descriptor(descriptor)
                        .inputs(references)
                        .build()
        );
    }
 
Example #9
public <R> void register(MethodRuleDefinition<R> ruleDefinition, ModelRegistry modelRegistry, RuleSourceDependencies dependencies) {
    try {
        ModelType<? extends T> type = readType(ruleDefinition);
        ModelType<? extends U> implementation = determineImplementationType(ruleDefinition, type);

        dependencies.add(ComponentModelBasePlugin.class);
        if (implementation != null) {
            ModelMutator<?> mutator = new RegisterTypeRule<T, U>(type, implementation, ruleDefinition.getDescriptor(), registerer);
            modelRegistry.mutate(mutator);
        }
    } catch (InvalidComponentModelException e) {
        invalidModelRule(ruleDefinition, e);
    }
}
 
Example #10
public <R> void register(MethodRuleDefinition<R> ruleDefinition, ModelRegistry modelRegistry, RuleSourceDependencies dependencies) {
    List<ModelReference<?>> bindings = ruleDefinition.getReferences();

    ModelReference<?> subject = bindings.get(0);
    List<ModelReference<?>> inputs = bindings.subList(1, bindings.size());
    MethodModelMutator<?> mutator = toMutator(ruleDefinition, subject, inputs);

    if (isFinalize()) {
        modelRegistry.finalize(mutator);
    } else {
        modelRegistry.mutate(mutator);
    }
}
 
Example #11
Source Project: pushfish-android   Author: PushFish   File: TransformedModelDslBacking.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
TransformedModelDslBacking(ModelRegistry modelRegistry, Object thisObject, Object owner, Transformer<? extends List<ModelReference<?>>, ? super Closure<?>> inputPathsExtractor,
                           Transformer<SourceLocation, ? super Closure<?>> ruleLocationExtractor) {
    this.modelRegistry = modelRegistry;
    this.thisObject = thisObject;
    this.owner = owner;
    this.inputPathsExtractor = inputPathsExtractor;
    this.ruleLocationExtractor = ruleLocationExtractor;
}
 
Example #12
Source Project: pushfish-android   Author: PushFish   File: PluginModelRuleExtractor.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void execute(PluginApplication pluginApplication) {
    Class<? extends Plugin> pluginClass = pluginApplication.getPlugin().getClass();
    Set<Class<?>> sources = inspector.getDeclaredSources(pluginClass);
    if (!sources.isEmpty()) {
        PluginAware target = pluginApplication.getTarget();
        if (!(target instanceof ModelRegistryScope)) {
            throw new UnsupportedOperationException(String.format("Cannot apply model rules of plugin '%s' as the target '%s' is not model rule aware", pluginClass.getName(), target));
        }

        ModelRegistry modelRegistry = ((ModelRegistryScope) target).getModelRegistry();
        for (Class<?> source : sources) {
            inspector.inspect(source, modelRegistry, new PluginRuleSourceDependencies(target));
        }
    }
}
 
Example #13
Source Project: javaide   Author: tranleduy2000   File: JavaBasePlugin.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Inject
public JavaBasePlugin(Instantiator instantiator, JavaToolChain javaToolChain, ITaskFactory taskFactory, ModelRegistry modelRegistry) {
    this.instantiator = instantiator;
    this.javaToolChain = javaToolChain;
    this.taskFactory = taskFactory;
    this.modelRegistry = modelRegistry;
}
 
Example #14
public <R> void register(MethodRuleDefinition<R> ruleDefinition, ModelRegistry modelRegistry, RuleSourceDependencies dependencies) {
    try {
        ModelType<? extends T> type = readType(ruleDefinition);
        ModelType<? extends U> implementation = determineImplementationType(ruleDefinition, type);

        dependencies.add(ComponentModelBasePlugin.class);
        if (implementation != null) {
            ModelMutator<?> mutator = new RegisterTypeRule<T, U>(type, implementation, ruleDefinition.getDescriptor(), registerer);
            modelRegistry.mutate(mutator);
        }
    } catch (InvalidComponentModelException e) {
        invalidModelRule(ruleDefinition, e);
    }
}
 
Example #15
public <R> void register(MethodRuleDefinition<R> ruleDefinition, ModelRegistry modelRegistry, RuleSourceDependencies dependencies) {
    List<ModelReference<?>> bindings = ruleDefinition.getReferences();

    ModelReference<?> subject = bindings.get(0);
    List<ModelReference<?>> inputs = bindings.subList(1, bindings.size());
    MethodModelMutator<?> mutator = toMutator(ruleDefinition, subject, inputs);

    if (isFinalize()) {
        modelRegistry.finalize(mutator);
    } else {
        modelRegistry.mutate(mutator);
    }
}
 
Example #16
Source Project: Pushjet-Android   Author: Pushjet   File: TransformedModelDslBacking.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
TransformedModelDslBacking(ModelRegistry modelRegistry, Object thisObject, Object owner, Transformer<? extends List<ModelReference<?>>, ? super Closure<?>> inputPathsExtractor,
                           Transformer<SourceLocation, ? super Closure<?>> ruleLocationExtractor) {
    this.modelRegistry = modelRegistry;
    this.thisObject = thisObject;
    this.owner = owner;
    this.inputPathsExtractor = inputPathsExtractor;
    this.ruleLocationExtractor = ruleLocationExtractor;
}
 
Example #17
Source Project: Pushjet-Android   Author: Pushjet   File: PluginModelRuleExtractor.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void execute(PluginApplication pluginApplication) {
    Class<? extends Plugin> pluginClass = pluginApplication.getPlugin().getClass();
    Set<Class<?>> sources = inspector.getDeclaredSources(pluginClass);
    if (!sources.isEmpty()) {
        PluginAware target = pluginApplication.getTarget();
        if (!(target instanceof ModelRegistryScope)) {
            throw new UnsupportedOperationException(String.format("Cannot apply model rules of plugin '%s' as the target '%s' is not model rule aware", pluginClass.getName(), target));
        }

        ModelRegistry modelRegistry = ((ModelRegistryScope) target).getModelRegistry();
        for (Class<?> source : sources) {
            inspector.inspect(source, modelRegistry, new PluginRuleSourceDependencies(target));
        }
    }
}
 
Example #18
public <R> void register(final MethodRuleDefinition<R> ruleDefinition, final ModelRegistry modelRegistry, RuleSourceDependencies dependencies) {
    doRegister(ruleDefinition, modelRegistry, dependencies);
}
 
Example #19
private <S extends BinarySpec, R> void configureMutationRule(ModelRegistry modelRegistry, ModelReference<BinaryContainer> subject, Class<? extends ComponentSpec> componentType, Class<S> binaryType, MethodRuleDefinition<R> ruleDefinition) {
    modelRegistry.mutate(new ComponentBinariesRule<R, S>(subject, componentType, binaryType, ruleDefinition, modelRegistry));
}
 
Example #20
public ComponentBinariesRule(ModelReference<BinaryContainer> subject, final Class<? extends ComponentSpec> componentType, final Class<S> binaryType, MethodRuleDefinition<R> ruleDefinition, ModelRegistry modelRegistry) {
    super(subject, componentType, ruleDefinition, ModelReference.of("componentSpecs", ComponentSpecContainer.class));
    this.componentType = componentType;
    this.binaryType = binaryType;
    this.modelRegistry = modelRegistry;
}
 
Example #21
public BinaryTaskRule(ModelReference<TaskContainer> subject, final Class<T> binaryType, MethodRuleDefinition<R> ruleDefinition, ModelRegistry modelRegistry) {
    super(subject, binaryType, ruleDefinition, ModelReference.of("binaries", BinaryContainer.class));
    this.binaryType = binaryType;
    this.modelRegistry = modelRegistry;
}
 
Example #22
Source Project: pushfish-android   Author: PushFish   File: ComponentModelBasePlugin.java    License: BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Inject
public ComponentModelBasePlugin(Instantiator instantiator, ModelRegistry modelRegistry) {
    this.instantiator = instantiator;
    this.modelRegistry = modelRegistry;
}
 
Example #23
Source Project: pushfish-android   Author: PushFish   File: LanguageBasePlugin.java    License: BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Inject
public LanguageBasePlugin(Instantiator instantiator, ModelRegistry modelRegistry) {
    this.instantiator = instantiator;
    this.modelRegistry = modelRegistry;
}
 
Example #24
Source Project: pushfish-android   Author: PushFish   File: NonTransformedModelDslBacking.java    License: BSD 2-Clause "Simplified" License 4 votes vote down vote up
public NonTransformedModelDslBacking(ModelRegistry modelRegistry) {
    this(new AtomicBoolean(), null, modelRegistry);
}
 
Example #25
Source Project: pushfish-android   Author: PushFish   File: NonTransformedModelDslBacking.java    License: BSD 2-Clause "Simplified" License 4 votes vote down vote up
private NonTransformedModelDslBacking(AtomicBoolean executingDsl, ModelPath modelPath, ModelRegistry modelRegistry) {
    this.executingDsl = executingDsl;
    this.modelPath = modelPath;
    this.modelRegistry = modelRegistry;
}
 
Example #26
Source Project: pushfish-android   Author: PushFish   File: TransformedModelDslBacking.java    License: BSD 2-Clause "Simplified" License 4 votes vote down vote up
public TransformedModelDslBacking(ModelRegistry modelRegistry, Object thisObject, Object owner) {
    this(modelRegistry, thisObject, owner, INPUT_PATHS_EXTRACTOR, RULE_LOCATION_EXTRACTOR);
}
 
Example #27
Source Project: pushfish-android   Author: PushFish   File: ComponentReport.java    License: BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Inject
protected ModelRegistry getModelRegistry() {
    throw new UnsupportedOperationException();
}
 
Example #28
Source Project: pushfish-android   Author: PushFish   File: AbstractProject.java    License: BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Inject
public ModelRegistry getModelRegistry() {
    // Decoration takes care of the implementation
    throw new UnsupportedOperationException();
}
 
Example #29
Source Project: pushfish-android   Author: PushFish   File: ProjectScopeServices.java    License: BSD 2-Clause "Simplified" License 4 votes vote down vote up
protected ModelRegistry createModelRegistry() {
    return new DefaultModelRegistry();
}
 
Example #30
public <R> void register(final MethodRuleDefinition<R> ruleDefinition, final ModelRegistry modelRegistry, RuleSourceDependencies dependencies) {
    doRegister(ruleDefinition, modelRegistry, dependencies);
}