Search in sources :

Example 1 with Managed

use of org.gradle.internal.state.Managed 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

ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Set (java.util.Set)1 Attribute (org.gradle.api.attributes.Attribute)1 HashCode (org.gradle.internal.hash.HashCode)1 Isolatable (org.gradle.internal.isolation.Isolatable)1 Managed (org.gradle.internal.state.Managed)1