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

The following examples show how to use com.google.protobuf.Descriptors.FieldDescriptor#isExtension() . 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: GeneratedMessage.java    From travelguide with Apache License 2.0 6 votes vote down vote up
@Override
public Object getField(final FieldDescriptor field) {
  if (field.isExtension()) {
    verifyContainingType(field);
    final Object value = extensions.getField(field);
    if (value == null) {
      if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
        // Lacking an ExtensionRegistry, we have no way to determine the
        // extension's real type, so we return a DynamicMessage.
        return DynamicMessage.getDefaultInstance(field.getMessageType());
      } else {
        return field.getDefaultValue();
      }
    } else {
      return value;
    }
  } else {
    return super.getField(field);
  }
}
 
Example 2
Source File: GeneratedMessage.java    From travelguide with Apache License 2.0 6 votes vote down vote up
@Override
public Object getField(final FieldDescriptor field) {
  if (field.isExtension()) {
    verifyContainingType(field);
    final Object value = extensions.getField(field);
    if (value == null) {
      if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
        // Lacking an ExtensionRegistry, we have no way to determine the
        // extension's real type, so we return a DynamicMessage.
        return DynamicMessage.getDefaultInstance(field.getMessageType());
      } else {
        return field.getDefaultValue();
      }
    } else {
      return value;
    }
  } else {
    return super.getField(field);
  }
}
 
Example 3
Source File: ProtobufDecompiler.java    From sql-layer with GNU Affero General Public License v3.0 6 votes vote down vote up
protected void decompileOptions(MessageOrBuilder options) throws IOException {
    for (Map.Entry<FieldDescriptor,Object> entry : options.getAllFields().entrySet()) {
        FieldDescriptor field = entry.getKey();
        Object value = entry.getValue();
        String fieldName = field.getName();
        if (field.isExtension()) {
            fieldName = "(" + fieldName + ")";
        }
        if (field.getType() == FieldDescriptor.Type.MESSAGE) {
            for (Map.Entry<FieldDescriptor,Object> subentry : ((MessageOrBuilder)value).getAllFields().entrySet()) {
                FieldDescriptor subfield = subentry.getKey();
                Object subvalue = subentry.getValue();
                indentedFormat("option %s.%s = %s;", fieldName, subfield.getName(), literal(subvalue, subfield.getType()));
            }
        }
        else {
            indentedFormat("option %s = %s;", fieldName, literal(value, field.getType()));
        }
    }
}
 
Example 4
Source File: GeneratedMessage.java    From play-store-api with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Object getField(final FieldDescriptor field) {
  if (field.isExtension()) {
    verifyContainingType(field);
    final Object value = extensions.getField(field);
    if (value == null) {
      if (field.isRepeated()) {
        return Collections.emptyList();
      } else if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
        // Lacking an ExtensionRegistry, we have no way to determine the
        // extension's real type, so we return a DynamicMessage.
        return DynamicMessage.getDefaultInstance(field.getMessageType());
      } else {
        return field.getDefaultValue();
      }
    } else {
      return value;
    }
  } else {
    return super.getField(field);
  }
}
 
Example 5
Source File: GeneratedMessage.java    From travelguide with Apache License 2.0 5 votes vote down vote up
@Override
public BuilderType setRepeatedField(final FieldDescriptor field,
                                    final int index, final Object value) {
  if (field.isExtension()) {
    verifyContainingType(field);
    ensureExtensionsIsMutable();
    extensions.setRepeatedField(field, index, value);
    onChanged();
    return (BuilderType) this;
  } else {
    return super.setRepeatedField(field, index, value);
  }
}
 
Example 6
Source File: GeneratedMessage.java    From play-store-api with GNU General Public License v3.0 5 votes vote down vote up
@Override
public BuilderType clearField(final FieldDescriptor field) {
  if (field.isExtension()) {
    verifyContainingType(field);
    ensureExtensionsIsMutable();
    extensions.clearField(field);
    onChanged();
    return (BuilderType) this;
  } else {
    return super.clearField(field);
  }
}
 
Example 7
Source File: GeneratedMessage.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public boolean hasField(final FieldDescriptor field) {
  if (field.isExtension()) {
    verifyContainingType(field);
    return extensions.hasField(field);
  } else {
    return super.hasField(field);
  }
}
 
Example 8
Source File: GeneratedMessage.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public BuilderType setField(final FieldDescriptor field,
                            final Object value) {
  if (field.isExtension()) {
    verifyContainingType(field);
    ensureExtensionsIsMutable();
    extensions.setField(field, value);
    onChanged();
    return (BuilderType) this;
  } else {
    return super.setField(field, value);
  }
}
 
Example 9
Source File: GeneratedMessage.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Get the FieldAccessor for a particular field. */
private FieldAccessor getField(final FieldDescriptor field) {
  if (field.getContainingType() != descriptor) {
    throw new IllegalArgumentException(
      "FieldDescriptor does not match message type.");
  } else if (field.isExtension()) {
    // If this type had extensions, it would subclass ExtendableMessage,
    // which overrides the reflection interface to handle extensions.
    throw new IllegalArgumentException(
      "This type does not have extensions.");
  }
  return fields[field.getIndex()];
}
 
Example 10
Source File: GeneratedMessage.java    From travelguide with Apache License 2.0 5 votes vote down vote up
@Override
public boolean hasField(final FieldDescriptor field) {
  if (field.isExtension()) {
    verifyContainingType(field);
    return extensions.hasField(field);
  } else {
    return super.hasField(field);
  }
}
 
Example 11
Source File: AbstractMessage.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public int getSerializedSize() {
  int size = memoizedSize;
  if (size != -1) {
    return size;
  }

  size = 0;
  final boolean isMessageSet =
      getDescriptorForType().getOptions().getMessageSetWireFormat();

  for (final Map.Entry<FieldDescriptor, Object> entry :
      getAllFields().entrySet()) {
    final FieldDescriptor field = entry.getKey();
    final Object value = entry.getValue();
    if (isMessageSet && field.isExtension() &&
        field.getType() == FieldDescriptor.Type.MESSAGE &&
        !field.isRepeated()) {
      size += CodedOutputStream.computeMessageSetExtensionSize(
          field.getNumber(), (Message) value);
    } else {
      size += FieldSet.computeFieldSize(field, value);
    }
  }

  final UnknownFieldSet unknownFields = getUnknownFields();
  if (isMessageSet) {
    size += unknownFields.getSerializedSizeAsMessageSet();
  } else {
    size += unknownFields.getSerializedSize();
  }

  memoizedSize = size;
  return size;
}
 
Example 12
Source File: GeneratedMessage.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public int getRepeatedFieldCount(final FieldDescriptor field) {
  if (field.isExtension()) {
    verifyContainingType(field);
    return extensions.getRepeatedFieldCount(field);
  } else {
    return super.getRepeatedFieldCount(field);
  }
}
 
Example 13
Source File: GeneratedMessage.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public BuilderType setField(final FieldDescriptor field,
                            final Object value) {
  if (field.isExtension()) {
    verifyContainingType(field);
    ensureExtensionsIsMutable();
    extensions.setField(field, value);
    onChanged();
    return (BuilderType) this;
  } else {
    return super.setField(field, value);
  }
}
 
Example 14
Source File: GeneratedMessage.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public boolean hasField(final FieldDescriptor field) {
  if (field.isExtension()) {
    verifyContainingType(field);
    return extensions.hasField(field);
  } else {
    return super.hasField(field);
  }
}
 
Example 15
Source File: GeneratedMessage.java    From travelguide with Apache License 2.0 5 votes vote down vote up
@Override
public BuilderType setField(final FieldDescriptor field,
                            final Object value) {
  if (field.isExtension()) {
    verifyContainingType(field);
    ensureExtensionsIsMutable();
    extensions.setField(field, value);
    onChanged();
    return (BuilderType) this;
  } else {
    return super.setField(field, value);
  }
}
 
Example 16
Source File: ProtobufJsonFormat.java    From tajo with Apache License 2.0 4 votes vote down vote up
private void printSingleField(FieldDescriptor field,
                              Object value,
                              JsonGenerator generator) throws IOException {
  if (field.isExtension()) {
    generator.print("\"");
    // We special-case MessageSet elements for compatibility with proto1.
    if (field.getContainingType().getOptions().getMessageSetWireFormat()
        && (field.getType() == FieldDescriptor.Type.MESSAGE) && (field.isOptional())
        // object equality
        && (field.getExtensionScope() == field.getMessageType())) {
      generator.print(field.getMessageType().getFullName());
    } else {
      generator.print(field.getFullName());
    }
    generator.print("\"");
  } else {
    generator.print("\"");
    if (field.getType() == FieldDescriptor.Type.GROUP) {
      // Groups must be serialized with their original capitalization.
      generator.print(field.getMessageType().getName());
    } else {
      generator.print(field.getName());
    }
    generator.print("\"");
  }

  // Done with the name, on to the value

  if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
    generator.print(": ");
    generator.indent();
  } else {
    generator.print(": ");
  }


  if (field.isRepeated()) {
    // Repeated field. Print each element.
    generator.print("[");
    for (Iterator<?> iter = ((List<?>) value).iterator(); iter.hasNext();) {
      printFieldValue(field, iter.next(), generator);
      if (iter.hasNext()) {
        generator.print(",");
      }
    }
    generator.print("]");
  } else {
    printFieldValue(field, value, generator);
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
      generator.outdent();
    }
  }
}
 
Example 17
Source File: TextFormat.java    From play-store-api with GNU General Public License v3.0 4 votes vote down vote up
private void printSingleField(final FieldDescriptor field,
                              final Object value,
                              final TextGenerator generator)
                              throws IOException {
  if (field.isExtension()) {
    generator.print("[");
    // We special-case MessageSet elements for compatibility with proto1.
    if (field.getContainingType().getOptions().getMessageSetWireFormat()
        && (field.getType() == FieldDescriptor.Type.MESSAGE)
        && (field.isOptional())
        // object equality
        && (field.getExtensionScope() == field.getMessageType())) {
      generator.print(field.getMessageType().getFullName());
    } else {
      generator.print(field.getFullName());
    }
    generator.print("]");
  } else {
    if (field.getType() == FieldDescriptor.Type.GROUP) {
      // Groups must be serialized with their original capitalization.
      generator.print(field.getMessageType().getName());
    } else {
      generator.print(field.getName());
    }
  }

  if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
    if (singleLineMode) {
      generator.print(" { ");
    } else {
      generator.print(" {\n");
      generator.indent();
    }
  } else {
    generator.print(": ");
  }

  printFieldValue(field, value, generator);

  if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
    if (singleLineMode) {
      generator.print("} ");
    } else {
      generator.outdent();
      generator.print("}\n");
    }
  } else {
    if (singleLineMode) {
      generator.print(" ");
    } else {
      generator.print("\n");
    }
  }
}
 
Example 18
Source File: TextFormat.java    From android-chromium with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void printSingleField(final FieldDescriptor field,
                              final Object value,
                              final TextGenerator generator)
                              throws IOException {
  if (field.isExtension()) {
    generator.print("[");
    // We special-case MessageSet elements for compatibility with proto1.
    if (field.getContainingType().getOptions().getMessageSetWireFormat()
        && (field.getType() == FieldDescriptor.Type.MESSAGE)
        && (field.isOptional())
        // object equality
        && (field.getExtensionScope() == field.getMessageType())) {
      generator.print(field.getMessageType().getFullName());
    } else {
      generator.print(field.getFullName());
    }
    generator.print("]");
  } else {
    if (field.getType() == FieldDescriptor.Type.GROUP) {
      // Groups must be serialized with their original capitalization.
      generator.print(field.getMessageType().getName());
    } else {
      generator.print(field.getName());
    }
  }

  if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
    if (singleLineMode) {
      generator.print(" { ");
    } else {
      generator.print(" {\n");
      generator.indent();
    }
  } else {
    generator.print(": ");
  }

  printFieldValue(field, value, generator);

  if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
    if (singleLineMode) {
      generator.print("} ");
    } else {
      generator.outdent();
      generator.print("}\n");
    }
  } else {
    if (singleLineMode) {
      generator.print(" ");
    } else {
      generator.print("\n");
    }
  }
}
 
Example 19
Source File: JsonFormat.java    From gsc-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static void printSingleField(FieldDescriptor field,
                                     Object value, JsonGenerator generator, boolean selfType) throws IOException {
    if (field.isExtension()) {
        generator.print("\"");
        // We special-case MessageSet elements for compatibility with proto1.
        if (field.getContainingType().getOptions().getMessageSetWireFormat()
                && (field.getType() == FieldDescriptor.Type.MESSAGE) && (field.isOptional())
                // object equality
                && (field.getExtensionScope() == field.getMessageType())) {
            generator.print(field.getMessageType().getFullName());
        } else {
            generator.print(field.getFullName());
        }
        generator.print("\"");
    } else {
        generator.print("\"");
        if (field.getType() == FieldDescriptor.Type.GROUP) {
            // Groups must be serialized with their original capitalization.
            generator.print(field.getMessageType().getName());
        } else {
            generator.print(field.getName());
        }
        generator.print("\"");
    }

    // Done with the name, on to the value

    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
        generator.print(": ");
        generator.indent();
    } else {
        generator.print(": ");
    }

    if (field.isRepeated()) {
        // Repeated field. Print each element.
        generator.print("[");
        for (Iterator<?> iter = ((List<?>) value).iterator(); iter.hasNext(); ) {
            printFieldValue(field, iter.next(), generator, selfType);
            if (iter.hasNext()) {
                generator.print(",");
            }
        }
        generator.print("]");
    } else {
        printFieldValue(field, value, generator, selfType);
        if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
            generator.outdent();
        }
    }
}
 
Example 20
Source File: TextFormat.java    From 365browser with Apache License 2.0 4 votes vote down vote up
private void printSingleField(final FieldDescriptor field,
                              final Object value,
                              final TextGenerator generator)
                              throws IOException {
  if (field.isExtension()) {
    generator.print("[");
    // We special-case MessageSet elements for compatibility with proto1.
    if (field.getContainingType().getOptions().getMessageSetWireFormat()
        && (field.getType() == FieldDescriptor.Type.MESSAGE)
        && (field.isOptional())
        // object equality
        && (field.getExtensionScope() == field.getMessageType())) {
      generator.print(field.getMessageType().getFullName());
    } else {
      generator.print(field.getFullName());
    }
    generator.print("]");
  } else {
    if (field.getType() == FieldDescriptor.Type.GROUP) {
      // Groups must be serialized with their original capitalization.
      generator.print(field.getMessageType().getName());
    } else {
      generator.print(field.getName());
    }
  }

  if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
    if (singleLineMode) {
      generator.print(" { ");
    } else {
      generator.print(" {\n");
      generator.indent();
    }
  } else {
    generator.print(": ");
  }

  printFieldValue(field, value, generator);

  if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
    if (singleLineMode) {
      generator.print("} ");
    } else {
      generator.outdent();
      generator.print("}\n");
    }
  } else {
    if (singleLineMode) {
      generator.print(" ");
    } else {
      generator.print("\n");
    }
  }
}