aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec/AutoCodecProcessor.java
blob: ed54d585080979ae631225d63cf15c58cd35a1b6 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright 2017 The Bazel Authors. All rights reserved.
//
// 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.devtools.build.lib.skyframe.serialization.autocodec;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec;
import com.google.devtools.build.lib.skyframe.serialization.SerializationException;
import com.google.protobuf.CodedInputStream;
import com.google.protobuf.CodedOutputStream;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
import java.io.IOException;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.ElementFilter;
import javax.tools.Diagnostic;

/**
 * Javac annotation processor (compiler plugin) for generating {@link ObjectCodec} implementations.
 *
 * <p>User code must never reference this class.
 */
@AutoService(Processor.class)
public class AutoCodecProcessor extends AbstractProcessor {
  /**
   * Passing {@code --javacopt=-Aautocodec_print_generated} to {@code blaze build} tells AutoCodec
   * to print the generated code.
   */
  private static final String PRINT_GENERATED_OPTION = "autocodec_print_generated";

  private ProcessingEnvironment env; // Captured from `init` method.

  @Override
  public Set<String> getSupportedOptions() {
    return ImmutableSet.of(PRINT_GENERATED_OPTION);
  }

  @Override
  public Set<String> getSupportedAnnotationTypes() {
    return ImmutableSet.of(AutoCodecUtil.ANNOTATION.getCanonicalName());
  }

  @Override
  public SourceVersion getSupportedSourceVersion() {
    return SourceVersion.latestSupported(); // Supports all versions of Java.
  }

  @Override
  public synchronized void init(ProcessingEnvironment processingEnv) {
    super.init(processingEnv);
    this.env = processingEnv;
  }

  @Override
  public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    for (Element element : roundEnv.getElementsAnnotatedWith(AutoCodecUtil.ANNOTATION)) {
      AutoCodec annotation = element.getAnnotation(AutoCodecUtil.ANNOTATION);
      switch (annotation.strategy()) {
        case CONSTRUCTOR:
          buildCodecUsingConstructor((TypeElement) element);
          break;
        default:
          throw new IllegalArgumentException("Unknown strategy: " + annotation.strategy());
      }
    }
    return true;
  }

  /**
   * Uses the first constructor of the class to synthesize a codec.
   *
   * <p>This strategy depends on
   *
   * <ul>
   *   <li>the class constructor taking all serialized fields as parameters
   *   <li>and each serialized field having a corresponding getter.
   * </ul>
   *
   * For example, a constructor having parameter, {@code target}, should having a matching getter,
   * {@code getTarget()}.
   *
   * <p>The first constructor is the first ocurring in the source code.
   */
  private void buildCodecUsingConstructor(TypeElement classElement) {
    TypeSpec.Builder codecClassBuilder =
        TypeSpec.classBuilder(AutoCodecUtil.getCodecName(classElement))
            .superclass(TypeName.get(classElement.asType()));

    TypeElement encodedType = getEncodedType(classElement);

    // Generates the getEncodedClass method.
    codecClassBuilder.addMethod(
        MethodSpec.methodBuilder("getEncodedClass")
            .addModifiers(Modifier.PUBLIC)
            .addAnnotation(Override.class)
            .returns(
                ParameterizedTypeName.get(
                    ClassName.get(Class.class), TypeName.get(encodedType.asType())))
            .addStatement("return $T.class", TypeName.get(encodedType.asType()))
            .build());

    // In Java, every class has a constructor, so this always succeeds.
    ExecutableElement constructor =
        ElementFilter.constructorsIn(encodedType.getEnclosedElements()).get(0);
    List<? extends VariableElement> constructorParameters = constructor.getParameters();
    addSerializeMethodUsingConstructor(codecClassBuilder, encodedType, constructorParameters);
    addDeserializeMethodUsingConstructor(codecClassBuilder, encodedType, constructorParameters);

    String packageName =
        env.getElementUtils().getPackageOf(classElement).getQualifiedName().toString();
    try {
      JavaFile file = JavaFile.builder(packageName, codecClassBuilder.build()).build();
      file.writeTo(env.getFiler());
      if (env.getOptions().containsKey("autocodec_print_generated")) {
        note("AutoCodec generated codec for " + classElement + ":\n" + file);
      }
    } catch (IOException e) {
      env.getMessager()
          .printMessage(Diagnostic.Kind.ERROR, "Failed to generate output file: " + e.getMessage());
    }
  }

  /**
   * Heuristic that converts a constructor parameter to a getter.
   *
   * <p>For example, a parameter called {@code target} results in {@code getTarget()}.
   */
  private static String paramNameAsAccessor(String name) {
    return "get" + name.substring(0, 1).toUpperCase() + name.substring(1) + "()";
  }

  private void addSerializeMethodUsingConstructor(
      TypeSpec.Builder codecClassBuilder,
      TypeElement encodedType,
      List<? extends VariableElement> constructorParameters) {
    MethodSpec.Builder serializeBuilder =
        MethodSpec.methodBuilder("serialize")
            .addModifiers(Modifier.PUBLIC)
            .returns(void.class)
            .addParameter(TypeName.get(encodedType.asType()), "input")
            .addParameter(CodedOutputStream.class, "codedOut")
            .addAnnotation(Override.class)
            .addException(SerializationException.class)
            .addException(IOException.class);
    for (VariableElement parameter : constructorParameters) {
      buildSerializeBody(
          serializeBuilder,
          (DeclaredType) parameter.asType(),
          "input." + paramNameAsAccessor(parameter.getSimpleName().toString()));
    }
    codecClassBuilder.addMethod(serializeBuilder.build());
  }

  private void addDeserializeMethodUsingConstructor(
      TypeSpec.Builder codecClassBuilder,
      TypeElement encodedType,
      List<? extends VariableElement> constructorParameters) {
    MethodSpec.Builder deserializeBuilder =
        MethodSpec.methodBuilder("deserialize")
            .addModifiers(Modifier.PUBLIC)
            .returns(TypeName.get(encodedType.asType()))
            .addParameter(CodedInputStream.class, "codedIn")
            .addAnnotation(Override.class)
            .addException(SerializationException.class)
            .addException(IOException.class);
    for (VariableElement parameter : constructorParameters) {
      buildDeserializeBody(
          deserializeBuilder,
          (DeclaredType) parameter.asType(),
          parameter.getSimpleName().toString());
    }
    // Invokes the constructor and returns the value.
    deserializeBuilder.addStatement(
        "return new $T($L)",
        TypeName.get(encodedType.asType()),
        constructorParameters
            .stream()
            .map(p -> p.getSimpleName().toString())
            .collect(Collectors.joining(", ")));
    codecClassBuilder.addMethod(deserializeBuilder.build());
  }

  /**
   * Appends code statements to {@code builder} to serialize a pre-declared variable named {@code
   * accessor}.
   *
   * @param type the type of {@code accessor}
   */
  private void buildSerializeBody(MethodSpec.Builder builder, DeclaredType type, String accessor) {
    builder.beginControlFlow("if ($L != null)", accessor); // Begin if not null block.
    builder.addStatement("codedOut.writeBoolNoTag(true)");
    // TODO(shahan): Add support for more types.
    if (matchesErased(type, ImmutableSortedSet.class)) {
      // Writes the target count to the stream so deserialization knows when to stop.
      builder.addStatement("codedOut.writeInt32NoTag($L.size())", accessor);
      DeclaredType repeatedType = (DeclaredType) type.getTypeArguments().get(0);
      // TODO(shahan): consider introducing a depth parameter to avoid shadowing here.
      builder.beginControlFlow("for ($T repeated : $L)", TypeName.get(repeatedType), accessor);
      buildSerializeBody(builder, repeatedType, "repeated");
      builder.endControlFlow();
    } else {
      // Otherwise use the type's CODEC.
      builder.addStatement("$T.CODEC.serialize($L, codedOut)", TypeName.get(type), accessor);
    }
    builder.nextControlFlow("else");
    builder.addStatement("codedOut.writeBoolNoTag(false)");
    builder.endControlFlow(); // End if not null.
  }

  /**
   * Appends code statements to {@code builder} declaring a variable called {@code name} and
   * initializing it by deserialization.
   *
   * @param type the type of {@code name}
   */
  private void buildDeserializeBody(MethodSpec.Builder builder, DeclaredType type, String name) {
    builder.addStatement("$T $L = null", TypeName.get(type), name);
    builder.beginControlFlow("if (codedIn.readBool())"); // Begin null-handling block.
    // TODO(shahan): Add support for more types.
    if (matchesErased(type, ImmutableSortedSet.class)) {
      DeclaredType repeatedType = (DeclaredType) type.getTypeArguments().get(0);
      builder.addStatement(
          "$T<$T> builder = new $T<>($T.naturalOrder())",
          ImmutableSortedSet.Builder.class,
          TypeName.get(repeatedType),
          ImmutableSortedSet.Builder.class,
          Comparator.class);
      builder.addStatement("int length = codedIn.readInt32()");
      builder.beginControlFlow("for (int i = 0; i < length; ++i)");
      buildDeserializeBody(builder, repeatedType, "repeated");
      builder.addStatement("builder.add(repeated)");
      builder.endControlFlow();
      builder.addStatement("$L = builder.build()", name);
    } else {
      // Otherwise, use the type's CODEC value.
      builder.addStatement("$L = $T.CODEC.deserialize(codedIn)", name, TypeName.get(type));
    }
    builder.endControlFlow(); // End null-handling block.
  }

  /**
   * Gets the type parameter of ObjectCodec, i.e., the type being encoded.
   *
   * <p>{@code element} must implement ObjectCodec.
   */
  private TypeElement getEncodedType(TypeElement element) {
    for (TypeMirror implementedInterface : element.getInterfaces()) {
      if (matchesErased(implementedInterface, ObjectCodec.class)) {
        return (TypeElement)
            env.getTypeUtils()
                .asElement(((DeclaredType) implementedInterface).getTypeArguments().get(0));
      }
    }
    throw new IllegalArgumentException(element + " does not implement ObjectCodec!");
  }

  /** True when erasure of {@code type} matches erasure of {@code clazz}. */
  private boolean matchesErased(TypeMirror type, Class<?> clazz) {
    return env.getTypeUtils()
        .isSameType(
            env.getTypeUtils().erasure(type),
            env.getTypeUtils()
                .erasure(
                    env.getElementUtils().getTypeElement((clazz.getCanonicalName())).asType()));
  }

  /** Emits a note to BUILD log during annotation processing for debugging. */
  private void note(String note) {
    env.getMessager().printMessage(Diagnostic.Kind.NOTE, note);
  }
}