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 <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; }