com.google.protobuf.Message#getClass ( )源码实例Demo

下面列出了com.google.protobuf.Message#getClass ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

private JsonDeserializer<Object> getMessageDeserializer(
        Message.Builder builder,
        FieldDescriptor field,
        Message defaultInstance,
        DeserializationContext context
) throws IOException {
  JsonDeserializer<Object> deserializer = deserializerCache.get(field);
  if (deserializer == null) {
    final Class<?> subType;
    if (defaultInstance == null) {
      Message.Builder subBuilder = builder.newBuilderForField(field);
      subType = subBuilder.getDefaultInstanceForType().getClass();
    } else {
      subType = defaultInstance.getClass();
    }

    JavaType type = context.constructType(subType);
    deserializer = context.findContextualValueDeserializer(type, null);
    deserializerCache.put(field, deserializer);
  }

  return deserializer;
}
 
源代码2 项目: saluki   文件: ProtobufSerializer.java
/**
 * @see io.github.saluki.serializer.IProtobufSerializer#fromProtobuf(com.google.protobuf.Message,
 *      java.lang.Class)
 */
@Override
public Object fromProtobuf(Message protobuf, Class<?> pojoClazz) throws ProtobufException {
  try {
    final Class<? extends Message> protoClazz =
        ProtobufSerializerUtils.getProtobufClassFromPojoAnno(pojoClazz);
    if (protoClazz == null) {
      throw new ProtobufAnnotationException(
          "Doesn't seem like " + pojoClazz + " is ProtobufEntity");
    }
    final Map<Field, ProtobufAttribute> protobufFields =
        ProtobufSerializerUtils.getAllProtbufFields(pojoClazz);
    if (protobufFields.isEmpty()) {
      throw new ProtobufException("No protoBuf fields have been annotated on the class "
          + pojoClazz + ", thus cannot continue.");
    }
    Object pojo = pojoClazz.newInstance();
    for (Entry<Field, ProtobufAttribute> entry : protobufFields.entrySet()) {
      final Field field = entry.getKey();
      final ProtobufAttribute protobufAttribute = entry.getValue();
      final String setter = ProtobufSerializerUtils.getPojoSetter(protobufAttribute, field);
      Object protobufValue =
          Protobuf2PojoHelp.getProtobufFieldValue(protobuf, protobufAttribute, field);
      if (protobufValue == null) {
        continue;
      }
      Protobuf2PojoHelp.setPojoFieldValue(pojo, setter, protobufValue, protobufAttribute, field);
    }
    return pojo;
  } catch (Exception e) {
    throw new ProtobufException("Could not generate POJO of type " + pojoClazz
        + " from Protobuf object " + protobuf.getClass() + ": " + e, e);
  }
}
 
源代码3 项目: curiostack   文件: DoWrite.java
DoWrite(
    Message prototype,
    boolean includeDefaults,
    boolean printingEnumsAsInts,
    boolean sortingMapKeys) {
  this.prototype = prototype;
  this.messageClass = prototype.getClass();
  this.descriptor = prototype.getDescriptorForType();
  this.includeDefaults = includeDefaults;
  this.printingEnumsAsInts = printingEnumsAsInts;
  this.sortingMapKeys = sortingMapKeys;
}
 
private Object getValue(Builder builder, FieldDescriptor field, Message defaultInstance, ExtensionRegistryWrapper extensionRegistry) {
  switch (field.getJavaType()) {
    case INT:
      return r.nextInt();
    case LONG:
      return r.nextLong();
    case FLOAT:
      return r.nextFloat();
    case DOUBLE:
      return r.nextDouble();
    case BOOLEAN:
      return r.nextBoolean();
    case STRING:
      String available = "abcdefghijklmnopqrstuvwxyz0123456789";
      int length = r.nextInt(20) + 1;
      String value = "";
      for (int i = 0; i < length; i++) {
        value += available.charAt(r.nextInt(available.length()));
      }
      return value;
    case BYTE_STRING:
      byte[] bytes = new byte[r.nextInt(20) + 1];
      r.nextBytes(bytes);
      return ByteString.copyFrom(bytes);
    case ENUM:
      List<EnumValueDescriptor> values = field.getEnumType().getValues();
      return values.get(r.nextInt(values.size()));
    case MESSAGE:
      final Class<? extends Message> subMessageType;
      if (field.isExtension()) {
        subMessageType = defaultInstance.getClass();
      } else {
        subMessageType = builder.newBuilderForField(field).getDefaultInstanceForType().getClass();
      }

      // Handle recursive relationships by returning a partially populated proto (better than an infinite loop)
      if (partiallyBuilt.containsKey(subMessageType)) {
        return partiallyBuilt.get(subMessageType).build();
      } else {
        return create(subMessageType, extensionRegistry);
      }
    default:
      throw new IllegalArgumentException("Unrecognized field type: " + field.getJavaType());
  }
}