Search in sources :

Example 1 with Named

use of org.gradle.api.Named 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 2 with Named

use of org.gradle.api.Named in project gradle by gradle.

the class VariantsMatcher method filterBinaries.

public Collection<? extends BinarySpec> filterBinaries(VariantsMetaData variantsMetaData, Collection<BinarySpec> binaries) {
    if (binaries.isEmpty()) {
        return binaries;
    }
    Set<String> resolveDimensions = variantsMetaData.getNonNullVariantAxes();
    TreeMultimap<String, VariantValue> selectedVariants = TreeMultimap.create(String.CASE_INSENSITIVE_ORDER, SPEC_COMPARATOR);
    Set<BinarySpec> removedSpecs = Sets.newHashSet();
    for (BinarySpec binarySpec : binaries) {
        if (binarySpecType.isAssignableFrom(binarySpec.getClass())) {
            VariantsMetaData binaryVariants = DefaultVariantsMetaData.extractFrom(binarySpec, schemaStore.getSchema(((BinarySpecInternal) binarySpec).getPublicType()));
            Set<String> commonsDimensions = Sets.intersection(resolveDimensions, binaryVariants.getNonNullVariantAxes());
            Set<String> incompatibleDimensionTypes = VariantsMetaDataHelper.determineAxesWithIncompatibleTypes(variantsMetaData, binaryVariants, commonsDimensions);
            if (incompatibleDimensionTypes.isEmpty()) {
                for (String dimension : commonsDimensions) {
                    Class<?> dimensionType = variantsMetaData.getVariantAxisType(dimension).getConcreteClass();
                    boolean isStringType = String.class == dimensionType;
                    Object requestedValue = isStringType ? variantsMetaData.getValueAsString(dimension) : variantsMetaData.getValueAsType(Cast.<Class<? extends Named>>uncheckedCast(dimensionType), dimension);
                    Object binaryValue = isStringType ? binaryVariants.getValueAsString(dimension) : binaryVariants.getValueAsType(Cast.<Class<? extends Named>>uncheckedCast(dimensionType), dimension);
                    VariantAxisCompatibility<Object> selector = createSelector(requestedValue);
                    if (selector.isCompatibleWithRequirement(requestedValue, binaryValue)) {
                        VariantValue value = new VariantValue(binaryValue, binarySpec);
                        SortedSet<VariantValue> variantValues = selectedVariants.get(dimension);
                        for (VariantValue variantValue : variantValues) {
                            // and incrementally build a list of binaries which are excluded because of a better match
                            if (selector.betterFit(requestedValue, variantValue.value, binaryValue)) {
                                // the new value is a better fit than the old one
                                removedSpecs.add(variantValue.spec);
                            } else if (selector.betterFit(requestedValue, binaryValue, variantValue.value)) {
                                // the old value is a better fit than the new one, let's ignore the new one altogether
                                removedSpecs.add(value.spec);
                            }
                        }
                        selectedVariants.put(dimension, value);
                    } else {
                        removedSpecs.add(binarySpec);
                    }
                }
            }
        }
    }
    Set<BinarySpec> union = null;
    for (String dimension : selectedVariants.keySet()) {
        Set<BinarySpec> variantValues = ImmutableSet.copyOf(Iterables.transform(selectedVariants.get(dimension), VariantValue.SPEC_FUNCTION));
        union = union == null ? variantValues : Sets.union(union, variantValues);
    }
    return union == null ? Collections.<BinarySpec>emptySet() : Sets.difference(union, removedSpecs);
}
Also used : Named(org.gradle.api.Named) BinarySpecInternal(org.gradle.platform.base.internal.BinarySpecInternal) BinarySpec(org.gradle.platform.base.BinarySpec)

Example 3 with Named

use of org.gradle.api.Named in project gradle by gradle.

the class DesugaringAttributeContainerSerializer method write.

@Override
public void write(Encoder encoder, AttributeContainer container) throws IOException {
    encoder.writeSmallInt(container.keySet().size());
    for (Attribute<?> attribute : container.keySet()) {
        encoder.writeString(attribute.getName());
        if (attribute.getType().equals(Boolean.class)) {
            encoder.writeByte(BOOLEAN_ATTRIBUTE);
            encoder.writeBoolean((Boolean) container.getAttribute(attribute));
        } else if (attribute.getType().equals(String.class)) {
            encoder.writeByte(STRING_ATTRIBUTE);
            encoder.writeString((String) container.getAttribute(attribute));
        } else if (attribute.getType().equals(Integer.class)) {
            encoder.writeByte(INTEGER_ATTRIBUTE);
            encoder.writeInt((Integer) container.getAttribute(attribute));
        } else {
            assert Named.class.isAssignableFrom(attribute.getType());
            Named attributeValue = (Named) container.getAttribute(attribute);
            encoder.writeByte(DESUGARED_ATTRIBUTE);
            encoder.writeString(attributeValue.getName());
        }
    }
}
Also used : Named(org.gradle.api.Named)

Example 4 with Named

use of org.gradle.api.Named in project gradle by gradle.

the class RendererUtils method displayValueOf.

/**
 * Converts a value into a string. Never returns {@code null}.
 *
 * <p>Rules used to determine the string value:</p>
 *
 * <ul>
 *     <li>the {@code null} value is converted to the string {@code "null"},</li>
 *     <li>if the value has a type that overrides {@link Object#toString()} then it is converted to {@code value.toString()},</li>
 *     <li>if the value's type implements {@link Named} then it is converted to {@code value.getName()},</li>
 *     <li>otherwise the return value of {@link Object#toString()} is used.</li>
 * </ul>
 *
 * <p>The method returns the converted value, unless it is {@code null}, in which case the string {@code "null"} is returned instead.</p>
 */
public static String displayValueOf(Object value) {
    String result = null;
    if (value != null) {
        boolean hasCustomToString;
        try {
            Method toString = value.getClass().getMethod("toString");
            hasCustomToString = !toString.getDeclaringClass().equals(Object.class);
        } catch (NoSuchMethodException ignore) {
            hasCustomToString = false;
        }
        if (!hasCustomToString && Named.class.isAssignableFrom(value.getClass())) {
            result = ((Named) value).getName();
        } else {
            result = value.toString();
        }
    }
    if (result == null) {
        result = "null";
    }
    return result;
}
Also used : Named(org.gradle.api.Named) Method(java.lang.reflect.Method)

Example 5 with Named

use of org.gradle.api.Named in project gradle by gradle.

the class DefaultNamedDomainObjectCollection method addLater.

@Override
public void addLater(final Provider<? extends T> provider) {
    assertMutable("addLater(Provider)");
    super.addLater(provider);
    if (provider instanceof Named) {
        final Named named = (Named) provider;
        index.putPending(named.getName(), Providers.internal(provider));
        deferredElementKnown(named.getName(), provider);
    }
}
Also used : Named(org.gradle.api.Named)

Aggregations

Named (org.gradle.api.Named)5 Method (java.lang.reflect.Method)1 TreeMap (java.util.TreeMap)1 Attribute (org.gradle.api.attributes.Attribute)1 BinarySpec (org.gradle.platform.base.BinarySpec)1 BinarySpecInternal (org.gradle.platform.base.internal.BinarySpecInternal)1