Java Code Examples for com.google.protobuf.Message#getClass()

The following examples show how to use com.google.protobuf.Message#getClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProtobufDeserializer.java    From jackson-datatype-protobuf with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: ProtobufSerializer.java    From saluki with Apache License 2.0 5 votes vote down vote up
/**
 * @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);
  }
}
 
Example 3
Source File: DoWrite.java    From curiostack with MIT License 5 votes vote down vote up
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;
}
 
Example 4
Source File: ProtobufCreator.java    From jackson-datatype-protobuf with Apache License 2.0 4 votes vote down vote up
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());
  }
}