Java Code Examples for com.google.protobuf.Descriptors.FieldDescriptor#getContainingOneof()

The following examples show how to use com.google.protobuf.Descriptors.FieldDescriptor#getContainingOneof() . 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: DynamicMessage.java    From play-store-api with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Builder setField(FieldDescriptor field, Object value) {
  verifyContainingType(field);
  ensureIsMutable();
  // TODO(xiaofeng): This check should really be put in FieldSet.setField()
  // where all other such checks are done. However, currently
  // FieldSet.setField() permits Integer value for enum fields probably
  // because of some internal features we support. Should figure it out
  // and move this check to a more appropriate place.
  if (field.getType() == FieldDescriptor.Type.ENUM) {
    ensureEnumValueDescriptor(field, value);
  }
  OneofDescriptor oneofDescriptor = field.getContainingOneof();
  if (oneofDescriptor != null) {
    int index = oneofDescriptor.getIndex();
    FieldDescriptor oldField = oneofCases[index];
    if ((oldField != null) && (oldField != field)) {
      fields.clearField(oldField);
    }
    oneofCases[index] = field;
  }
  fields.setField(field, value);
  return this;
}
 
Example 2
Source File: GeneratedMessage.java    From play-store-api with GNU General Public License v3.0 6 votes vote down vote up
SingularFieldAccessor(
    final FieldDescriptor descriptor, final String camelCaseName,
    final Class<? extends GeneratedMessage> messageClass,
    final Class<? extends Builder> builderClass,
    final String containingOneofCamelCaseName) {
  field = descriptor;
  isOneofField = descriptor.getContainingOneof() != null;
  hasHasMethod = supportFieldPresence(descriptor.getFile())
      || (!isOneofField && descriptor.getJavaType() == FieldDescriptor.JavaType.MESSAGE);
  getMethod = getMethodOrDie(messageClass, "get" + camelCaseName);
  getMethodBuilder = getMethodOrDie(builderClass, "get" + camelCaseName);
  type = getMethod.getReturnType();
  setMethod = getMethodOrDie(builderClass, "set" + camelCaseName, type);
  hasMethod =
      hasHasMethod ? getMethodOrDie(messageClass, "has" + camelCaseName) : null;
  hasMethodBuilder =
      hasHasMethod ? getMethodOrDie(builderClass, "has" + camelCaseName) : null;
  clearMethod = getMethodOrDie(builderClass, "clear" + camelCaseName);
  caseMethod = isOneofField ? getMethodOrDie(
      messageClass, "get" + containingOneofCamelCaseName + "Case") : null;
  caseMethodBuilder = isOneofField ? getMethodOrDie(
      builderClass, "get" + containingOneofCamelCaseName + "Case") : null;
}
 
Example 3
Source File: DynamicMessage.java    From 365browser with Apache License 2.0 6 votes vote down vote up
public Builder setField(FieldDescriptor field, Object value) {
  verifyContainingType(field);
  ensureIsMutable();
  // TODO(xiaofeng): This check should really be put in FieldSet.setField()
  // where all other such checks are done. However, currently
  // FieldSet.setField() permits Integer value for enum fields probably
  // because of some internal features we support. Should figure it out
  // and move this check to a more appropriate place.
  if (field.getType() == FieldDescriptor.Type.ENUM) {
    ensureEnumValueDescriptor(field, value);
  }
  OneofDescriptor oneofDescriptor = field.getContainingOneof();
  if (oneofDescriptor != null) {
    int index = oneofDescriptor.getIndex();
    FieldDescriptor oldField = oneofCases[index];
    if ((oldField != null) && (oldField != field)) {
      fields.clearField(oldField);
    }
    oneofCases[index] = field;
  }
  fields.setField(field, value);
  return this;
}
 
Example 4
Source File: GeneratedMessage.java    From 365browser with Apache License 2.0 6 votes vote down vote up
SingularFieldAccessor(
    final FieldDescriptor descriptor, final String camelCaseName,
    final Class<? extends GeneratedMessage> messageClass,
    final Class<? extends Builder> builderClass,
    final String containingOneofCamelCaseName) {
  field = descriptor;
  isOneofField = descriptor.getContainingOneof() != null;
  hasHasMethod = supportFieldPresence(descriptor.getFile())
      || (!isOneofField && descriptor.getJavaType() == FieldDescriptor.JavaType.MESSAGE);
  getMethod = getMethodOrDie(messageClass, "get" + camelCaseName);
  getMethodBuilder = getMethodOrDie(builderClass, "get" + camelCaseName);
  type = getMethod.getReturnType();
  setMethod = getMethodOrDie(builderClass, "set" + camelCaseName, type);
  hasMethod =
      hasHasMethod ? getMethodOrDie(messageClass, "has" + camelCaseName) : null;
  hasMethodBuilder =
      hasHasMethod ? getMethodOrDie(builderClass, "has" + camelCaseName) : null;
  clearMethod = getMethodOrDie(builderClass, "clear" + camelCaseName);
  caseMethod = isOneofField ? getMethodOrDie(
      messageClass, "get" + containingOneofCamelCaseName + "Case") : null;
  caseMethodBuilder = isOneofField ? getMethodOrDie(
      builderClass, "get" + containingOneofCamelCaseName + "Case") : null;
}
 
Example 5
Source File: DynamicMessage.java    From play-store-api with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Builder clearField(FieldDescriptor field) {
  verifyContainingType(field);
  ensureIsMutable();
  OneofDescriptor oneofDescriptor = field.getContainingOneof();
  if (oneofDescriptor != null) {
    int index = oneofDescriptor.getIndex();
    if (oneofCases[index] == field) {
      oneofCases[index] = null;
    }
  }
  fields.clearField(field);
  return this;
}
 
Example 6
Source File: DynamicMessage.java    From 365browser with Apache License 2.0 5 votes vote down vote up
public Builder clearField(FieldDescriptor field) {
  verifyContainingType(field);
  ensureIsMutable();
  OneofDescriptor oneofDescriptor = field.getContainingOneof();
  if (oneofDescriptor != null) {
    int index = oneofDescriptor.getIndex();
    if (oneofCases[index] == field) {
      oneofCases[index] = null;
    }
  }
  fields.clearField(field);
  return this;
}
 
Example 7
Source File: GeneratedMessage.java    From play-store-api with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Internal helper to return a modifiable map containing all the fields.
 * The returned Map is modifialbe so that the caller can add additional
 * extension fields to implement {@link #getAllFields()}.
 *
 * @param getBytesForString whether to generate ByteString for string fields
 */
private Map<FieldDescriptor, Object> getAllFieldsMutable(
    boolean getBytesForString) {
  final TreeMap<FieldDescriptor, Object> result =
    new TreeMap<FieldDescriptor, Object>();
  final Descriptor descriptor = internalGetFieldAccessorTable().descriptor;
  final List<FieldDescriptor> fields = descriptor.getFields();

  for (int i = 0; i < fields.size(); i++) {
    FieldDescriptor field = fields.get(i);
    final OneofDescriptor oneofDescriptor = field.getContainingOneof();

    /*
     * If the field is part of a Oneof, then at maximum one field in the Oneof is set
     * and it is not repeated. There is no need to iterate through the others.
     */
    if (oneofDescriptor != null) {
      // Skip other fields in the Oneof we know are not set
      i += oneofDescriptor.getFieldCount() - 1;
      if (!hasOneof(oneofDescriptor)) {
        // If no field is set in the Oneof, skip all the fields in the Oneof
        continue;
      }
      // Get the pointer to the only field which is set in the Oneof
      field = getOneofFieldDescriptor(oneofDescriptor);
    } else {
      // If we are not in a Oneof, we need to check if the field is set and if it is repeated
      if (field.isRepeated()) {
        final List<?> value = (List<?>) getField(field);
        if (!value.isEmpty()) {
          result.put(field, value);
        }
        continue;
      }
      if (!hasField(field)) {
        continue;
      }
    }
    // Add the field to the map
    if (getBytesForString && field.getJavaType() == FieldDescriptor.JavaType.STRING) {
      result.put(field, getFieldRaw(field));
    } else {
      result.put(field, getField(field));
    }
  }
  return result;
}
 
Example 8
Source File: GeneratedMessage.java    From play-store-api with GNU General Public License v3.0 4 votes vote down vote up
/** Internal helper which returns a mutable map. */
private Map<FieldDescriptor, Object> getAllFieldsMutable() {
  final TreeMap<FieldDescriptor, Object> result =
    new TreeMap<FieldDescriptor, Object>();
  final Descriptor descriptor = internalGetFieldAccessorTable().descriptor;
  final List<FieldDescriptor> fields = descriptor.getFields();

  for (int i = 0; i < fields.size(); i++) {
    FieldDescriptor field = fields.get(i);
    final OneofDescriptor oneofDescriptor = field.getContainingOneof();

    /*
     * If the field is part of a Oneof, then at maximum one field in the Oneof is set
     * and it is not repeated. There is no need to iterate through the others.
     */
    if (oneofDescriptor != null) {
      // Skip other fields in the Oneof we know are not set
      i += oneofDescriptor.getFieldCount() - 1;
      if (!hasOneof(oneofDescriptor)) {
        // If no field is set in the Oneof, skip all the fields in the Oneof
        continue;
      }
      // Get the pointer to the only field which is set in the Oneof
      field = getOneofFieldDescriptor(oneofDescriptor);
    } else {
      // If we are not in a Oneof, we need to check if the field is set and if it is repeated
      if (field.isRepeated()) {
        final List<?> value = (List<?>) getField(field);
        if (!value.isEmpty()) {
          result.put(field, value);
        }
        continue;
      }
      if (!hasField(field)) {
        continue;
      }
    }
    // Add the field to the map
    result.put(field, getField(field));
  }
  return result;
}
 
Example 9
Source File: GeneratedMessage.java    From play-store-api with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Ensures the field accessors are initialized. This method is thread-safe.
 *
 * @param messageClass   The message type.
 * @param builderClass   The builder type.
 * @return this
 */
public FieldAccessorTable ensureFieldAccessorsInitialized(
    Class<? extends GeneratedMessage> messageClass,
    Class<? extends Builder> builderClass) {
  if (initialized) { return this; }
  synchronized (this) {
    if (initialized) { return this; }
    int fieldsSize = fields.length;
    for (int i = 0; i < fieldsSize; i++) {
      FieldDescriptor field = descriptor.getFields().get(i);
      String containingOneofCamelCaseName = null;
      if (field.getContainingOneof() != null) {
        containingOneofCamelCaseName =
            camelCaseNames[fieldsSize + field.getContainingOneof().getIndex()];
      }
      if (field.isRepeated()) {
        if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
          if (field.isMapField() && isMapFieldEnabled(field)) {
            fields[i] = new MapFieldAccessor(
                field, camelCaseNames[i], messageClass, builderClass);
          } else {
            fields[i] = new RepeatedMessageFieldAccessor(
                field, camelCaseNames[i], messageClass, builderClass);
          }
        } else if (field.getJavaType() == FieldDescriptor.JavaType.ENUM) {
          fields[i] = new RepeatedEnumFieldAccessor(
              field, camelCaseNames[i], messageClass, builderClass);
        } else {
          fields[i] = new RepeatedFieldAccessor(
              field, camelCaseNames[i], messageClass, builderClass);
        }
      } else {
        if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
          fields[i] = new SingularMessageFieldAccessor(
              field, camelCaseNames[i], messageClass, builderClass,
              containingOneofCamelCaseName);
        } else if (field.getJavaType() == FieldDescriptor.JavaType.ENUM) {
          fields[i] = new SingularEnumFieldAccessor(
              field, camelCaseNames[i], messageClass, builderClass,
              containingOneofCamelCaseName);
        } else if (field.getJavaType() == FieldDescriptor.JavaType.STRING) {
          fields[i] = new SingularStringFieldAccessor(
              field, camelCaseNames[i], messageClass, builderClass,
              containingOneofCamelCaseName);
        } else {
          fields[i] = new SingularFieldAccessor(
              field, camelCaseNames[i], messageClass, builderClass,
              containingOneofCamelCaseName);
        }
      }
    }

    int oneofsSize = oneofs.length;
    for (int i = 0; i < oneofsSize; i++) {
      oneofs[i] = new OneofAccessor(
          descriptor, camelCaseNames[i + fieldsSize],
          messageClass, builderClass);
    }
    initialized = true;
    camelCaseNames = null;
    return this;
  }
}
 
Example 10
Source File: GeneratedMessage.java    From 365browser with Apache License 2.0 4 votes vote down vote up
/**
 * Ensures the field accessors are initialized. This method is thread-safe.
 *
 * @param messageClass   The message type.
 * @param builderClass   The builder type.
 * @return this
 */
public FieldAccessorTable ensureFieldAccessorsInitialized(
    Class<? extends GeneratedMessage> messageClass,
    Class<? extends Builder> builderClass) {
  if (initialized) { return this; }
  synchronized (this) {
    if (initialized) { return this; }
    int fieldsSize = fields.length;
    for (int i = 0; i < fieldsSize; i++) {
      FieldDescriptor field = descriptor.getFields().get(i);
      String containingOneofCamelCaseName = null;
      if (field.getContainingOneof() != null) {
        containingOneofCamelCaseName =
            camelCaseNames[fieldsSize + field.getContainingOneof().getIndex()];
      }
      if (field.isRepeated()) {
        if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
          fields[i] = new RepeatedMessageFieldAccessor(
              field, camelCaseNames[i], messageClass, builderClass);
        } else if (field.getJavaType() == FieldDescriptor.JavaType.ENUM) {
          fields[i] = new RepeatedEnumFieldAccessor(
              field, camelCaseNames[i], messageClass, builderClass);
        } else {
          fields[i] = new RepeatedFieldAccessor(
              field, camelCaseNames[i], messageClass, builderClass);
        }
      } else {
        if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
          fields[i] = new SingularMessageFieldAccessor(
              field, camelCaseNames[i], messageClass, builderClass,
              containingOneofCamelCaseName);
        } else if (field.getJavaType() == FieldDescriptor.JavaType.ENUM) {
          fields[i] = new SingularEnumFieldAccessor(
              field, camelCaseNames[i], messageClass, builderClass,
              containingOneofCamelCaseName);
        } else {
          fields[i] = new SingularFieldAccessor(
              field, camelCaseNames[i], messageClass, builderClass,
              containingOneofCamelCaseName);
        }
      }
    }

    int oneofsSize = oneofs.length;
    for (int i = 0; i < oneofsSize; i++) {
      oneofs[i] = new OneofAccessor(
          descriptor, camelCaseNames[i + fieldsSize],
          messageClass, builderClass);
    }
    initialized = true;
    camelCaseNames = null;
    return this;
  }
}
 
Example 11
Source File: MessageSerializer.java    From jackson-datatype-protobuf with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(
        MessageOrBuilder message,
        JsonGenerator generator,
        SerializerProvider serializerProvider
) throws IOException {
  generator.writeStartObject();

  boolean proto3 = message.getDescriptorForType().getFile().getSyntax() == Syntax.PROTO3;
  Include include = serializerProvider.getConfig().getDefaultPropertyInclusion().getValueInclusion();
  boolean writeDefaultValues = proto3 && include != Include.NON_DEFAULT;
  boolean writeEmptyCollections = include != Include.NON_DEFAULT && include != Include.NON_EMPTY;
  PropertyNamingStrategyBase namingStrategy =
          new PropertyNamingStrategyWrapper(serializerProvider.getConfig().getPropertyNamingStrategy());

  Descriptor descriptor = message.getDescriptorForType();
  List<FieldDescriptor> fields = new ArrayList<>(descriptor.getFields());
  if (message instanceof ExtendableMessageOrBuilder<?>) {
    for (ExtensionInfo extensionInfo : config.extensionRegistry().getExtensionsByDescriptor(descriptor)) {
      fields.add(extensionInfo.descriptor);
    }
  }

  for (FieldDescriptor field : fields) {
    if (field.isRepeated()) {
      List<?> valueList = (List<?>) message.getField(field);

      if (!valueList.isEmpty() || writeEmptyCollections) {
        if (field.isMapField()) {
          generator.writeFieldName(namingStrategy.translate(field.getName()));
          writeMap(field, valueList, generator, serializerProvider);
        } else if (valueList.size() == 1 && writeSingleElementArraysUnwrapped(serializerProvider)) {
          generator.writeFieldName(namingStrategy.translate(field.getName()));
          writeValue(field, valueList.get(0), generator, serializerProvider);
        } else {
          generator.writeArrayFieldStart(namingStrategy.translate(field.getName()));
          for (Object subValue : valueList) {
            writeValue(field, subValue, generator, serializerProvider);
          }
          generator.writeEndArray();
        }
      }
    } else if (message.hasField(field) || (writeDefaultValues && !supportsFieldPresence(field) && field.getContainingOneof() == null)) {
      generator.writeFieldName(namingStrategy.translate(field.getName()));
      writeValue(field, message.getField(field), generator, serializerProvider);
    } else if (include == Include.ALWAYS && field.getContainingOneof() == null) {
      generator.writeFieldName(namingStrategy.translate(field.getName()));
      generator.writeNull();
    }
  }

  generator.writeEndObject();
}