コード例 #1
0
  private Expression deserializeClassSimple(
      final int version,
      SerializerBuilder.StaticMethods staticMethods,
      CompatibilityLevel compatibilityLevel) {
    Expression local = let(constructor(this.getRawType()));

    List<Expression> list = new ArrayList<>();
    list.add(local);
    for (String fieldName : fields.keySet()) {
      FieldGen fieldGen = fields.get(fieldName);

      if (!fieldGen.hasVersion(version)) continue;

      Variable field = field(local, fieldName);
      fieldGen.serializer.prepareDeserializeStaticMethods(
          version, staticMethods, compatibilityLevel);
      list.add(
          set(
              field,
              fieldGen.serializer.deserialize(
                  fieldGen.getRawType(), version, staticMethods, compatibilityLevel)));
    }
    list.add(local);
    return sequence(list);
  }
コード例 #2
0
 private Expression callFactory(Map<String, Expression> map, int version) {
   Expression[] param = new Expression[factoryParams.size()];
   int i = 0;
   for (String fieldName : factoryParams) {
     final FieldGen fieldGen = fields.get(fieldName);
     checkNotNull(fieldGen, "Field '%s' is not found in '%s'", fieldName, factory);
     if (fieldGen.hasVersion(version)) {
       param[i++] = map.get(fieldName);
     } else {
       param[i++] = pushDefaultValue(fieldGen.getAsmType());
     }
   }
   return callStatic(factory.getDeclaringClass(), factory.getName(), param);
 }
コード例 #3
0
  @Override
  public void prepareSerializeStaticMethods(
      int version,
      SerializerBuilder.StaticMethods staticMethods,
      CompatibilityLevel compatibilityLevel) {
    if (staticMethods.startSerializeStaticMethod(this, version)) {
      return;
    }

    List<Expression> list = new ArrayList<>();
    for (String fieldName : fields.keySet()) {

      FieldGen fieldGen = fields.get(fieldName);
      if (!fieldGen.hasVersion(version)) continue;

      Class<?> type = fieldGen.serializer.getRawType();
      if (!fieldGen.getRawType().equals(Object.class)) type = fieldGen.getRawType();

      if (fieldGen.field != null) {
        fieldGen.serializer.prepareSerializeStaticMethods(
            version, staticMethods, compatibilityLevel);
        list.add(
            set(
                arg(1),
                fieldGen.serializer.serialize(
                    arg(0),
                    arg(1),
                    cast(field(arg(2), fieldName), type),
                    version,
                    staticMethods,
                    compatibilityLevel)));
      } else if (fieldGen.method != null) {
        fieldGen.serializer.prepareSerializeStaticMethods(
            version, staticMethods, compatibilityLevel);
        list.add(
            set(
                arg(1),
                fieldGen.serializer.serialize(
                    arg(0),
                    arg(1),
                    cast(call(arg(2), fieldGen.method.getName()), type),
                    version,
                    staticMethods,
                    compatibilityLevel)));
      } else throw new AssertionError();
    }
    list.add(arg(1));

    staticMethods.registerStaticSerializeMethod(this, version, sequence(list));
  }
コード例 #4
0
  private Expression callConstructor(
      Class<?> targetType, final Map<String, Expression> map, int version) {
    Expression[] param;
    if (constructorParams == null) {
      param = new Expression[0];
      return constructor(targetType, param);
    }
    param = new Expression[constructorParams.size()];

    int i = 0;
    for (String fieldName : constructorParams) {
      FieldGen fieldGen = fields.get(fieldName);
      checkNotNull(fieldGen, "Field '%s' is not found in '%s'", fieldName, constructor);
      if (fieldGen.hasVersion(version)) {
        param[i++] = map.get(fieldName);
      } else {
        param[i++] = pushDefaultValue(fieldGen.getAsmType());
      }
    }
    return constructor(targetType, param);
  }
コード例 #5
0
  private Expression deserializeInterface(
      Class<?> targetType,
      final int version,
      SerializerBuilder.StaticMethods staticMethods,
      CompatibilityLevel compatibilityLevel) {
    ClassBuilder<?> asmFactory =
        ClassBuilder.create(staticMethods.getDefiningClassLoader(), targetType);
    for (String fieldName : fields.keySet()) {
      FieldGen fieldGen = fields.get(fieldName);

      Method method = checkNotNull(fieldGen.method);

      asmFactory =
          asmFactory
              .withField(fieldName, method.getReturnType())
              .withMethod(method.getName(), field(self(), fieldName));
    }

    Class<?> newClass = asmFactory.build();

    Expression local = let(constructor(newClass));
    List<Expression> list = new ArrayList<>();
    list.add(local);

    for (String fieldName : fields.keySet()) {
      FieldGen fieldGen = fields.get(fieldName);
      if (!fieldGen.hasVersion(version)) continue;
      Variable field = field(local, fieldName);

      fieldGen.serializer.prepareDeserializeStaticMethods(
          version, staticMethods, compatibilityLevel);
      Expression expression =
          fieldGen.serializer.deserialize(
              fieldGen.getRawType(), version, staticMethods, compatibilityLevel);
      list.add(set(field, expression));
    }

    list.add(local);
    return sequence(list);
  }
コード例 #6
0
  @Override
  public void prepareDeserializeStaticMethods(
      int version,
      SerializerBuilder.StaticMethods staticMethods,
      CompatibilityLevel compatibilityLevel) {
    if (staticMethods.startDeserializeStaticMethod(this, version)) {
      return;
    }

    if (!implInterface && dataTypeIn.isInterface()) {
      Expression expression =
          deserializeInterface(this.getRawType(), version, staticMethods, compatibilityLevel);
      staticMethods.registerStaticDeserializeMethod(this, version, expression);
      return;
    }
    if (!implInterface && constructor == null && factory == null && setters.isEmpty()) {
      Expression expression = deserializeClassSimple(version, staticMethods, compatibilityLevel);
      staticMethods.registerStaticDeserializeMethod(this, version, expression);
      return;
    }

    List<Expression> list = new ArrayList<>();
    final Map<String, Expression> map = new HashMap<>();
    for (String fieldName : fields.keySet()) {
      FieldGen fieldGen = fields.get(fieldName);

      if (!fieldGen.hasVersion(version)) continue;

      fieldGen.serializer.prepareDeserializeStaticMethods(
          version, staticMethods, compatibilityLevel);
      Expression expression =
          let(
              fieldGen.serializer.deserialize(
                  fieldGen.getRawType(), version, staticMethods, compatibilityLevel));
      list.add(expression);
      map.put(fieldName, cast(expression, fieldGen.getRawType()));
    }

    Expression constructor;
    if (factory == null) {
      constructor = callConstructor(this.getRawType(), map, version);
    } else {
      constructor = callFactory(map, version);
    }

    Expression local = let(constructor);
    list.add(local);

    for (Method method : setters.keySet()) {
      boolean found = false;
      for (String fieldName : setters.get(method)) {
        FieldGen fieldGen = fields.get(fieldName);
        Preconditions.checkNotNull(fieldGen, "Field '%s' is not found in '%s'", fieldName, method);
        if (fieldGen.hasVersion(version)) {
          found = true;
          break;
        }
      }
      if (found) {
        Expression[] temp = new Expression[method.getParameterTypes().length];
        int i = 0;
        for (String fieldName : setters.get(method)) {
          FieldGen fieldGen = fields.get(fieldName);
          assert fieldGen != null;
          if (fieldGen.hasVersion(version)) {
            temp[i++] = map.get(fieldName);
          } else {
            temp[i++] = pushDefaultValue(fieldGen.getAsmType());
          }
        }
        list.add(call(local, method.getName(), temp));
      }
    }

    for (String fieldName : fields.keySet()) {
      FieldGen fieldGen = fields.get(fieldName);
      if (!fieldGen.hasVersion(version)) continue;
      if (fieldGen.field == null || isFinal(fieldGen.field.getModifiers())) continue;
      Variable field = field(local, fieldName);
      list.add(set(field, map.get(fieldName)));
    }

    list.add(local);
    staticMethods.registerStaticDeserializeMethod(this, version, sequence(list));
  }