Search in sources :

Example 1 with ModelSchemaAspectExtractionResult

use of org.gradle.model.internal.manage.schema.extract.ModelSchemaAspectExtractionResult 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(org.gradle.api.Nullable)

Aggregations

ImmutableSet (com.google.common.collect.ImmutableSet)1 Nullable (org.gradle.api.Nullable)1 ModelProperty (org.gradle.model.internal.manage.schema.ModelProperty)1 ModelSchemaAspectExtractionResult (org.gradle.model.internal.manage.schema.extract.ModelSchemaAspectExtractionResult)1 PropertyAccessorExtractionContext (org.gradle.model.internal.manage.schema.extract.PropertyAccessorExtractionContext)1