aboutsummaryrefslogtreecommitdiff
path: root/value/src/main/java/com/google/auto/value/extension/serializable/serializer/impl/ImmutableMapSerializerExtension.java
blob: 9d571e3bb3d11f38b1264a650b0e6da1a3e9daf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.auto.value.extension.serializable.serializer.impl;

import com.google.auto.common.MoreTypes;
import com.google.auto.service.AutoService;
import com.google.auto.value.extension.serializable.serializer.interfaces.Serializer;
import com.google.auto.value.extension.serializable.serializer.interfaces.SerializerExtension;
import com.google.auto.value.extension.serializable.serializer.interfaces.SerializerFactory;
import com.google.auto.value.extension.serializable.serializer.runtime.FunctionWithExceptions;
import com.google.common.collect.ImmutableMap;
import com.squareup.javapoet.CodeBlock;
import java.util.Optional;
import java.util.function.Function;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;

/**
 * A {@link SerializerExtension} that deserializes objects inside an {@link ImmutableMap}.
 *
 * <p>Enables unserializable objects inside an ImmutableMap to be serializable.
 */
@AutoService(SerializerExtension.class)
public final class ImmutableMapSerializerExtension implements SerializerExtension {

  public ImmutableMapSerializerExtension() {}

  @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, processingEnv));
  }

  private static class ImmutableMapSerializer implements Serializer {

    private final TypeMirror keyType;
    private final TypeMirror valueType;
    private final TypeMirror keyProxyType;
    private final TypeMirror valueProxyType;
    private final Serializer keyTypeSerializer;
    private final Serializer valueTypeSerializer;
    private final ProcessingEnvironment processingEnv;

    ImmutableMapSerializer(
        TypeMirror keyType,
        TypeMirror valueType,
        Serializer keyTypeSerializer,
        Serializer valueTypeSerializer,
        ProcessingEnvironment processingEnv) {
      this.keyType = keyType;
      this.valueType = valueType;
      this.keyProxyType = keyTypeSerializer.proxyFieldType();
      this.valueProxyType = valueTypeSerializer.proxyFieldType();
      this.keyTypeSerializer = keyTypeSerializer;
      this.valueTypeSerializer = valueTypeSerializer;
      this.processingEnv = processingEnv;
    }

    @Override
    public TypeMirror proxyFieldType() {
      TypeElement immutableMapTypeElement =
          processingEnv.getElementUtils().getTypeElement(ImmutableMap.class.getCanonicalName());
      return processingEnv
          .getTypeUtils()
          .getDeclaredType(immutableMapTypeElement, keyProxyType, valueProxyType);
    }

    @Override
    public CodeBlock toProxy(CodeBlock expression) {
      return CodeBlock.of(
          "$L.entrySet().stream().collect($T.toImmutableMap($L, $L))",
          expression,
          ImmutableMap.class,
          generateKeyMapFunction(keyType, keyProxyType, keyTypeSerializer::toProxy),
          generateValueMapFunction(valueType, valueProxyType, valueTypeSerializer::toProxy));
    }

    @Override
    public CodeBlock fromProxy(CodeBlock expression) {
      return CodeBlock.of(
          "$L.entrySet().stream().collect($T.toImmutableMap($L, $L))",
          expression,
          ImmutableMap.class,
          generateKeyMapFunction(keyProxyType, keyType, keyTypeSerializer::fromProxy),
          generateValueMapFunction(valueProxyType, valueType, valueTypeSerializer::fromProxy));
    }

    private static CodeBlock generateKeyMapFunction(
        TypeMirror originalType,
        TypeMirror transformedType,
        Function<CodeBlock, CodeBlock> proxyMap) {
      CodeBlock element = CodeBlock.of("element$$");
      return CodeBlock.of(
          "value$$ -> $T.<$T, $T>wrapper($L -> $L).apply(value$$.getKey())",
          FunctionWithExceptions.class,
          originalType,
          transformedType,
          element,
          proxyMap.apply(element));
    }

    private static CodeBlock generateValueMapFunction(
        TypeMirror originalType,
        TypeMirror transformedType,
        Function<CodeBlock, CodeBlock> proxyMap) {
      CodeBlock element = CodeBlock.of("element$$");
      return CodeBlock.of(
          "value$$ -> $T.<$T, $T>wrapper($L -> $L).apply(value$$.getValue())",
          FunctionWithExceptions.class,
          originalType,
          transformedType,
          element,
          proxyMap.apply(element));
    }
  }

  private static boolean isImmutableMap(TypeMirror type) {
    if (type.getKind() != TypeKind.DECLARED) {
      return false;
    }

    return MoreTypes.asTypeElement(type)
        .getQualifiedName()
        .contentEquals("com.google.common.collect.ImmutableMap");
  }

  private static TypeMirror getKeyType(TypeMirror type) {
    return MoreTypes.asDeclared(type).getTypeArguments().get(0);
  }

  private static TypeMirror getValueType(TypeMirror type) {
    return MoreTypes.asDeclared(type).getTypeArguments().get(1);
  }
}