Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright (C) 2014 Google, Inc.
   * 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
  * 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 auto.parcelgson.processor;
 import java.util.List;
 import java.util.Map;
Generates GWT serialization code for @AutoParcelGson classes also marked @GwtCompatible(serializable = true).

√Čamonn McManus
 class GwtSerialization {
   private final GwtCompatibility gwtCompatibility;
   private final ProcessingEnvironment processingEnv;
   private final TypeElement type;
       GwtCompatibility gwtCompatibilityProcessingEnvironment processingEnvTypeElement type) {
     this. = gwtCompatibility;
     this. = processingEnv;
     this. = type;
   private boolean shouldWriteGwtSerializer() {
     Optional<AnnotationMirroroptionalGwtCompatible = .gwtCompatibleAnnotation();
     if (optionalGwtCompatible.isPresent()) {
       AnnotationMirror gwtCompatible = optionalGwtCompatible.get();
       for (Map.Entry<ExecutableElementAnnotationValueentry :
           Collections.unmodifiableMap(gwtCompatible.getElementValues()).entrySet()) {
         if (entry.getKey().getSimpleName().contentEquals("serializable")
             && entry.getValue().getValue().equals(true)) {
           return true;
     return false;

Writes the GWT serializer for the given type, if appropriate. An @AutoParcelGson class gets a GWT serializer if it is annotated with @GwtCompatible(serializable = true), where the @GwtCompatible annotation can come from any package.

If the type is com.example.Foo then the generated AutoParcel subclass is com.example.AutoParcelGson_Foo and the GWT serializer is com.example.AutoParcelGson_Foo_CustomFieldSerializer.

autoVars the template variables defined for this type.
     if (shouldWriteGwtSerializer()) {
       GwtTemplateVars vars = new GwtTemplateVars();
       vars.imports = autoVars.imports;
       vars.pkg = autoVars.pkg;
       vars.subclass = autoVars.subclass;
       String className = (vars.pkg.isEmpty() ? "" : vars.pkg + ".") + vars.subclass
           + "_CustomFieldSerializer";
       vars.serializerClass = TypeSimplifier.simpleNameOf(className);
       vars.props = new ArrayList<Property>();
       for (AutoParcelProcessor.Property prop : autoVars.props) {
         vars.props.add(new Property(prop));
       vars.classHashString = computeClassHash(autoVars.props);
       String text = vars.toText();
  public static class Property {
    private final AutoParcelProcessor.Property property;
    private final boolean isCastingUnchecked;
      this. = property;
      this. = TypeSimplifier.isCastingUnchecked(property.getTypeMirror());
    @Override public String toString() {
      return .toString();
    public String getGetter() {
      return .getGetter();
    public String getType() {
      return .getType();

Returns the suffix in serializer method names for values of the given type. For example, if the type is "int" then the returned value will be "Int" because the serializer methods are called readInt and writeInt. There are methods for all primitive types and String; every other type uses readObject and writeObject.
    public String getGwtType() {
      String type = .getType();
      if (.getKind().isPrimitive()) {
        return Character.toUpperCase(type.charAt(0)) + type.substring(1);
      } else if (type.equals("String")) {
        return "String";
      } else {
        return "Object";

Returns a string to be inserted before the call to the readFoo() call so that the expression can be assigned to the given type. For primitive types and String, the readInt() etc methods already return the right type so the string is empty. For other types, the string is a cast like "(Foo) ".
    public String getGwtCast() {
      if (.getKind().isPrimitive() || getType().equals("String")) {
        return "";
      } else {
        return "(" + getType() + ") ";
    public boolean isCastingUnchecked() {
      return ;
  @SuppressWarnings("unused")  // some fields are only read through reflection
  static class GwtTemplateVars extends TemplateVars {
The properties defined by the parent class's abstract methods.

The fully-qualified names of the classes to be imported in the generated class.

The package of the class with the @AutoParcelGson annotation and its generated subclass.
    String pkg;

The simple name of the generated subclass.
    String subclass;

The simple name of the generated GWT serializer class.
    String serializerClass;

A string that should change if any salient details of the serialized class change.
    private static final SimpleNode TEMPLATE = parsedTemplateForResource("gwtserializer.vm");
      return ;
  private void writeSourceFile(String classNameString textTypeElement originatingType) {
    try {
      JavaFileObject sourceFile =
      Writer writer = sourceFile.openWriter();
      try {
      } finally {
    } catch (IOException e) {
          "Could not write generated class " + className + ": " + e);
  private static final Charset UTF8 = Charset.forName("UTF-8");
    TypeSimplifier typeSimplifier = new TypeSimplifier(
        .getTypeUtils(), ""new TypeMirrorSet(), null);
    CRC32 crc = new CRC32();
    update(crctypeSimplifier.simplify(.asType()) + ":");
    for (AutoParcelProcessor.Property prop : props) {
      update(crcprop.toString() + ":" + prop.getType() + ";");
    return String.format("%08x"crc.getValue());
  private static void update(CRC32 crcString s) {
New to GrepCode? Check out our FAQ X