Search in sources :

Example 1 with Attribute

use of org.gradle.api.attributes.Attribute in project gradle by gradle.

the class AmbiguousConfigurationSelectionException method formatAttributeMatches.

static void formatAttributeMatches(TreeFormatter formatter, AttributeContainerInternal consumerAttributes, AttributeMatcher attributeMatcher, AttributeContainerInternal producerAttributes) {
    Map<String, Attribute<?>> allAttributes = new TreeMap<String, Attribute<?>>();
    for (Attribute<?> attribute : producerAttributes.keySet()) {
        allAttributes.put(attribute.getName(), attribute);
    }
    for (Attribute<?> attribute : consumerAttributes.keySet()) {
        allAttributes.put(attribute.getName(), attribute);
    }
    ImmutableAttributes immmutableConsumer = consumerAttributes.asImmutable();
    ImmutableAttributes immutableProducer = producerAttributes.asImmutable();
    formatter.startChildren();
    for (Attribute<?> attribute : allAttributes.values()) {
        Attribute<Object> untyped = Cast.uncheckedCast(attribute);
        String attributeName = attribute.getName();
        AttributeValue<Object> consumerValue = immmutableConsumer.findEntry(untyped);
        AttributeValue<?> producerValue = immutableProducer.findEntry(attribute.getName());
        if (consumerValue.isPresent() && producerValue.isPresent()) {
            if (attributeMatcher.isMatching(untyped, producerValue.coerce(attribute), consumerValue.coerce(attribute))) {
                formatter.node("Required " + attributeName + " '" + consumerValue.get() + "' and found compatible value '" + producerValue.get() + "'.");
            } else {
                formatter.node("Required " + attributeName + " '" + consumerValue.get() + "' and found incompatible value '" + producerValue.get() + "'.");
            }
        } else if (consumerValue.isPresent()) {
            formatter.node("Required " + attributeName + " '" + consumerValue.get() + "' but no value provided.");
        } else {
            formatter.node("Found " + attributeName + " '" + producerValue.get() + "' but wasn't required.");
        }
    }
    formatter.endChildren();
}
Also used : ImmutableAttributes(org.gradle.api.internal.attributes.ImmutableAttributes) Attribute(org.gradle.api.attributes.Attribute) TreeMap(java.util.TreeMap)

Example 2 with Attribute

use of org.gradle.api.attributes.Attribute in project gradle by gradle.

the class ModuleMetadataFileGenerator method writeAttributes.

private void writeAttributes(AttributeContainer attributes, JsonWriter jsonWriter) throws IOException {
    if (attributes.isEmpty()) {
        return;
    }
    jsonWriter.name("attributes");
    jsonWriter.beginObject();
    Map<String, Attribute<?>> sortedAttributes = new TreeMap<String, Attribute<?>>();
    for (Attribute<?> attribute : attributes.keySet()) {
        sortedAttributes.put(attribute.getName(), attribute);
    }
    for (Attribute<?> attribute : sortedAttributes.values()) {
        jsonWriter.name(attribute.getName());
        Object value = attributes.getAttribute(attribute);
        if (value instanceof Boolean) {
            Boolean b = (Boolean) value;
            jsonWriter.value(b);
        } else if (value instanceof String) {
            String s = (String) value;
            jsonWriter.value(s);
        } else if (value instanceof Named) {
            Named named = (Named) value;
            jsonWriter.value(named.getName());
        } else if (value instanceof Enum) {
            Enum<?> enumValue = (Enum<?>) value;
            jsonWriter.value(enumValue.name());
        } else {
            throw new IllegalArgumentException(String.format("Cannot write attribute %s with unsupported value %s of type %s.", attribute.getName(), value, value.getClass().getName()));
        }
    }
    jsonWriter.endObject();
}
Also used : Named(org.gradle.api.Named) Attribute(org.gradle.api.attributes.Attribute) TreeMap(java.util.TreeMap)

Example 3 with Attribute

use of org.gradle.api.attributes.Attribute in project gradle by gradle.

the class ComponentAttributeMatcher method describeMatching.

@SuppressWarnings({ "unchecked", "rawtypes" })
public List<AttributeMatcher.MatchingDescription<?>> describeMatching(AttributeSelectionSchema schema, AttributeContainerInternal candidate, AttributeContainerInternal requested) {
    if (requested.isEmpty() || candidate.isEmpty()) {
        return Collections.emptyList();
    }
    ImmutableAttributes requestedAttributes = requested.asImmutable();
    ImmutableAttributes candidateAttributes = candidate.asImmutable();
    ImmutableSet<Attribute<?>> attributes = requestedAttributes.keySet();
    List<AttributeMatcher.MatchingDescription<?>> result = Lists.newArrayListWithCapacity(attributes.size());
    for (Attribute<?> attribute : attributes) {
        AttributeValue<?> requestedValue = requestedAttributes.findEntry(attribute);
        AttributeValue<?> candidateValue = candidateAttributes.findEntry(attribute.getName());
        if (candidateValue.isPresent()) {
            Object coercedValue = candidateValue.coerce(attribute);
            boolean match = schema.matchValue(attribute, requestedValue.get(), coercedValue);
            result.add(new AttributeMatcher.MatchingDescription(attribute, requestedValue, candidateValue, match));
        } else {
            result.add(new AttributeMatcher.MatchingDescription(attribute, requestedValue, candidateValue, false));
        }
    }
    return result;
}
Also used : ImmutableAttributes(org.gradle.api.internal.attributes.ImmutableAttributes) Attribute(org.gradle.api.attributes.Attribute)

Example 4 with Attribute

use of org.gradle.api.attributes.Attribute in project gradle by gradle.

the class DescriberSelector method selectDescriber.

public static AttributeDescriber selectDescriber(AttributeContainerInternal consumerAttributes, AttributesSchemaInternal consumerSchema) {
    List<AttributeDescriber> consumerDescribers = consumerSchema.getConsumerDescribers();
    Set<Attribute<?>> consumerAttributeSet = consumerAttributes.keySet();
    AttributeDescriber current = null;
    int maxSize = 0;
    for (AttributeDescriber consumerDescriber : consumerDescribers) {
        int size = Sets.intersection(consumerDescriber.getAttributes(), consumerAttributeSet).size();
        if (size > maxSize) {
            // Select the describer which handles the maximum number of attributes
            current = consumerDescriber;
            maxSize = size;
        }
    }
    if (current != null) {
        return new FallbackDescriber(current);
    }
    return DefaultDescriber.INSTANCE;
}
Also used : Attribute(org.gradle.api.attributes.Attribute) AttributeDescriber(org.gradle.api.internal.attributes.AttributeDescriber)

Example 5 with Attribute

use of org.gradle.api.attributes.Attribute in project gradle by gradle.

the class AbstractValueProcessor method processValue.

protected <T> T processValue(@Nullable Object value, ValueVisitor<T> visitor) {
    if (value == null) {
        return visitor.nullValue();
    }
    if (value instanceof String) {
        return visitor.stringValue((String) value);
    }
    if (value instanceof Boolean) {
        return visitor.booleanValue((Boolean) value);
    }
    if (value instanceof List) {
        List<?> list = (List<?>) value;
        if (list.size() == 0) {
            return visitor.emptyList();
        }
        ImmutableList.Builder<T> builder = ImmutableList.builderWithExpectedSize(list.size());
        for (Object element : list) {
            builder.add(processValue(element, visitor));
        }
        return visitor.list(builder.build());
    }
    if (value instanceof Enum) {
        return visitor.enumValue((Enum) value);
    }
    if (value instanceof Class<?>) {
        return visitor.classValue((Class<?>) value);
    }
    Class<?> valueClass = value.getClass();
    if (valueClass.equals(File.class)) {
        // Not subtypes as we don't know whether they are immutable or not
        return visitor.fileValue((File) value);
    }
    if (value instanceof Number) {
        if (value instanceof Integer) {
            return visitor.integerValue((Integer) value);
        }
        if (value instanceof Long) {
            return visitor.longValue((Long) value);
        }
        if (value instanceof Short) {
            return visitor.shortValue((Short) value);
        }
    }
    if (value instanceof Set) {
        Set<?> set = (Set<?>) value;
        ImmutableSet.Builder<T> builder = ImmutableSet.builderWithExpectedSize(set.size());
        for (Object element : set) {
            builder.add(processValue(element, visitor));
        }
        return visitor.set(builder.build());
    }
    if (value instanceof Map) {
        Map<?, ?> map = (Map<?, ?>) value;
        ImmutableList.Builder<MapEntrySnapshot<T>> builder = ImmutableList.builderWithExpectedSize(map.size());
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            builder.add(new MapEntrySnapshot<T>(processValue(entry.getKey(), visitor), processValue(entry.getValue(), visitor)));
        }
        if (value instanceof Properties) {
            return visitor.properties(builder.build());
        } else {
            return visitor.map(builder.build());
        }
    }
    if (valueClass.isArray()) {
        int length = Array.getLength(value);
        if (length == 0) {
            return visitor.emptyArray(valueClass.getComponentType());
        }
        ImmutableList.Builder<T> builder = ImmutableList.builderWithExpectedSize(length);
        for (int i = 0; i < length; i++) {
            Object element = Array.get(value, i);
            builder.add(processValue(element, visitor));
        }
        return visitor.array(builder.build(), valueClass.getComponentType());
    }
    if (value instanceof Attribute) {
        return visitor.attributeValue((Attribute<?>) value);
    }
    if (value instanceof Managed) {
        Managed managed = (Managed) value;
        if (managed.isImmutable()) {
            return visitor.managedImmutableValue(managed);
        } else {
            // May (or may not) be mutable - unpack the state
            T state = processValue(managed.unpackState(), visitor);
            return visitor.managedValue(managed, state);
        }
    }
    if (value instanceof Isolatable) {
        return visitor.fromIsolatable((Isolatable<?>) value);
    }
    if (value instanceof HashCode) {
        return visitor.hashCode((HashCode) value);
    }
    // Pluggable serialization
    for (ValueSnapshotterSerializerRegistry registry : valueSnapshotterSerializerRegistryList) {
        if (registry.canSerialize(valueClass)) {
            return gradleSerialization(value, registry.build(valueClass), visitor);
        }
    }
    // Fall back to Java serialization
    return javaSerialization(value, visitor);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Attribute(org.gradle.api.attributes.Attribute) ImmutableList(com.google.common.collect.ImmutableList) Properties(java.util.Properties) HashCode(org.gradle.internal.hash.HashCode) ImmutableSet(com.google.common.collect.ImmutableSet) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Isolatable(org.gradle.internal.isolation.Isolatable) Map(java.util.Map) Managed(org.gradle.internal.state.Managed)

Aggregations

Attribute (org.gradle.api.attributes.Attribute)12 ImmutableAttributes (org.gradle.api.internal.attributes.ImmutableAttributes)6 TreeMap (java.util.TreeMap)2 AttributeContainer (org.gradle.api.attributes.AttributeContainer)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Properties (java.util.Properties)1 Set (java.util.Set)1 Named (org.gradle.api.Named)1 PublishException (org.gradle.api.artifacts.PublishException)1 Category (org.gradle.api.attributes.Category)1 DocumentationRegistry (org.gradle.api.internal.DocumentationRegistry)1 AttributeContainerInternal (org.gradle.api.internal.attributes.AttributeContainerInternal)1 AttributeDescriber (org.gradle.api.internal.attributes.AttributeDescriber)1 SoftwareComponentInternal (org.gradle.api.internal.component.SoftwareComponentInternal)1 UsageContext (org.gradle.api.internal.component.UsageContext)1 ConfigurationMetadata (org.gradle.internal.component.model.ConfigurationMetadata)1