31 package com.google.protobuf;
41 import java.io.IOException;
42 import java.io.InputStream;
43 import java.io.ObjectStreamException;
44 import java.io.Serializable;
47 import java.nio.ByteBuffer;
48 import java.util.ArrayList;
49 import java.util.Collections;
50 import java.util.Iterator;
51 import java.util.List;
53 import java.util.concurrent.ConcurrentHashMap;
72 @SuppressWarnings(
"unchecked")
78 @SuppressWarnings(
"unchecked")
80 return (MessageType)
dynamicMethod(MethodToInvoke.GET_DEFAULT_INSTANCE);
84 @SuppressWarnings(
"unchecked")
86 return (BuilderType)
dynamicMethod(MethodToInvoke.NEW_BUILDER);
103 return MessageLiteToString.toString(
this, super.toString());
106 @SuppressWarnings(
"unchecked")
116 @SuppressWarnings(
"unchecked")
127 return Protobuf.getInstance().schemaFor(
this).equals(
this, (MessageType) other);
170 Protobuf.getInstance().schemaFor(
this).makeImmutable(
this);
177 return (BuilderType)
dynamicMethod(MethodToInvoke.NEW_BUILDER);
193 @SuppressWarnings(
"unchecked")
195 BuilderType builder = (BuilderType)
dynamicMethod(MethodToInvoke.NEW_BUILDER);
196 builder.mergeFrom((MessageType)
this);
255 int getMemoizedSerializedSize() {
260 void setMemoizedSerializedSize(
int size) {
265 Protobuf.getInstance()
267 .writeTo(
this, CodedOutputStreamWriter.forCodedOutput(
output));
278 Object buildMessageInfo() throws Exception {
283 new ConcurrentHashMap<Object, GeneratedMessageLite<?, ?>>();
285 @SuppressWarnings(
"unchecked")
288 if (result ==
null) {
292 Class.forName(clazz.getName(),
true, clazz.getClassLoader());
293 }
catch (ClassNotFoundException e) {
294 throw new IllegalStateException(
"Class initialization cannot fail.", e);
298 if (result ==
null) {
301 result = (
T) UnsafeUtil.allocateInstance(clazz).getDefaultInstanceForType();
303 if (result ==
null) {
304 throw new IllegalStateException();
312 Class<T> clazz,
T defaultInstance) {
317 MessageLite defaultInstance, String info, Object[] objects) {
318 return new RawMessageInfo(defaultInstance, info, objects);
330 @SuppressWarnings(
"unchecked")
333 BuilderType extends
Builder<MessageType, BuilderType>>
340 protected Builder(MessageType defaultInstance) {
341 this.defaultInstance = defaultInstance;
353 MessageType newInstance =
355 mergeFromInstance(newInstance, instance);
356 instance = newInstance;
370 return (BuilderType)
this;
376 builder.mergeFrom(buildPartial());
386 instance.makeImmutable();
394 MessageType result = buildPartial();
395 if (!result.isInitialized()) {
396 throw newUninitializedMessageException(result);
409 mergeFromInstance(instance,
message);
410 return (BuilderType)
this;
414 Protobuf.getInstance().schemaFor(
dest).mergeFrom(
dest,
src);
419 return defaultInstance;
428 Protobuf.getInstance().schemaFor(instance).mergeFrom(
430 new ArrayDecoders.Registers(extensionRegistry));
433 }
catch (IndexOutOfBoundsException e) {
435 }
catch (IOException e) {
436 throw new RuntimeException(
"Reading from byte array should not throw IOException.", e);
438 return (BuilderType)
this;
457 Protobuf.getInstance().schemaFor(instance).mergeFrom(
458 instance, CodedInputStreamReader.forCodedInput(
input), extensionRegistry);
459 }
catch (RuntimeException e) {
460 if (e.getCause() instanceof IOException) {
461 throw (IOException) e.getCause();
465 return (BuilderType)
this;
500 protected FieldSet<ExtensionDescriptor>
extensions = FieldSet.emptySet();
502 @SuppressWarnings(
"unchecked")
517 protected <MessageType extends MessageLite>
boolean parseUnknownField(
518 MessageType defaultInstance,
528 extensionRegistry.findLiteExtensionByNumber(defaultInstance, fieldNumber);
541 boolean unknown =
false;
542 boolean packed =
false;
546 == FieldSet.getWireFormatForFieldType(
547 extension.descriptor.getLiteType(),
false)) {
549 }
else if (
extension.descriptor.isRepeated
552 == FieldSet.getWireFormatForFieldType(
553 extension.descriptor.getLiteType(),
true)) {
560 return parseUnknownField(tag,
input);
563 ensureExtensionsAreMutable();
569 while (
input.getBytesUntilLimit() > 0) {
570 int rawValue =
input.readEnum();
571 Object
value =
extension.descriptor.getEnumType().findValueByNumber(rawValue);
581 while (
input.getBytesUntilLimit() > 0) {
583 FieldSet.readPrimitiveField(
588 input.popLimit(limit);
591 switch (
extension.descriptor.getLiteJavaType()) {
595 if (!
extension.descriptor.isRepeated()) {
597 if (existingValue !=
null) {
601 if (subBuilder ==
null) {
602 subBuilder =
extension.getMessageDefaultInstance().newBuilderForType();
605 input.readGroup(
extension.getNumber(), subBuilder, extensionRegistry);
607 input.readMessage(subBuilder, extensionRegistry);
609 value = subBuilder.build();
613 int rawValue =
input.readEnum();
614 value =
extension.descriptor.getEnumType().findValueByNumber(rawValue);
624 FieldSet.readPrimitiveField(
646 protected <MessageType extends MessageLite>
boolean parseUnknownFieldAsMessageSet(
647 MessageType defaultInstance,
654 mergeMessageSetExtensionFromCodedStream(defaultInstance,
input, extensionRegistry);
662 return parseUnknownField(defaultInstance,
input, extensionRegistry, tag);
665 return input.skipField(tag);
676 private <MessageType extends MessageLite>
void mergeMessageSetExtensionFromCodedStream(
677 MessageType defaultInstance,
678 CodedInputStream
input,
679 ExtensionRegistryLite extensionRegistry)
698 ByteString rawBytes =
null;
699 GeneratedExtension<?, ?>
extension =
null;
704 final int tag =
input.readTag();
709 if (tag == WireFormat.MESSAGE_SET_TYPE_ID_TAG) {
710 typeId =
input.readUInt32();
712 extension = extensionRegistry.findLiteExtensionByNumber(defaultInstance, typeId);
715 }
else if (tag == WireFormat.MESSAGE_SET_MESSAGE_TAG) {
726 rawBytes =
input.readBytes();
729 if (!
input.skipField(tag)) {
734 input.checkLastTagWas(WireFormat.MESSAGE_SET_ITEM_END_TAG);
737 if (rawBytes !=
null && typeId != 0) {
741 if (rawBytes !=
null) {
754 int fieldNumber = typeId;
766 if (existingValue !=
null) {
769 if (subBuilder ==
null) {
770 subBuilder =
extension.getMessageDefaultInstance().newBuilderForType();
772 subBuilder.mergeFrom(rawBytes, extensionRegistry);
775 ensureExtensionsAreMutable()
779 FieldSet<ExtensionDescriptor> ensureExtensionsAreMutable() {
789 throw new IllegalArgumentException(
790 "This extension is for a different message type. Please make "
791 +
"sure that you are not suppressing any generics type warnings.");
801 return extensions.hasField(extensionLite.descriptor);
811 return extensions.getRepeatedFieldCount(extensionLite.descriptor);
816 @SuppressWarnings(
"unchecked")
823 return extensionLite.defaultValue;
825 return (
Type) extensionLite.fromFieldSetType(
value);
831 @SuppressWarnings(
"unchecked")
838 extensionLite.singularFromFieldSetType(
857 private Map.Entry<ExtensionDescriptor, Object>
next;
861 if (
iter.hasNext()) {
868 while (
next !=
null &&
next.getKey().getNumber() <
end) {
877 if (
iter.hasNext()) {
900 return extensions.getMessageSetSerializedSize();
905 @SuppressWarnings(
"unchecked")
909 extends
Builder<MessageType, BuilderType>
912 super(defaultInstance);
916 void internalSetExtensionSet(FieldSet<ExtensionDescriptor> extensions) {
918 instance.extensions = extensions;
928 instance.extensions = instance.extensions.clone();
932 FieldSet<ExtensionDescriptor> extensions = instance.extensions;
933 if (extensions.isImmutable()) {
934 extensions = extensions.clone();
935 instance.extensions = extensions;
946 instance.extensions.makeImmutable();
947 return super.buildPartial();
953 throw new IllegalArgumentException(
954 "This extension is for a different message type. Please make "
955 +
"sure that you are not suppressing any generics type warnings.");
969 return instance.getExtensionCount(
extension);
974 @SuppressWarnings(
"unchecked")
981 @SuppressWarnings(
"unchecked")
992 verifyExtensionContainingType(extensionLite);
994 ensureExtensionsAreMutable()
995 .setField(extensionLite.descriptor, extensionLite.toFieldSetType(
value));
996 return (BuilderType)
this;
1004 verifyExtensionContainingType(extensionLite);
1006 ensureExtensionsAreMutable()
1008 extensionLite.descriptor,
index, extensionLite.singularToFieldSetType(
value));
1009 return (BuilderType)
this;
1017 verifyExtensionContainingType(extensionLite);
1019 ensureExtensionsAreMutable()
1020 .addRepeatedField(extensionLite.descriptor, extensionLite.singularToFieldSetType(
value));
1021 return (BuilderType)
this;
1028 verifyExtensionContainingType(extensionLite);
1030 ensureExtensionsAreMutable().clearField(extensionLite.descriptor);
1031 return (BuilderType)
this;
1038 public static <ContainingType extends MessageLite, Type>
1040 final ContainingType containingTypeDefaultInstance,
1041 final Type defaultValue,
1046 final Class singularType) {
1048 containingTypeDefaultInstance,
1050 messageDefaultInstance,
1051 new ExtensionDescriptor(
1057 public static <ContainingType extends MessageLite, Type>
1059 final ContainingType containingTypeDefaultInstance,
1064 final boolean isPacked,
1065 final Class singularType) {
1066 @SuppressWarnings(
"unchecked")
1067 Type emptyList = (
Type) Collections.emptyList();
1069 containingTypeDefaultInstance,
1071 messageDefaultInstance,
1072 new ExtensionDescriptor(enumTypeMap,
number,
type,
true, isPacked),
1076 static final class ExtensionDescriptor
1078 ExtensionDescriptor(
1082 final boolean isRepeated,
1083 final boolean isPacked) {
1084 this.enumTypeMap = enumTypeMap;
1087 this.isRepeated = isRepeated;
1088 this.isPacked = isPacked;
1091 final Internal.EnumLiteMap<?> enumTypeMap;
1093 final WireFormat.FieldType
type;
1094 final boolean isRepeated;
1095 final boolean isPacked;
1098 public int getNumber() {
1103 public WireFormat.FieldType getLiteType() {
1108 public WireFormat.JavaType getLiteJavaType() {
1109 return type.getJavaType();
1113 public boolean isRepeated() {
1118 public boolean isPacked() {
1123 public Internal.EnumLiteMap<?> getEnumType() {
1128 @SuppressWarnings(
"unchecked")
1129 public MessageLite.
Builder internalMergeFrom(MessageLite.
Builder to, MessageLite from) {
1130 return ((
Builder) to).mergeFrom((GeneratedMessageLite) from);
1135 public int compareTo(ExtensionDescriptor other) {
1136 return number - other.number;
1143 @SuppressWarnings(
"unchecked")
1147 }
catch (NoSuchMethodException e) {
1148 throw new RuntimeException(
1149 "Generated message class \"" + clazz.getName() +
"\" missing method \"" +
name +
"\".",
1158 }
catch (IllegalAccessException e) {
1159 throw new RuntimeException(
1160 "Couldn't use Java reflection to implement protocol message reflection.", e);
1161 }
catch (InvocationTargetException e) {
1162 final Throwable cause = e.getCause();
1163 if (cause instanceof RuntimeException) {
1164 throw (RuntimeException) cause;
1165 }
else if (cause instanceof Error) {
1166 throw (Error) cause;
1168 throw new RuntimeException(
1169 "Unexpected exception thrown by generated accessor method.", cause);
1192 final ContainingType containingTypeDefaultInstance,
1193 final Type defaultValue,
1195 final ExtensionDescriptor descriptor,
1196 final Class singularType) {
1199 if (containingTypeDefaultInstance ==
null) {
1200 throw new IllegalArgumentException(
"Null containingTypeDefaultInstance");
1203 && messageDefaultInstance ==
null) {
1204 throw new IllegalArgumentException(
"Null messageDefaultInstance");
1206 this.containingTypeDefaultInstance = containingTypeDefaultInstance;
1207 this.defaultValue = defaultValue;
1208 this.messageDefaultInstance = messageDefaultInstance;
1212 final ContainingType containingTypeDefaultInstance;
1213 final Type defaultValue;
1219 return containingTypeDefaultInstance;
1234 return messageDefaultInstance;
1237 @SuppressWarnings(
"unchecked")
1238 Object fromFieldSetType(final Object
value) {
1241 final List result =
new ArrayList();
1242 for (
final Object element : (List)
value) {
1243 result.add(singularFromFieldSetType(element));
1250 return singularFromFieldSetType(
value);
1254 Object singularFromFieldSetType(
final Object
value) {
1255 if (
descriptor.getLiteJavaType() == WireFormat.JavaType.ENUM) {
1262 @SuppressWarnings(
"unchecked")
1263 Object toFieldSetType(final Object
value) {
1265 if (
descriptor.getLiteJavaType() == WireFormat.JavaType.ENUM) {
1266 final List result =
new ArrayList();
1267 for (
final Object element : (List)
value) {
1268 result.add(singularToFieldSetType(element));
1275 return singularToFieldSetType(
value);
1279 Object singularToFieldSetType(
final Object
value) {
1280 if (
descriptor.getLiteJavaType() == WireFormat.JavaType.ENUM) {
1281 return ((Internal.EnumLite)
value).getNumber();
1299 return defaultValue;
1338 @SuppressWarnings(
"unchecked")
1344 defaultInstanceField.setAccessible(
true);
1347 }
catch (ClassNotFoundException e) {
1348 throw new RuntimeException(
"Unable to find proto buffer class: " +
messageClassName, e);
1349 }
catch (NoSuchFieldException e) {
1351 }
catch (SecurityException e) {
1352 throw new RuntimeException(
"Unable to call DEFAULT_INSTANCE in " +
messageClassName, e);
1353 }
catch (IllegalAccessException e) {
1354 throw new RuntimeException(
"Unable to call parsePartialFrom", e);
1356 throw new RuntimeException(
"Unable to understand proto buffer", e);
1369 defaultInstanceField.setAccessible(
true);
1374 }
catch (ClassNotFoundException e) {
1375 throw new RuntimeException(
"Unable to find proto buffer class: " +
messageClassName, e);
1376 }
catch (NoSuchFieldException e) {
1377 throw new RuntimeException(
"Unable to find defaultInstance in " +
messageClassName, e);
1378 }
catch (SecurityException e) {
1379 throw new RuntimeException(
"Unable to call defaultInstance in " +
messageClassName, e);
1380 }
catch (IllegalAccessException e) {
1381 throw new RuntimeException(
"Unable to call parsePartialFrom", e);
1383 throw new RuntimeException(
"Unable to understand proto buffer", e);
1394 MessageType extends ExtendableMessage<MessageType, BuilderType>,
1399 throw new IllegalArgumentException(
"Expected a lite extension.");
1410 protected static final <T extends GeneratedMessageLite<T, ?>>
boolean isInitialized(
1411 T message,
boolean shouldMemoize) {
1412 byte memoizedIsInitialized =
1413 (Byte)
message.dynamicMethod(MethodToInvoke.GET_MEMOIZED_IS_INITIALIZED);
1414 if (memoizedIsInitialized == 1) {
1417 if (memoizedIsInitialized == 0) {
1421 if (shouldMemoize) {
1429 return IntArrayList.emptyList();
1433 int size = list.size();
1435 size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY :
size * 2);
1439 return LongArrayList.emptyList();
1443 int size = list.size();
1445 size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY :
size * 2);
1449 return FloatArrayList.emptyList();
1453 int size = list.size();
1455 size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY :
size * 2);
1459 return DoubleArrayList.emptyList();
1463 int size = list.size();
1465 size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY :
size * 2);
1469 return BooleanArrayList.emptyList();
1473 int size = list.size();
1475 size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY :
size * 2);
1479 return ProtobufArrayList.emptyList();
1483 int size = list.size();
1485 size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY :
size * 2);
1522 static <T extends GeneratedMessageLite<T, ?>>
T parsePartialFrom(
1525 @SuppressWarnings(
"unchecked")
1526 T result = (
T) instance.
dynamicMethod(MethodToInvoke.NEW_MUTABLE_INSTANCE);
1530 Schema<T> schema = Protobuf.getInstance().schemaFor(result);
1531 schema.mergeFrom(result, CodedInputStreamReader.forCodedInput(
input), extensionRegistry);
1532 schema.makeImmutable(result);
1533 }
catch (IOException e) {
1534 if (e.getCause() instanceof InvalidProtocolBufferException) {
1535 throw (InvalidProtocolBufferException) e.getCause();
1537 throw new InvalidProtocolBufferException(e.getMessage()).setUnfinishedMessage(result);
1538 }
catch (RuntimeException e) {
1539 if (e.getCause() instanceof InvalidProtocolBufferException) {
1540 throw (InvalidProtocolBufferException) e.getCause();
1548 static <T extends GeneratedMessageLite<T, ?>>
T parsePartialFrom(
1549 T instance,
byte[]
input,
int offset,
int length, ExtensionRegistryLite extensionRegistry)
1550 throws InvalidProtocolBufferException {
1551 @SuppressWarnings(
"unchecked")
1552 T result = (
T) instance.
dynamicMethod(MethodToInvoke.NEW_MUTABLE_INSTANCE);
1554 Schema<T> schema = Protobuf.getInstance().schemaFor(result);
1557 schema.makeImmutable(result);
1558 if (result.memoizedHashCode != 0) {
1559 throw new RuntimeException();
1561 }
catch (IOException e) {
1562 if (e.getCause() instanceof InvalidProtocolBufferException) {
1563 throw (InvalidProtocolBufferException) e.getCause();
1565 throw new InvalidProtocolBufferException(e.getMessage()).setUnfinishedMessage(result);
1566 }
catch (IndexOutOfBoundsException e) {
1567 throw InvalidProtocolBufferException.truncatedMessage().setUnfinishedMessage(result);
1587 .newUninitializedMessageException()
1588 .asInvalidProtocolBufferException()
1595 protected static <T extends GeneratedMessageLite<T, ?>>
T parseFrom(
1603 protected static <T extends GeneratedMessageLite<T, ?>>
T parseFrom(
1609 protected static <T extends GeneratedMessageLite<T, ?>>
T parseFrom(
1616 protected static <T extends GeneratedMessageLite<T, ?>>
T parseFrom(
1630 message = parsePartialFrom(defaultInstance,
input, extensionRegistry);
1632 input.checkLastTagWas(0);
1648 parsePartialFrom(defaultInstance,
data, 0,
data.length, extensionRegistry));
1652 protected static <T extends GeneratedMessageLite<T, ?>>
T parseFrom(
1659 protected static <T extends GeneratedMessageLite<T, ?>>
T parseFrom(
1663 parsePartialFrom(defaultInstance,
data, 0,
data.length, extensionRegistry));
1667 protected static <T extends GeneratedMessageLite<T, ?>>
T parseFrom(
1677 protected static <T extends GeneratedMessageLite<T, ?>>
T parseFrom(
1685 protected static <T extends GeneratedMessageLite<T, ?>>
T parseFrom(
1691 protected static <T extends GeneratedMessageLite<T, ?>>
T parseFrom(
1718 int firstByte =
input.read();
1719 if (firstByte == -1) {
1723 }
catch (IOException e) {
1726 InputStream limitedInput =
new LimitedInputStream(
input,
size);
1728 T message = parsePartialFrom(defaultInstance, codedInput, extensionRegistry);