Search in sources :

Example 6 with WeaklyTypeReferencingMethod

use of org.gradle.model.internal.method.WeaklyTypeReferencingMethod in project gradle by gradle.

the class ModelRuleExtractor method doExtract.

private <T> CachedRuleSource doExtract(final Class<T> source) {
    final ModelType<T> type = ModelType.of(source);
    FormattingValidationProblemCollector problems = new FormattingValidationProblemCollector("rule source", type);
    DefaultMethodModelRuleExtractionContext context = new DefaultMethodModelRuleExtractionContext(this, problems);
    // TODO - exceptions thrown here should point to some extensive documentation on the concept of class rule sources
    StructSchema<T> schema = getSchema(source, context);
    if (schema == null) {
        throw new InvalidModelRuleDeclarationException(problems.format());
    }
    // sort for determinism
    Set<Method> methods = new TreeSet<Method>(Ordering.usingToString());
    methods.addAll(Arrays.asList(source.getDeclaredMethods()));
    ImmutableList.Builder<ModelProperty<?>> implicitInputs = ImmutableList.builder();
    ModelProperty<?> target = null;
    for (ModelProperty<?> property : schema.getProperties()) {
        if (property.isAnnotationPresent(RuleTarget.class)) {
            target = property;
        } else if (property.isAnnotationPresent(RuleInput.class) && !(property.getSchema() instanceof ScalarValueSchema)) {
            implicitInputs.add(property);
        }
        for (WeaklyTypeReferencingMethod<?, ?> method : property.getAccessors()) {
            methods.remove(method.getMethod());
        }
    }
    ImmutableList.Builder<ExtractedRuleDetails> rules = ImmutableList.builder();
    for (Method method : methods) {
        MethodRuleDefinition<?, ?> ruleDefinition = DefaultMethodRuleDefinition.create(source, method);
        ExtractedModelRule rule = getMethodHandler(ruleDefinition, method, context);
        if (rule != null) {
            rules.add(new ExtractedRuleDetails(ruleDefinition, rule));
        }
    }
    if (context.hasProblems()) {
        throw new InvalidModelRuleDeclarationException(problems.format());
    }
    StructBindings<T> bindings = structBindingsStore.getBindings(schema.getType());
    if (schema.getProperties().isEmpty()) {
        return new StatelessRuleSource(rules.build(), Modifier.isAbstract(source.getModifiers()) ? new AbstractRuleSourceFactory<T>(schema, bindings, proxyFactory) : new ConcreteRuleSourceFactory<T>(type));
    } else {
        return new ParameterizedRuleSource(rules.build(), target, implicitInputs.build(), schema, bindings, proxyFactory);
    }
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Method(java.lang.reflect.Method) WeaklyTypeReferencingMethod(org.gradle.model.internal.method.WeaklyTypeReferencingMethod) TreeSet(java.util.TreeSet) ModelProperty(org.gradle.model.internal.manage.schema.ModelProperty) ScalarValueSchema(org.gradle.model.internal.manage.schema.ScalarValueSchema) InvalidModelRuleDeclarationException(org.gradle.model.InvalidModelRuleDeclarationException)

Aggregations

Method (java.lang.reflect.Method)6 WeaklyTypeReferencingMethod (org.gradle.model.internal.method.WeaklyTypeReferencingMethod)6 PropertyAccessorType (org.gradle.internal.reflect.PropertyAccessorType)5 ModelType (org.gradle.model.internal.type.ModelType)4 Type (org.objectweb.asm.Type)4 MethodVisitor (org.objectweb.asm.MethodVisitor)3 Wrapper (com.google.common.base.Equivalence.Wrapper)2 ModelProperty (org.gradle.model.internal.manage.schema.ModelProperty)2 ScalarValueSchema (org.gradle.model.internal.manage.schema.ScalarValueSchema)2 Label (org.objectweb.asm.Label)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 TreeSet (java.util.TreeSet)1 InvalidModelRuleDeclarationException (org.gradle.model.InvalidModelRuleDeclarationException)1 BridgeMethodBinding (org.gradle.model.internal.manage.binding.BridgeMethodBinding)1 DelegateMethodBinding (org.gradle.model.internal.manage.binding.DelegateMethodBinding)1 DirectMethodBinding (org.gradle.model.internal.manage.binding.DirectMethodBinding)1 ManagedProperty (org.gradle.model.internal.manage.binding.ManagedProperty)1 ManagedPropertyMethodBinding (org.gradle.model.internal.manage.binding.ManagedPropertyMethodBinding)1 StructMethodBinding (org.gradle.model.internal.manage.binding.StructMethodBinding)1