Java Code Examples for com.google.protobuf.ExtensionRegistry#ExtensionInfo
The following examples show how to use
com.google.protobuf.ExtensionRegistry#ExtensionInfo .
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: JsonFormat.java From gsc-core with GNU Lesser General Public License v3.0 | 6 votes |
private static void handleValue(Tokenizer tokenizer, ExtensionRegistry extensionRegistry, Message.Builder builder, FieldDescriptor field, ExtensionRegistry.ExtensionInfo extension, boolean unknown, boolean selfType) throws ParseException { Object value = null; if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { value = handleObject(tokenizer, extensionRegistry, builder, field, extension, unknown, selfType); } else { value = handlePrimitive(tokenizer, field, selfType); } if (value != null) { if (field.isRepeated()) { builder.addRepeatedField(field, value); } else { builder.setField(field, value); } } }
Example 2
Source File: JsonJacksonFormat.java From jigsaw-payment with Apache License 2.0 | 6 votes |
private void handleValue(JsonParser parser, ExtensionRegistry extensionRegistry, Message.Builder builder, FieldDescriptor field, ExtensionRegistry.ExtensionInfo extension, boolean unknown) throws IOException { Object value = null; if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { value = handleObject(parser, extensionRegistry, builder, field, extension, unknown); } else { value = handlePrimitive(parser, field); } if (value != null) { if (field.isRepeated()) { builder.addRepeatedField(field, value); } else { builder.setField(field, value); } } }
Example 3
Source File: JsonFormat.java From jigsaw-payment with Apache License 2.0 | 6 votes |
private void handleValue(Tokenizer tokenizer, ExtensionRegistry extensionRegistry, Message.Builder builder, FieldDescriptor field, ExtensionRegistry.ExtensionInfo extension, boolean unknown) throws ParseException { Object value = null; if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { value = handleObject(tokenizer, extensionRegistry, builder, field, extension, unknown); } else { value = handlePrimitive(tokenizer, field); } if (value != null) { if (field.isRepeated()) { builder.addRepeatedField(field, value); } else { builder.setField(field, value); } } }
Example 4
Source File: JsonFormat.java From gsc-core with GNU Lesser General Public License v3.0 | 5 votes |
private static Object handleObject(Tokenizer tokenizer, ExtensionRegistry extensionRegistry, Message.Builder builder, FieldDescriptor field, ExtensionRegistry.ExtensionInfo extension, boolean unknown, boolean selfType) throws ParseException { Message.Builder subBuilder; if (extension == null) { subBuilder = builder.newBuilderForField(field); } else { subBuilder = extension.defaultInstance.newBuilderForType(); } if (unknown) { ByteString data = tokenizer.consumeByteString("", selfType); try { subBuilder.mergeFrom(data); return subBuilder.build(); } catch (InvalidProtocolBufferException e) { throw tokenizer.parseException("Failed to build " + field.getFullName() + " from " + data); } } tokenizer.consume("{"); String endToken = "}"; while (!tokenizer.tryConsume(endToken)) { if (tokenizer.atEnd()) { throw tokenizer.parseException("Expected \"" + endToken + "\"."); } mergeField(tokenizer, extensionRegistry, subBuilder, selfType); if (tokenizer.tryConsume(",")) { // there are more fields in the object, so continue continue; } } return subBuilder.build(); }
Example 5
Source File: XmlJavaxFormat.java From jigsaw-payment with Apache License 2.0 | 5 votes |
private Message.Builder createSubBuilder(Message.Builder builder, FieldDescriptor field, ExtensionRegistry.ExtensionInfo extension) { Message.Builder subBuilder; if (extension == null) { subBuilder = builder.newBuilderForField(field); } else { subBuilder = extension.defaultInstance.newBuilderForType(); } return subBuilder; }
Example 6
Source File: XmlJavaxFormat.java From jigsaw-payment with Apache License 2.0 | 5 votes |
private Object handleObject(XMLEventReader parser, XMLEvent startEvent, ExtensionRegistry extensionRegistry, Message.Builder builder, FieldDescriptor field, ExtensionRegistry.ExtensionInfo extension) throws XMLStreamException { Message.Builder subBuilder = createSubBuilder(builder, field, extension); XMLEvent event = startEvent; int depth = 0; // initialize to 0 do { if (event.isStartElement()) { depth++; // we're inside the element mergeField(parser, event, extensionRegistry, subBuilder); XMLEvent nextEvent = parser.nextTag(); if (nextEvent.isEndElement()) { depth--; // if we're back under the top level obj, and there is another close, we're done. if (depth <= 0 && parser.peek().isEndElement()) { break; } } else if (nextEvent.isStartElement()) { depth++; } } else { // something is potentially wrong.. break; } } while (parser.hasNext() && (event = parser.nextTag()) != null); return subBuilder.build(); }
Example 7
Source File: XmlFormat.java From jigsaw-payment with Apache License 2.0 | 5 votes |
private Object handleValue(Tokenizer tokenizer, ExtensionRegistry extensionRegistry, Message.Builder builder, FieldDescriptor field, ExtensionRegistry.ExtensionInfo extension) throws ParseException { Object value = null; if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { value = handleObject(tokenizer, extensionRegistry, builder, field, extension); } else { value = handlePrimitive(tokenizer, field); } return value; }
Example 8
Source File: XmlFormat.java From jigsaw-payment with Apache License 2.0 | 5 votes |
private Object handleObject(Tokenizer tokenizer, ExtensionRegistry extensionRegistry, Message.Builder builder, FieldDescriptor field, ExtensionRegistry.ExtensionInfo extension) throws ParseException { Object value; Message.Builder subBuilder; if (extension == null) { subBuilder = builder.newBuilderForField(field); } else { subBuilder = extension.defaultInstance.newBuilderForType(); } //tokenizer.consume("<"); String endToken = "</"; while (!tokenizer.tryConsume(endToken)) { if (tokenizer.atEnd()) { throw tokenizer.parseException("Expected \"" + endToken + "\"."); } mergeField(tokenizer, extensionRegistry, subBuilder); } value = subBuilder.build(); return value; }
Example 9
Source File: JsonJacksonFormat.java From jigsaw-payment with Apache License 2.0 | 5 votes |
private Object handleObject(JsonParser parser, ExtensionRegistry extensionRegistry, Message.Builder builder, FieldDescriptor field, ExtensionRegistry.ExtensionInfo extension, boolean unknown) throws IOException { Message.Builder subBuilder; if (extension == null) { subBuilder = builder.newBuilderForField(field); } else { subBuilder = extension.defaultInstance.newBuilderForType(); } JsonToken token = parser.getCurrentToken(); if (JsonToken.VALUE_NULL == token) { return null; } if (unknown) { ByteString data = ByteString.copyFrom(parser.getBinaryValue()); try { subBuilder.mergeFrom(data); return subBuilder.build(); } catch (InvalidProtocolBufferException e) { throw new RuntimeException("Failed to build " + field.getFullName() + " from " + data); } } //token = parser.nextToken(); if (token.equals(JsonToken.START_OBJECT)) { token = parser.nextToken(); while (token != null && !token.equals(JsonToken.END_OBJECT)) { mergeField(parser, extensionRegistry, subBuilder); token = parser.nextToken(); } } return subBuilder.build(); }
Example 10
Source File: JsonFormat.java From jigsaw-payment with Apache License 2.0 | 5 votes |
private Object handleObject(Tokenizer tokenizer, ExtensionRegistry extensionRegistry, Message.Builder builder, FieldDescriptor field, ExtensionRegistry.ExtensionInfo extension, boolean unknown) throws ParseException { Message.Builder subBuilder; if (extension == null) { subBuilder = builder.newBuilderForField(field); } else { subBuilder = extension.defaultInstance.newBuilderForType(); } if (unknown) { ByteString data = tokenizer.consumeByteString(); try { subBuilder.mergeFrom(data); return subBuilder.build(); } catch (InvalidProtocolBufferException e) { throw tokenizer.parseException("Failed to build " + field.getFullName() + " from " + data); } } tokenizer.consume("{"); String endToken = "}"; while (!tokenizer.tryConsume(endToken)) { if (tokenizer.atEnd()) { throw tokenizer.parseException("Expected \"" + endToken + "\"."); } mergeField(tokenizer, extensionRegistry, subBuilder); if (tokenizer.tryConsume(",")) { // there are more fields in the object, so continue continue; } } return subBuilder.build(); }
Example 11
Source File: ConfigGeneratorDriver.java From api-compiler with Apache License 2.0 | 5 votes |
private TypeRegistry.Builder addPlatformExtensions(TypeRegistry.Builder registryBuilder) { ExtensionRegistry extensions = ToolProtoUtil.getStandardPlatformExtensions(); for (String extendedType : EXTENDED_ELEMENTS) { for (ExtensionRegistry.ExtensionInfo info : extensions.getAllImmutableExtensionsByExtendedType(extendedType)) { if (null != info.defaultInstance) { registryBuilder.add(info.defaultInstance.getDescriptorForType()); } } } return registryBuilder; }
Example 12
Source File: CompatibilityTest.java From j2objc with Apache License 2.0 | 5 votes |
public void testExtensionRegistry() throws Exception { ExtensionRegistry registry = ExtensionRegistry.newInstance(); Typical.registerAllExtensions(registry); Descriptor descriptor = TypicalData.Builder.getDescriptor(); FieldDescriptor fieldDescriptor = descriptor.findFieldByNumber(1); assertFalse(fieldDescriptor.isExtension()); ExtensionRegistry.ExtensionInfo extensionInfo = registry.findExtensionByNumber(descriptor, 1000); assertNotNull(extensionInfo); FieldDescriptor extensionFieldDescriptor = extensionInfo.descriptor; assertNotNull(extensionFieldDescriptor); assertEquals(1000, extensionFieldDescriptor.getNumber()); assertTrue(extensionFieldDescriptor.isExtension()); Message message = extensionInfo.defaultInstance; assertTrue(message instanceof TypicalDataMessage); TypicalDataMessage data = ((TypicalDataMessage.Builder) message.toBuilder()) .setMyMessageInt(100) .build(); assertEquals(100, data.getMyMessageInt()); // Primitive extension extensionInfo = registry.findExtensionByNumber(descriptor, 1001); assertNotNull(extensionInfo); extensionFieldDescriptor = extensionInfo.descriptor; assertNotNull(extensionFieldDescriptor); assertEquals(1001, extensionFieldDescriptor.getNumber()); assertTrue(extensionFieldDescriptor.isExtension()); assertNull(extensionInfo.defaultInstance); }
Example 13
Source File: CompatibilityTest.java From j2objc with Apache License 2.0 | 5 votes |
public void testExtensionRegistryGetUnmodifiable() throws Exception { ExtensionRegistry registry = ExtensionRegistry.newInstance(); ExtensionRegistry registry2 = registry.getUnmodifiable(); registry.add(Typical.myPrimitiveExtension); // Extension added to registry should be visible in registry2. Descriptor descriptor = TypicalData.getDescriptor(); ExtensionRegistry.ExtensionInfo extensionInfo = registry2.findExtensionByNumber(descriptor, 1001); assertNotNull(extensionInfo); ExtensionRegistryLite registryLite = ExtensionRegistryLite.newInstance(); ExtensionRegistryLite registryLite2 = registryLite.getUnmodifiable(); assertNotNull(registryLite2); }
Example 14
Source File: JsonFormat.java From gsc-core with GNU Lesser General Public License v3.0 | 4 votes |
/** * Parse a single field from {@code tokenizer} and merge it into {@code builder}. If a ',' is * detected after the field ends, the next field will be parsed automatically */ protected static void mergeField(Tokenizer tokenizer, ExtensionRegistry extensionRegistry, Message.Builder builder, boolean selfType) throws ParseException { FieldDescriptor field; Descriptor type = builder.getDescriptorForType(); final ExtensionRegistry.ExtensionInfo extension; boolean unknown = false; String name = tokenizer.consumeIdentifier(); field = type.findFieldByName(name); // Group names are expected to be capitalized as they appear in the // .proto file, which actually matches their type names, not their field // names. if (field == null) { // Explicitly specify US locale so that this code does not break when // executing in Turkey. String lowerName = name.toLowerCase(Locale.US); field = type.findFieldByName(lowerName); // If the case-insensitive match worked but the field is NOT a group, if ((field != null) && (field.getType() != FieldDescriptor.Type.GROUP)) { field = null; } } // Again, special-case group names as described above. if ((field != null) && (field.getType() == FieldDescriptor.Type.GROUP) && !field.getMessageType().getName().equals(name)) { field = null; } // Last try to lookup by field-index if 'name' is numeric, // which indicates a possible unknown field if (field == null && DIGITS.matcher(name).matches()) { field = type.findFieldByNumber(Integer.parseInt(name)); unknown = true; } // Finally, look for extensions extension = extensionRegistry.findExtensionByName(name); if (extension != null) { if (extension.descriptor.getContainingType() != type) { throw tokenizer.parseExceptionPreviousToken("Extension \"" + name + "\" does not extend message type \"" + type.getFullName() + "\"."); } field = extension.descriptor; } // Disabled throwing exception if field not found, since it could be a different version. if (field == null) { handleMissingField(tokenizer, extensionRegistry, builder); //throw tokenizer.parseExceptionPreviousToken("Message type \"" + type.getFullName() // + "\" has no field named \"" + name // + "\"."); } if (field != null) { tokenizer.consume(":"); boolean array = tokenizer.tryConsume("["); if (array) { while (!tokenizer.tryConsume("]")) { handleValue(tokenizer, extensionRegistry, builder, field, extension, unknown, selfType); tokenizer.tryConsume(","); } } else { handleValue(tokenizer, extensionRegistry, builder, field, extension, unknown, selfType); } } if (tokenizer.tryConsume(",")) { // Continue with the next field mergeField(tokenizer, extensionRegistry, builder, selfType); } }
Example 15
Source File: JsonJacksonFormat.java From jigsaw-payment with Apache License 2.0 | 4 votes |
/** * Parse a single field from {@code parser} and merge it into {@code builder}. If a ',' is * detected after the field ends, the next field will be parsed automatically * @throws IOException * @throws JsonParseException */ protected void mergeField(JsonParser parser, ExtensionRegistry extensionRegistry, Message.Builder builder) throws JsonParseException, IOException { FieldDescriptor field = null; Descriptor type = builder.getDescriptorForType(); boolean unknown = false; ExtensionRegistry.ExtensionInfo extension = null; JsonToken token = parser.getCurrentToken(); if (token != null) { String name = parser.getCurrentName(); if (name.contains(".")) { // should be an extension extension = extensionRegistry.findExtensionByName(name); if (extension == null) { throw new RuntimeException("Extension \"" + name + "\" not found in the ExtensionRegistry."); } else if (extension.descriptor.getContainingType() != type) { throw new RuntimeException("Extension \"" + name + "\" does not extend message type \"" + type.getFullName() + "\"."); } field = extension.descriptor; } else { field = type.findFieldByName(name); } // Group names are expected to be capitalized as they appear in the // .proto file, which actually matches their type names, not their field // names. if (field == null) { // Explicitly specify US locale so that this code does not break when // executing in Turkey. String lowerName = name.toLowerCase(Locale.US); field = type.findFieldByName(lowerName); // If the case-insensitive match worked but the field is NOT a group, if ((field != null) && (field.getType() != FieldDescriptor.Type.GROUP)) { field = null; } } // Again, special-case group names as described above. if ((field != null) && (field.getType() == FieldDescriptor.Type.GROUP) && !field.getMessageType().getName().equals(name) && !field.getMessageType().getFullName().equalsIgnoreCase(name) /* extension */) { field = null; } // Last try to lookup by field-index if 'name' is numeric, // which indicates a possible unknown field if (field == null && TextUtils.isDigits(name)) { field = type.findFieldByNumber(Integer.parseInt(name)); unknown = true; } // no throwing exceptions if field not found, since it could be a different version. if (field == null) { UnknownFieldSet.Builder unknownsBuilder = UnknownFieldSet.newBuilder(); handleMissingField(name, parser, extensionRegistry, unknownsBuilder); builder.setUnknownFields(unknownsBuilder.build()); } } if (field != null) { token = parser.nextToken(); boolean array = token.equals(JsonToken.START_ARRAY); if (array) { token = parser.nextToken(); while (!token.equals(JsonToken.END_ARRAY)) { handleValue(parser, extensionRegistry, builder, field, extension, unknown); token = parser.nextToken(); } } else { handleValue(parser, extensionRegistry, builder, field, extension, unknown); } } }
Example 16
Source File: JsonFormat.java From jigsaw-payment with Apache License 2.0 | 4 votes |
/** * Parse a single field from {@code tokenizer} and merge it into {@code builder}. If a ',' is * detected after the field ends, the next field will be parsed automatically */ protected void mergeField(Tokenizer tokenizer, ExtensionRegistry extensionRegistry, Message.Builder builder) throws ParseException { FieldDescriptor field; Descriptor type = builder.getDescriptorForType(); ExtensionRegistry.ExtensionInfo extension = null; boolean unknown = false; String name = tokenizer.consumeIdentifier(); field = type.findFieldByName(name); // Group names are expected to be capitalized as they appear in the // .proto file, which actually matches their type names, not their field // names. if (field == null) { // Explicitly specify US locale so that this code does not break when // executing in Turkey. String lowerName = name.toLowerCase(Locale.US); field = type.findFieldByName(lowerName); // If the case-insensitive match worked but the field is NOT a group, if ((field != null) && (field.getType() != FieldDescriptor.Type.GROUP)) { field = null; } } // Again, special-case group names as described above. if ((field != null) && (field.getType() == FieldDescriptor.Type.GROUP) && !field.getMessageType().getName().equals(name)) { field = null; } // Last try to lookup by field-index if 'name' is numeric, // which indicates a possible unknown field if (field == null && TextUtils.isDigits(name)) { field = type.findFieldByNumber(Integer.parseInt(name)); unknown = true; } // Finally, look for extensions extension = extensionRegistry.findExtensionByName(name); if (extension != null) { if (extension.descriptor.getContainingType() != type) { throw tokenizer.parseExceptionPreviousToken("Extension \"" + name + "\" does not extend message type \"" + type.getFullName() + "\"."); } field = extension.descriptor; } // Disabled throwing exception if field not found, since it could be a different version. if (field == null) { handleMissingField(tokenizer, extensionRegistry, builder); //throw tokenizer.parseExceptionPreviousToken("Message type \"" + type.getFullName() // + "\" has no field named \"" + name // + "\"."); } if (field != null) { tokenizer.consume(":"); boolean array = tokenizer.tryConsume("["); if (array) { while (!tokenizer.tryConsume("]")) { handleValue(tokenizer, extensionRegistry, builder, field, extension, unknown); tokenizer.tryConsume(","); } } else { handleValue(tokenizer, extensionRegistry, builder, field, extension, unknown); } } if (tokenizer.tryConsume(",")) { // Continue with the next field mergeField(tokenizer, extensionRegistry, builder); } }