private <MType extends ExtendableMessage<MType>, BType extends ExtendableBuilder<MType, BType>> boolean resolveUninterpretedOptions( final BType optionsBuilder, final FieldDescriptor uninterpretedField) { ensureRegistry(); boolean anyUninterpetedOptionResolved = false; @SuppressWarnings("unchecked") final List<UninterpretedOption> options = new ArrayList<UninterpretedOption>( (List<UninterpretedOption>) optionsBuilder.getField(uninterpretedField)); for (final Iterator<UninterpretedOption> iterator = options.iterator(); iterator.hasNext();) { final UninterpretedOption option = iterator.next(); if (resolveUninterpretedOption(option, optionsBuilder)) { anyUninterpetedOptionResolved = true; iterator.remove(); } } if (options.isEmpty()) { optionsBuilder.clearField(uninterpretedField); } else { optionsBuilder.setField(uninterpretedField, options); } return anyUninterpetedOptionResolved; }
private <MType extends ExtendableMessage<MType>, BType extends ExtendableBuilder<MType, BType>> boolean resolveUninterpretedOption( final UninterpretedOptionOrBuilder option, final BType optionsBuilder) { if (option.getNameCount() == 0) { throw new IllegalArgumentException("custom option cannot be empty"); } if (!option.getName(0).getIsExtension()) { throw new IllegalArgumentException("custom option name should start with '('"); } final Map.Entry<FieldDescriptor, Object> fieldValue; if (option.getNameCount() == 1) { fieldValue = processUninterpretedOptionValue(option, optionsBuilder); } else { fieldValue = processUninterpretedOptionMessage(option, optionsBuilder); } if (fieldValue == null) { return false; } final FieldDescriptor field = fieldValue.getKey(); final Object value = fieldValue.getValue(); if (field == null || value == null) { // the UninterpretedOption is not resolved return false; } if (field.isRepeated()) { optionsBuilder.addRepeatedField(field, value); } else { optionsBuilder.setField(field, value); } return true; }
private static void mergeMessageSetExtensionFromBytes( ByteString rawBytes, ExtensionRegistry.ExtensionInfo extension, ExtensionRegistryLite extensionRegistry, Message.Builder builder, FieldSet<FieldDescriptor> extensions) throws IOException { FieldDescriptor field = extension.descriptor; boolean hasOriginalValue = hasOriginalMessage(builder, extensions, field); if (hasOriginalValue || ExtensionRegistryLite.isEagerlyParseMessageSets()) { // If the field already exists, we just parse the field. Message value = null; if (hasOriginalValue) { Message originalMessage = getOriginalMessage(builder, extensions, field); Message.Builder subBuilder= originalMessage.toBuilder(); subBuilder.mergeFrom(rawBytes, extensionRegistry); value = subBuilder.buildPartial(); } else { value = extension.defaultInstance.getParserForType() .parsePartialFrom(rawBytes, extensionRegistry); } setField(builder, extensions, field, value); } else { // Use LazyField to load MessageSet lazily. LazyField lazyField = new LazyField( extension.defaultInstance, extensionRegistry, rawBytes); if (builder != null) { // TODO(xiangl): it looks like this method can only be invoked by // ExtendableBuilder, but I'm not sure. So I double check the type of // builder here. It may be useless and need more investigation. if (builder instanceof ExtendableBuilder) { builder.setField(field, lazyField); } else { builder.setField(field, lazyField.getValue()); } } else { extensions.setField(field, lazyField); } } }
private <MType extends ExtendableMessage<MType>, BType extends ExtendableBuilder<MType, BType>> boolean buildOptions( final BType optionsBuilder) { final FieldDescriptor uninterpretedField = getFD(optionsBuilder, UNINTERPRETED_OPTION_FIELD_NUMBER); final boolean isUninterpretedEmpty = optionsBuilder.getRepeatedFieldCount(uninterpretedField) == 0; final boolean anyUninterpetedOptionResolved = !isUninterpretedEmpty && resolveUninterpretedOptions(optionsBuilder, uninterpretedField); final boolean isUnknownFieldsEmpty = optionsBuilder.getUnknownFields().asMap().isEmpty(); Map<FieldDescriptor, Object> allFields = null; if (isUninterpretedEmpty && isUnknownFieldsEmpty) { allFields = optionsBuilder.getAllFields(); if (allFields.isEmpty()) { return false; } } if (customOptionsAsExtensions) { if (!isUnknownFieldsEmpty) { reparseBuilder(optionsBuilder, ensureRegistry()); } } else { boolean anyExtension = anyUninterpetedOptionResolved; if (!anyUninterpetedOptionResolved) { if (allFields == null) { allFields = optionsBuilder.getAllFields(); } for (final FieldDescriptor fd : allFields.keySet()) { if (fd.isExtension()) { anyExtension = true; break; } } } if (anyExtension) { reparseBuilder(optionsBuilder, ExtensionRegistry.getEmptyRegistry()); } } return true; }