Search in sources :

Example 1 with Serializer

use of com.google.auto.value.extension.serializable.serializer.interfaces.Serializer in project auto by google.

the class ImmutableListSerializerExtension method getSerializer.

@Override
public Optional<Serializer> getSerializer(TypeMirror typeMirror, SerializerFactory factory, ProcessingEnvironment processingEnv) {
    if (!isImmutableList(typeMirror)) {
        return Optional.empty();
    }
    // Extract the T of ImmutableList<T>.
    TypeMirror containedType = getContainedType(typeMirror);
    Serializer containedTypeSerializer = factory.getSerializer(containedType);
    // We don't need this serializer if the T of ImmutableList<T> is serializable.
    if (containedTypeSerializer.isIdentity()) {
        return Optional.empty();
    }
    return Optional.of(new ImmutableListSerializer(containedTypeSerializer, factory, processingEnv));
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) Serializer(com.google.auto.value.extension.serializable.serializer.interfaces.Serializer)

Example 2 with Serializer

use of com.google.auto.value.extension.serializable.serializer.interfaces.Serializer in project auto by google.

the class OptionalSerializerExtension method getSerializer.

/**
 * Creates a {@link Serializer} that supports {@link Optional} types.
 */
@Override
public Optional<Serializer> getSerializer(TypeMirror typeMirror, SerializerFactory factory, ProcessingEnvironment processingEnv) {
    if (!isOptional(typeMirror)) {
        return Optional.empty();
    }
    // Extract the T of Optional<T>.
    TypeMirror containedType = getContainedType(typeMirror);
    Serializer containedTypeSerializer = factory.getSerializer(containedType);
    return Optional.of(new OptionalSerializer(containedTypeSerializer));
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) Serializer(com.google.auto.value.extension.serializable.serializer.interfaces.Serializer)

Example 3 with Serializer

use of com.google.auto.value.extension.serializable.serializer.interfaces.Serializer in project auto by google.

the class ImmutableMapSerializerExtension method getSerializer.

@Override
public Optional<Serializer> getSerializer(TypeMirror typeMirror, SerializerFactory factory, ProcessingEnvironment processingEnv) {
    if (!isImmutableMap(typeMirror)) {
        return Optional.empty();
    }
    // Extract the K, V of ImmutableMap<K, V>.
    TypeMirror keyType = getKeyType(typeMirror);
    TypeMirror valueType = getValueType(typeMirror);
    Serializer keyTypeSerializer = factory.getSerializer(keyType);
    Serializer valueTypeSerializer = factory.getSerializer(valueType);
    // We don't need this serializer if the K and V of ImmutableMap<K, V> are serializable.
    if (keyTypeSerializer.isIdentity() && valueTypeSerializer.isIdentity()) {
        return Optional.empty();
    }
    return Optional.of(new ImmutableMapSerializer(keyType, valueType, keyTypeSerializer, valueTypeSerializer, factory, processingEnv));
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) Serializer(com.google.auto.value.extension.serializable.serializer.interfaces.Serializer)

Example 4 with Serializer

use of com.google.auto.value.extension.serializable.serializer.interfaces.Serializer in project auto by google.

the class ImmutableListSerializerExtensionTest method getSerializer_nonImmutableList_emptyReturned.

@Test
public void getSerializer_nonImmutableList_emptyReturned() {
    TypeMirror typeMirror = typeMirrorOf(String.class);
    Optional<Serializer> actualSerializer = extension.getSerializer(typeMirror, fakeSerializerFactory, mockProcessingEnvironment);
    assertThat(actualSerializer).isEmpty();
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) Serializer(com.google.auto.value.extension.serializable.serializer.interfaces.Serializer) Test(org.junit.Test) CompilationAbstractTest(com.google.auto.value.extension.serializable.serializer.utils.CompilationAbstractTest)

Example 5 with Serializer

use of com.google.auto.value.extension.serializable.serializer.interfaces.Serializer in project auto by google.

the class ImmutableListSerializerExtensionTest method proxyFieldType.

@Test
public void proxyFieldType() {
    TypeMirror typeMirror = declaredTypeOf(ImmutableList.class, Integer.class);
    Serializer serializer = extension.getSerializer(typeMirror, fakeSerializerFactory, mockProcessingEnvironment).get();
    TypeMirror actualTypeMirror = serializer.proxyFieldType();
    assertThat(typeUtils.isSameType(actualTypeMirror, typeMirror)).isTrue();
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) Serializer(com.google.auto.value.extension.serializable.serializer.interfaces.Serializer) Test(org.junit.Test) CompilationAbstractTest(com.google.auto.value.extension.serializable.serializer.utils.CompilationAbstractTest)

Aggregations

Serializer (com.google.auto.value.extension.serializable.serializer.interfaces.Serializer)23 CompilationAbstractTest (com.google.auto.value.extension.serializable.serializer.utils.CompilationAbstractTest)20 TypeMirror (javax.lang.model.type.TypeMirror)20 Test (org.junit.Test)20 CodeBlock (com.squareup.javapoet.CodeBlock)6 SerializerFactory (com.google.auto.value.extension.serializable.serializer.interfaces.SerializerFactory)1 TestStringSerializerFactory (com.google.auto.value.extension.serializable.serializer.utils.TestStringSerializerFactory)1