下面列出了com.google.protobuf.ExtensionRegistry#ExtensionInfo ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
}
}
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);
}
}
}
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);
}
}
}
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();
}
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;
}
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();
}
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;
}
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;
}
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();
}
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();
}
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;
}
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);
}
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);
}
/**
* 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);
}
}
/**
* 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);
}
}
}
/**
* 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);
}
}