Search in sources :

Example 1 with ModelProperty

use of org.gradle.model.internal.manage.schema.ModelProperty in project gradle by gradle.

the class ManagedProxyClassGenerator method writeViewMethods.

private void writeViewMethods(ClassVisitor visitor, Type generatedType, Collection<ModelType<?>> viewTypes, StructBindings<?> bindings) {
    Type delegateType;
    StructSchema<?> delegateSchema = bindings.getDelegateSchema();
    if (delegateSchema != null) {
        Class<?> delegateClass = delegateSchema.getType().getRawClass();
        declareDelegateField(visitor, delegateClass);
        delegateType = Type.getType(delegateClass);
    } else {
        delegateType = null;
    }
    Multimap<String, ModelProperty<?>> viewPropertiesByNameBuilder = ArrayListMultimap.create();
    Set<Wrapper<Method>> viewMethods = Sets.newLinkedHashSet();
    for (StructSchema<?> viewSchema : bindings.getImplementedViewSchemas()) {
        for (ModelType<?> viewType : viewTypes) {
            if (viewType.equals(viewSchema.getType())) {
                for (ModelProperty<?> property : viewSchema.getProperties()) {
                    String propertyName = property.getName();
                    viewPropertiesByNameBuilder.put(propertyName, property);
                }
                for (WeaklyTypeReferencingMethod<?, ?> viewMethod : viewSchema.getAllMethods()) {
                    viewMethods.add(SIGNATURE_EQUIVALENCE.wrap(viewMethod.getMethod()));
                }
                break;
            }
        }
    }
    Class<?> viewClass = bindings.getPublicSchema().getType().getConcreteClass();
    for (Collection<ModelProperty<?>> viewProperties : viewPropertiesByNameBuilder.asMap().values()) {
        writeViewPropertyDslMethods(visitor, generatedType, viewProperties, viewClass);
    }
    for (StructMethodBinding methodBinding : bindings.getMethodBindings()) {
        WeaklyTypeReferencingMethod<?, ?> weakViewMethod = methodBinding.getViewMethod();
        Method viewMethod = weakViewMethod.getMethod();
        // Don't generate method if it's not part of the view schema
        Wrapper<Method> methodKey = SIGNATURE_EQUIVALENCE.wrap(viewMethod);
        if (!viewMethods.contains(methodKey)) {
            continue;
        }
        if (methodBinding instanceof DirectMethodBinding) {
            // TODO:LPTR What is with the "metaClass" property here?
            boolean isGetterMethod = methodBinding.getAccessorType() == GET_GETTER || methodBinding.getAccessorType() == IS_GETTER;
            if (isGetterMethod && !Modifier.isFinal(viewMethod.getModifiers()) && !viewMethod.getName().equals("getMetaClass")) {
                writeNonAbstractMethodWrapper(visitor, generatedType, viewClass, viewMethod);
            }
        } else if (methodBinding instanceof BridgeMethodBinding) {
            writeBridgeMethod(visitor, generatedType, viewMethod);
        } else if (methodBinding instanceof DelegateMethodBinding) {
            writeDelegatingMethod(visitor, generatedType, delegateType, viewMethod);
        } else if (methodBinding instanceof ManagedPropertyMethodBinding) {
            ManagedPropertyMethodBinding propertyBinding = (ManagedPropertyMethodBinding) methodBinding;
            ManagedProperty<?> managedProperty = bindings.getManagedProperty(propertyBinding.getPropertyName());
            String propertyName = managedProperty.getName();
            Class<?> propertyClass = managedProperty.getType().getRawClass();
            WeaklyTypeReferencingMethod<?, ?> propertyAccessor = propertyBinding.getViewMethod();
            switch(propertyBinding.getAccessorType()) {
                case GET_GETTER:
                case IS_GETTER:
                    writeGetter(visitor, generatedType, propertyName, propertyClass, propertyAccessor);
                    break;
                case SETTER:
                    writeSetter(visitor, generatedType, propertyName, propertyClass, propertyAccessor);
                    break;
                default:
                    throw new AssertionError();
            }
        } else {
            throw new AssertionError();
        }
    }
}
Also used : Wrapper(com.google.common.base.Equivalence.Wrapper) DirectMethodBinding(org.gradle.model.internal.manage.binding.DirectMethodBinding) Method(java.lang.reflect.Method) WeaklyTypeReferencingMethod(org.gradle.model.internal.method.WeaklyTypeReferencingMethod) BridgeMethodBinding(org.gradle.model.internal.manage.binding.BridgeMethodBinding) ManagedPropertyMethodBinding(org.gradle.model.internal.manage.binding.ManagedPropertyMethodBinding) DelegateMethodBinding(org.gradle.model.internal.manage.binding.DelegateMethodBinding) WeaklyTypeReferencingMethod(org.gradle.model.internal.method.WeaklyTypeReferencingMethod) ManagedProperty(org.gradle.model.internal.manage.binding.ManagedProperty) Type(org.objectweb.asm.Type) ModelType(org.gradle.model.internal.type.ModelType) StructMethodBinding(org.gradle.model.internal.manage.binding.StructMethodBinding) ModelProperty(org.gradle.model.internal.manage.schema.ModelProperty)

Example 2 with ModelProperty

use of org.gradle.model.internal.manage.schema.ModelProperty 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)

Example 3 with ModelProperty

use of org.gradle.model.internal.manage.schema.ModelProperty in project gradle by gradle.

the class VariantAspectExtractionStrategy method extract.

@Nullable
@Override
public ModelSchemaAspectExtractionResult extract(ModelSchemaExtractionContext<?> extractionContext, final List<ModelPropertyExtractionResult<?>> propertyResults) {
    ImmutableSet.Builder<ModelProperty<?>> dimensionsBuilder = ImmutableSet.builder();
    for (ModelPropertyExtractionResult<?> propertyResult : propertyResults) {
        ModelProperty<?> property = propertyResult.getProperty();
        for (PropertyAccessorExtractionContext accessor : propertyResult.getAccessors()) {
            if (accessor.isAnnotationPresent(Variant.class)) {
                if (accessor.getAccessorType() == PropertyAccessorType.SETTER) {
                    throw invalidProperty(extractionContext, property, "@Variant annotation is only allowed on getter methods");
                }
                Class<?> propertyType = property.getType().getRawClass();
                if (!String.class.equals(propertyType) && !Named.class.isAssignableFrom(propertyType)) {
                    throw invalidProperty(extractionContext, property, String.format("@Variant annotation only allowed for properties of type String and %s, but property has type %s", Named.class.getName(), propertyType.getName()));
                }
                dimensionsBuilder.add(property);
            }
        }
    }
    ImmutableSet<ModelProperty<?>> dimensions = dimensionsBuilder.build();
    if (dimensions.isEmpty()) {
        return null;
    }
    return new ModelSchemaAspectExtractionResult(new VariantAspect(dimensions));
}
Also used : PropertyAccessorExtractionContext(org.gradle.model.internal.manage.schema.extract.PropertyAccessorExtractionContext) ImmutableSet(com.google.common.collect.ImmutableSet) ModelSchemaAspectExtractionResult(org.gradle.model.internal.manage.schema.extract.ModelSchemaAspectExtractionResult) ModelProperty(org.gradle.model.internal.manage.schema.ModelProperty) Nullable(javax.annotation.Nullable)

Aggregations

ModelProperty (org.gradle.model.internal.manage.schema.ModelProperty)3 Method (java.lang.reflect.Method)2 WeaklyTypeReferencingMethod (org.gradle.model.internal.method.WeaklyTypeReferencingMethod)2 Wrapper (com.google.common.base.Equivalence.Wrapper)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 TreeSet (java.util.TreeSet)1 Nullable (javax.annotation.Nullable)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 ScalarValueSchema (org.gradle.model.internal.manage.schema.ScalarValueSchema)1 ModelSchemaAspectExtractionResult (org.gradle.model.internal.manage.schema.extract.ModelSchemaAspectExtractionResult)1 PropertyAccessorExtractionContext (org.gradle.model.internal.manage.schema.extract.PropertyAccessorExtractionContext)1 ModelType (org.gradle.model.internal.type.ModelType)1 Type (org.objectweb.asm.Type)1