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();
}
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();
}
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;
}
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;
}
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);
}
Aggregations