OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 18 matching lines...) Expand all Loading... |
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
31 package com.google.protobuf; | 31 package com.google.protobuf; |
32 | 32 |
33 import com.google.protobuf.Descriptors.Descriptor; | 33 import com.google.protobuf.Descriptors.Descriptor; |
34 import com.google.protobuf.Descriptors.EnumDescriptor; | 34 import com.google.protobuf.Descriptors.EnumDescriptor; |
35 import com.google.protobuf.Descriptors.EnumValueDescriptor; | 35 import com.google.protobuf.Descriptors.EnumValueDescriptor; |
36 import com.google.protobuf.Descriptors.FieldDescriptor; | 36 import com.google.protobuf.Descriptors.FieldDescriptor; |
37 import com.google.protobuf.Descriptors.FileDescriptor; | 37 import com.google.protobuf.Descriptors.FileDescriptor; |
38 import com.google.protobuf.Descriptors.OneofDescriptor; | 38 import com.google.protobuf.Descriptors.OneofDescriptor; |
| 39 // In opensource protobuf, we have versioned this GeneratedMessageV3 class to Ge
neratedMessageV3V3 and |
| 40 // in the future may have GeneratedMessageV3V4 etc. This allows us to change som
e aspects of this |
| 41 // class without breaking binary compatibility with old generated code that stil
l subclasses |
| 42 // the old GeneratedMessageV3 class. To allow these different GeneratedMessageV3
V? classes to |
| 43 // interoperate (e.g., a GeneratedMessageV3V3 object has a message extension fie
ld whose class |
| 44 // type is GeneratedMessageV3V4), these classes still share a common parent clas
s AbstarctMessage |
| 45 // and are using the same GeneratedMessage.GeneratedExtension class for extensio
n definitions. |
| 46 // Since this class becomes GeneratedMessageV3V? in opensource, we have to add a
n import here |
| 47 // to be able to use GeneratedMessage.GeneratedExtension. The GeneratedExtension
definition in |
| 48 // this file is also excluded from opensource to avoid conflict. |
| 49 import com.google.protobuf.GeneratedMessage.GeneratedExtension; |
39 | 50 |
40 import java.io.IOException; | 51 import java.io.IOException; |
41 import java.io.InputStream; | 52 import java.io.InputStream; |
42 import java.io.ObjectStreamException; | 53 import java.io.ObjectStreamException; |
43 import java.io.Serializable; | 54 import java.io.Serializable; |
44 import java.lang.reflect.InvocationTargetException; | 55 import java.lang.reflect.InvocationTargetException; |
45 import java.lang.reflect.Method; | 56 import java.lang.reflect.Method; |
46 import java.util.ArrayList; | 57 import java.util.ArrayList; |
| 58 import java.util.Arrays; |
47 import java.util.Collections; | 59 import java.util.Collections; |
48 import java.util.Iterator; | 60 import java.util.Iterator; |
49 import java.util.List; | 61 import java.util.List; |
50 import java.util.Map; | 62 import java.util.Map; |
51 import java.util.TreeMap; | 63 import java.util.TreeMap; |
52 | 64 |
53 /** | 65 /** |
54 * All generated protocol message classes extend this class. This class | 66 * All generated protocol message classes extend this class. This class |
55 * implements most of the Message and Builder interfaces using Java reflection. | 67 * implements most of the Message and Builder interfaces using Java reflection. |
56 * Users can ignore this class and pretend that generated messages implement | 68 * Users can ignore this class and pretend that generated messages implement |
57 * the Message interface directly. | 69 * the Message interface directly. |
58 * | 70 * |
59 * @author kenton@google.com Kenton Varda | 71 * @author kenton@google.com Kenton Varda |
60 */ | 72 */ |
61 public abstract class GeneratedMessage extends AbstractMessage | 73 public abstract class GeneratedMessageV3 extends AbstractMessage |
62 implements Serializable { | 74 implements Serializable { |
63 private static final long serialVersionUID = 1L; | 75 private static final long serialVersionUID = 1L; |
64 | 76 |
65 /** | 77 /** |
66 * For testing. Allows a test to disable the optimization that avoids using | 78 * For testing. Allows a test to disable the optimization that avoids using |
67 * field builders for nested messages until they are requested. By disabling | 79 * field builders for nested messages until they are requested. By disabling |
68 * this optimization, existing tests can be reused to test the field builders. | 80 * this optimization, existing tests can be reused to test the field builders. |
69 */ | 81 */ |
70 protected static boolean alwaysUseFieldBuilders = false; | 82 protected static boolean alwaysUseFieldBuilders = false; |
71 | 83 |
72 /** For use by generated code only. */ | 84 /** For use by generated code only. */ |
73 protected UnknownFieldSet unknownFields; | 85 protected UnknownFieldSet unknownFields; |
74 | 86 |
75 protected GeneratedMessage() { | 87 protected GeneratedMessageV3() { |
76 unknownFields = UnknownFieldSet.getDefaultInstance(); | 88 unknownFields = UnknownFieldSet.getDefaultInstance(); |
77 } | 89 } |
78 | 90 |
79 protected GeneratedMessage(Builder<?> builder) { | 91 protected GeneratedMessageV3(Builder<?> builder) { |
80 unknownFields = builder.getUnknownFields(); | 92 unknownFields = builder.getUnknownFields(); |
81 } | 93 } |
82 | 94 |
83 @Override | 95 @Override |
84 public Parser<? extends GeneratedMessage> getParserForType() { | 96 public Parser<? extends GeneratedMessageV3> getParserForType() { |
85 throw new UnsupportedOperationException( | 97 throw new UnsupportedOperationException( |
86 "This is supposed to be overridden by subclasses."); | 98 "This is supposed to be overridden by subclasses."); |
87 } | 99 } |
88 | 100 |
89 /** | 101 /** |
90 * For testing. Allows a test to disable the optimization that avoids using | 102 * For testing. Allows a test to disable the optimization that avoids using |
91 * field builders for nested messages until they are requested. By disabling | 103 * field builders for nested messages until they are requested. By disabling |
92 * this optimization, existing tests can be reused to test the field builders. | 104 * this optimization, existing tests can be reused to test the field builders. |
93 * See {@link RepeatedFieldBuilder} and {@link SingleFieldBuilder}. | 105 * See {@link RepeatedFieldBuilder} and {@link SingleFieldBuilder}. |
94 */ | 106 */ |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 | 360 |
349 | 361 |
350 | 362 |
351 /** | 363 /** |
352 * Used by parsing constructors in generated classes. | 364 * Used by parsing constructors in generated classes. |
353 */ | 365 */ |
354 protected void makeExtensionsImmutable() { | 366 protected void makeExtensionsImmutable() { |
355 // Noop for messages without extensions. | 367 // Noop for messages without extensions. |
356 } | 368 } |
357 | 369 |
| 370 /** |
| 371 * TODO(xiaofeng): remove this after b/29368482 is fixed. We need to move this |
| 372 * interface to AbstractMessage in order to versioning GeneratedMessageV3 but |
| 373 * this move breaks binary compatibility for AppEngine. After AppEngine is |
| 374 * fixed we can exlude this from google3. |
| 375 */ |
| 376 protected interface BuilderParent extends AbstractMessage.BuilderParent {} |
| 377 |
| 378 /** |
| 379 * TODO(xiaofeng): remove this together with GeneratedMessageV3.BuilderParent. |
| 380 */ |
358 protected abstract Message.Builder newBuilderForType(BuilderParent parent); | 381 protected abstract Message.Builder newBuilderForType(BuilderParent parent); |
359 | 382 |
360 /** | 383 @Override |
361 * Interface for the parent of a Builder that allows the builder to | 384 protected Message.Builder newBuilderForType(final AbstractMessage.BuilderParen
t parent) { |
362 * communicate invalidations back to the parent for use when using nested | 385 return newBuilderForType(new BuilderParent() { |
363 * builders. | 386 @Override |
364 */ | 387 public void markDirty() { |
365 protected interface BuilderParent { | 388 parent.markDirty(); |
| 389 } |
| 390 }); |
| 391 } |
366 | 392 |
367 /** | |
368 * A builder becomes dirty whenever a field is modified -- including fields | |
369 * in nested builders -- and becomes clean when build() is called. Thus, | |
370 * when a builder becomes dirty, all its parents become dirty as well, and | |
371 * when it becomes clean, all its children become clean. The dirtiness | |
372 * state is used to invalidate certain cached values. | |
373 * <br> | |
374 * To this end, a builder calls markAsDirty() on its parent whenever it | |
375 * transitions from clean to dirty. The parent must propagate this call to | |
376 * its own parent, unless it was already dirty, in which case the | |
377 * grandparent must necessarily already be dirty as well. The parent can | |
378 * only transition back to "clean" after calling build() on all children. | |
379 */ | |
380 void markDirty(); | |
381 } | |
382 | 393 |
383 @SuppressWarnings("unchecked") | 394 @SuppressWarnings("unchecked") |
384 public abstract static class Builder <BuilderType extends Builder<BuilderType>
> | 395 public abstract static class Builder <BuilderType extends Builder<BuilderType>
> |
385 extends AbstractMessage.Builder<BuilderType> { | 396 extends AbstractMessage.Builder<BuilderType> { |
386 | 397 |
387 private BuilderParent builderParent; | 398 private BuilderParent builderParent; |
388 | 399 |
389 private BuilderParentImpl meAsParent; | 400 private BuilderParentImpl meAsParent; |
390 | 401 |
391 // Indicates that we've built a message and so we are now obligated | 402 // Indicates that we've built a message and so we are now obligated |
392 // to dispatch dirty invalidations. See GeneratedMessage.BuilderListener. | 403 // to dispatch dirty invalidations. See GeneratedMessageV3.BuilderListener. |
393 private boolean isClean; | 404 private boolean isClean; |
394 | 405 |
395 private UnknownFieldSet unknownFields = | 406 private UnknownFieldSet unknownFields = |
396 UnknownFieldSet.getDefaultInstance(); | 407 UnknownFieldSet.getDefaultInstance(); |
397 | 408 |
398 protected Builder() { | 409 protected Builder() { |
399 this(null); | 410 this(null); |
400 } | 411 } |
401 | 412 |
402 protected Builder(BuilderParent builderParent) { | 413 protected Builder(BuilderParent builderParent) { |
403 this.builderParent = builderParent; | 414 this.builderParent = builderParent; |
404 } | 415 } |
405 | 416 |
| 417 @Override |
406 void dispose() { | 418 void dispose() { |
407 builderParent = null; | 419 builderParent = null; |
408 } | 420 } |
409 | 421 |
410 /** | 422 /** |
411 * Called by the subclass when a message is built. | 423 * Called by the subclass when a message is built. |
412 */ | 424 */ |
413 protected void onBuilt() { | 425 protected void onBuilt() { |
414 if (builderParent != null) { | 426 if (builderParent != null) { |
415 markClean(); | 427 markClean(); |
416 } | 428 } |
417 } | 429 } |
418 | 430 |
419 /** | 431 /** |
420 * Called by the subclass or a builder to notify us that a message was | 432 * Called by the subclass or a builder to notify us that a message was |
421 * built and may be cached and therefore invalidations are needed. | 433 * built and may be cached and therefore invalidations are needed. |
422 */ | 434 */ |
| 435 @Override |
423 protected void markClean() { | 436 protected void markClean() { |
424 this.isClean = true; | 437 this.isClean = true; |
425 } | 438 } |
426 | 439 |
427 /** | 440 /** |
428 * Gets whether invalidations are needed | 441 * Gets whether invalidations are needed |
429 * | 442 * |
430 * @return whether invalidations are needed | 443 * @return whether invalidations are needed |
431 */ | 444 */ |
432 protected boolean isClean() { | 445 protected boolean isClean() { |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
748 ExtensionLite<MessageType, List<Type>> extension); | 761 ExtensionLite<MessageType, List<Type>> extension); |
749 | 762 |
750 /** Get the value of an extension. */ | 763 /** Get the value of an extension. */ |
751 <Type> Type getExtension( | 764 <Type> Type getExtension( |
752 ExtensionLite<MessageType, Type> extension); | 765 ExtensionLite<MessageType, Type> extension); |
753 | 766 |
754 /** Get one element of a repeated extension. */ | 767 /** Get one element of a repeated extension. */ |
755 <Type> Type getExtension( | 768 <Type> Type getExtension( |
756 ExtensionLite<MessageType, List<Type>> extension, | 769 ExtensionLite<MessageType, List<Type>> extension, |
757 int index); | 770 int index); |
| 771 |
| 772 /** Check if a singular extension is present. */ |
| 773 <Type> boolean hasExtension( |
| 774 Extension<MessageType, Type> extension); |
| 775 /** Check if a singular extension is present. */ |
| 776 <Type> boolean hasExtension( |
| 777 GeneratedExtension<MessageType, Type> extension); |
| 778 /** Get the number of elements in a repeated extension. */ |
| 779 <Type> int getExtensionCount( |
| 780 Extension<MessageType, List<Type>> extension); |
| 781 /** Get the number of elements in a repeated extension. */ |
| 782 <Type> int getExtensionCount( |
| 783 GeneratedExtension<MessageType, List<Type>> extension); |
| 784 /** Get the value of an extension. */ |
| 785 <Type> Type getExtension( |
| 786 Extension<MessageType, Type> extension); |
| 787 /** Get the value of an extension. */ |
| 788 <Type> Type getExtension( |
| 789 GeneratedExtension<MessageType, Type> extension); |
| 790 /** Get one element of a repeated extension. */ |
| 791 <Type> Type getExtension( |
| 792 Extension<MessageType, List<Type>> extension, |
| 793 int index); |
| 794 /** Get one element of a repeated extension. */ |
| 795 <Type> Type getExtension( |
| 796 GeneratedExtension<MessageType, List<Type>> extension, |
| 797 int index); |
758 } | 798 } |
759 | 799 |
760 /** | 800 /** |
761 * Generated message classes for message types that contain extension ranges | 801 * Generated message classes for message types that contain extension ranges |
762 * subclass this. | 802 * subclass this. |
763 * | 803 * |
764 * <p>This class implements type-safe accessors for extensions. They | 804 * <p>This class implements type-safe accessors for extensions. They |
765 * implement all the same operations that you can do with normal fields -- | 805 * implement all the same operations that you can do with normal fields -- |
766 * e.g. "has", "get", and "getCount" -- but for extensions. The extensions | 806 * e.g. "has", "get", and "getCount" -- but for extensions. The extensions |
767 * are identified using instances of the class {@link GeneratedExtension}; | 807 * are identified using instances of the class {@link GeneratedExtension}; |
(...skipping 19 matching lines...) Expand all Loading... |
787 * | 827 * |
788 * <pre> | 828 * <pre> |
789 * MyProto.Foo foo = getFoo(); | 829 * MyProto.Foo foo = getFoo(); |
790 * int i = foo.getExtension(MyProto.bar); | 830 * int i = foo.getExtension(MyProto.bar); |
791 * </pre> | 831 * </pre> |
792 * | 832 * |
793 * <p>See also {@link ExtendableBuilder}. | 833 * <p>See also {@link ExtendableBuilder}. |
794 */ | 834 */ |
795 public abstract static class ExtendableMessage< | 835 public abstract static class ExtendableMessage< |
796 MessageType extends ExtendableMessage> | 836 MessageType extends ExtendableMessage> |
797 extends GeneratedMessage | 837 extends GeneratedMessageV3 |
798 implements ExtendableMessageOrBuilder<MessageType> { | 838 implements ExtendableMessageOrBuilder<MessageType> { |
799 | 839 |
800 private static final long serialVersionUID = 1L; | 840 private static final long serialVersionUID = 1L; |
801 | 841 |
802 private final FieldSet<FieldDescriptor> extensions; | 842 private final FieldSet<FieldDescriptor> extensions; |
803 | 843 |
804 protected ExtendableMessage() { | 844 protected ExtendableMessage() { |
805 this.extensions = FieldSet.newFieldSet(); | 845 this.extensions = FieldSet.newFieldSet(); |
806 } | 846 } |
807 | 847 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
874 public final <Type> Type getExtension( | 914 public final <Type> Type getExtension( |
875 final ExtensionLite<MessageType, List<Type>> extensionLite, final int in
dex) { | 915 final ExtensionLite<MessageType, List<Type>> extensionLite, final int in
dex) { |
876 Extension<MessageType, List<Type>> extension = checkNotLite(extensionLite)
; | 916 Extension<MessageType, List<Type>> extension = checkNotLite(extensionLite)
; |
877 | 917 |
878 verifyExtensionContainingType(extension); | 918 verifyExtensionContainingType(extension); |
879 FieldDescriptor descriptor = extension.getDescriptor(); | 919 FieldDescriptor descriptor = extension.getDescriptor(); |
880 return (Type) extension.singularFromReflectionType( | 920 return (Type) extension.singularFromReflectionType( |
881 extensions.getRepeatedField(descriptor, index)); | 921 extensions.getRepeatedField(descriptor, index)); |
882 } | 922 } |
883 | 923 |
| 924 /** Check if a singular extension is present. */ |
| 925 @Override |
| 926 public final <Type> boolean hasExtension(final Extension<MessageType, Type>
extension) { |
| 927 return hasExtension((ExtensionLite<MessageType, Type>) extension); |
| 928 } |
| 929 /** Check if a singular extension is present. */ |
| 930 @Override |
| 931 public final <Type> boolean hasExtension( |
| 932 final GeneratedExtension<MessageType, Type> extension) { |
| 933 return hasExtension((ExtensionLite<MessageType, Type>) extension); |
| 934 } |
| 935 /** Get the number of elements in a repeated extension. */ |
| 936 @Override |
| 937 public final <Type> int getExtensionCount( |
| 938 final Extension<MessageType, List<Type>> extension) { |
| 939 return getExtensionCount((ExtensionLite<MessageType, List<Type>>) extensio
n); |
| 940 } |
| 941 /** Get the number of elements in a repeated extension. */ |
| 942 @Override |
| 943 public final <Type> int getExtensionCount( |
| 944 final GeneratedExtension<MessageType, List<Type>> extension) { |
| 945 return getExtensionCount((ExtensionLite<MessageType, List<Type>>) extensio
n); |
| 946 } |
| 947 /** Get the value of an extension. */ |
| 948 @Override |
| 949 public final <Type> Type getExtension(final Extension<MessageType, Type> ext
ension) { |
| 950 return getExtension((ExtensionLite<MessageType, Type>) extension); |
| 951 } |
| 952 /** Get the value of an extension. */ |
| 953 @Override |
| 954 public final <Type> Type getExtension( |
| 955 final GeneratedExtension<MessageType, Type> extension) { |
| 956 return getExtension((ExtensionLite<MessageType, Type>) extension); |
| 957 } |
| 958 /** Get one element of a repeated extension. */ |
| 959 @Override |
| 960 public final <Type> Type getExtension( |
| 961 final Extension<MessageType, List<Type>> extension, final int index) { |
| 962 return getExtension((ExtensionLite<MessageType, List<Type>>) extension, in
dex); |
| 963 } |
| 964 /** Get one element of a repeated extension. */ |
| 965 @Override |
| 966 public final <Type> Type getExtension( |
| 967 final GeneratedExtension<MessageType, List<Type>> extension, final int i
ndex) { |
| 968 return getExtension((ExtensionLite<MessageType, List<Type>>) extension, in
dex); |
| 969 } |
| 970 |
884 /** Called by subclasses to check if all extensions are initialized. */ | 971 /** Called by subclasses to check if all extensions are initialized. */ |
885 protected boolean extensionsAreInitialized() { | 972 protected boolean extensionsAreInitialized() { |
886 return extensions.isInitialized(); | 973 return extensions.isInitialized(); |
887 } | 974 } |
888 | 975 |
889 @Override | 976 @Override |
890 public boolean isInitialized() { | 977 public boolean isInitialized() { |
891 return super.isInitialized() && extensionsAreInitialized(); | 978 return super.isInitialized() && extensionsAreInitialized(); |
892 } | 979 } |
893 | 980 |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1123 void internalSetExtensionSet(FieldSet<FieldDescriptor> extensions) { | 1210 void internalSetExtensionSet(FieldSet<FieldDescriptor> extensions) { |
1124 this.extensions = extensions; | 1211 this.extensions = extensions; |
1125 } | 1212 } |
1126 | 1213 |
1127 @Override | 1214 @Override |
1128 public BuilderType clear() { | 1215 public BuilderType clear() { |
1129 extensions = FieldSet.emptySet(); | 1216 extensions = FieldSet.emptySet(); |
1130 return super.clear(); | 1217 return super.clear(); |
1131 } | 1218 } |
1132 | 1219 |
1133 // This is implemented here only to work around an apparent bug in the | |
1134 // Java compiler and/or build system. See bug #1898463. The mere presence | |
1135 // of this clone() implementation makes it go away. | |
1136 @Override | |
1137 public BuilderType clone() { | |
1138 return super.clone(); | |
1139 } | |
1140 | |
1141 private void ensureExtensionsIsMutable() { | 1220 private void ensureExtensionsIsMutable() { |
1142 if (extensions.isImmutable()) { | 1221 if (extensions.isImmutable()) { |
1143 extensions = extensions.clone(); | 1222 extensions = extensions.clone(); |
1144 } | 1223 } |
1145 } | 1224 } |
1146 | 1225 |
1147 private void verifyExtensionContainingType( | 1226 private void verifyExtensionContainingType( |
1148 final Extension<MessageType, ?> extension) { | 1227 final Extension<MessageType, ?> extension) { |
1149 if (extension.getDescriptor().getContainingType() != | 1228 if (extension.getDescriptor().getContainingType() != |
1150 getDescriptorForType()) { | 1229 getDescriptorForType()) { |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1262 final ExtensionLite<MessageType, ?> extensionLite) { | 1341 final ExtensionLite<MessageType, ?> extensionLite) { |
1263 Extension<MessageType, ?> extension = checkNotLite(extensionLite); | 1342 Extension<MessageType, ?> extension = checkNotLite(extensionLite); |
1264 | 1343 |
1265 verifyExtensionContainingType(extension); | 1344 verifyExtensionContainingType(extension); |
1266 ensureExtensionsIsMutable(); | 1345 ensureExtensionsIsMutable(); |
1267 extensions.clearField(extension.getDescriptor()); | 1346 extensions.clearField(extension.getDescriptor()); |
1268 onChanged(); | 1347 onChanged(); |
1269 return (BuilderType) this; | 1348 return (BuilderType) this; |
1270 } | 1349 } |
1271 | 1350 |
| 1351 /** Check if a singular extension is present. */ |
| 1352 @Override |
| 1353 public final <Type> boolean hasExtension(final Extension<MessageType, Type>
extension) { |
| 1354 return hasExtension((ExtensionLite<MessageType, Type>) extension); |
| 1355 } |
| 1356 /** Check if a singular extension is present. */ |
| 1357 @Override |
| 1358 public final <Type> boolean hasExtension( |
| 1359 final GeneratedExtension<MessageType, Type> extension) { |
| 1360 return hasExtension((ExtensionLite<MessageType, Type>) extension); |
| 1361 } |
| 1362 /** Get the number of elements in a repeated extension. */ |
| 1363 @Override |
| 1364 public final <Type> int getExtensionCount( |
| 1365 final Extension<MessageType, List<Type>> extension) { |
| 1366 return getExtensionCount((ExtensionLite<MessageType, List<Type>>) extensio
n); |
| 1367 } |
| 1368 /** Get the number of elements in a repeated extension. */ |
| 1369 @Override |
| 1370 public final <Type> int getExtensionCount( |
| 1371 final GeneratedExtension<MessageType, List<Type>> extension) { |
| 1372 return getExtensionCount((ExtensionLite<MessageType, List<Type>>) extensio
n); |
| 1373 } |
| 1374 /** Get the value of an extension. */ |
| 1375 @Override |
| 1376 public final <Type> Type getExtension(final Extension<MessageType, Type> ext
ension) { |
| 1377 return getExtension((ExtensionLite<MessageType, Type>) extension); |
| 1378 } |
| 1379 /** Get the value of an extension. */ |
| 1380 @Override |
| 1381 public final <Type> Type getExtension( |
| 1382 final GeneratedExtension<MessageType, Type> extension) { |
| 1383 return getExtension((ExtensionLite<MessageType, Type>) extension); |
| 1384 } |
| 1385 /** Get the value of an extension. */ |
| 1386 @Override |
| 1387 public final <Type> Type getExtension( |
| 1388 final Extension<MessageType, List<Type>> extension, final int index) { |
| 1389 return getExtension((ExtensionLite<MessageType, List<Type>>) extension, in
dex); |
| 1390 } |
| 1391 /** Get the value of an extension. */ |
| 1392 @Override |
| 1393 public final <Type> Type getExtension( |
| 1394 final GeneratedExtension<MessageType, List<Type>> extension, final int i
ndex) { |
| 1395 return getExtension((ExtensionLite<MessageType, List<Type>>) extension, in
dex); |
| 1396 } |
| 1397 /** Set the value of an extension. */ |
| 1398 public final <Type> BuilderType setExtension( |
| 1399 final Extension<MessageType, Type> extension, final Type value) { |
| 1400 return setExtension((ExtensionLite<MessageType, Type>) extension, value); |
| 1401 } |
| 1402 /** Set the value of an extension. */ |
| 1403 public <Type> BuilderType setExtension( |
| 1404 final GeneratedExtension<MessageType, Type> extension, final Type value)
{ |
| 1405 return setExtension((ExtensionLite<MessageType, Type>) extension, value); |
| 1406 } |
| 1407 /** Set the value of one element of a repeated extension. */ |
| 1408 public final <Type> BuilderType setExtension( |
| 1409 final Extension<MessageType, List<Type>> extension, |
| 1410 final int index, final Type value) { |
| 1411 return setExtension((ExtensionLite<MessageType, List<Type>>) extension, in
dex, value); |
| 1412 } |
| 1413 /** Set the value of one element of a repeated extension. */ |
| 1414 public <Type> BuilderType setExtension( |
| 1415 final GeneratedExtension<MessageType, List<Type>> extension, |
| 1416 final int index, final Type value) { |
| 1417 return setExtension((ExtensionLite<MessageType, List<Type>>) extension, in
dex, value); |
| 1418 } |
| 1419 /** Append a value to a repeated extension. */ |
| 1420 public final <Type> BuilderType addExtension( |
| 1421 final Extension<MessageType, List<Type>> extension, final Type value) { |
| 1422 return addExtension((ExtensionLite<MessageType, List<Type>>) extension, va
lue); |
| 1423 } |
| 1424 /** Append a value to a repeated extension. */ |
| 1425 public <Type> BuilderType addExtension( |
| 1426 final GeneratedExtension<MessageType, List<Type>> extension, final Type
value) { |
| 1427 return addExtension((ExtensionLite<MessageType, List<Type>>) extension, va
lue); |
| 1428 } |
| 1429 /** Clear an extension. */ |
| 1430 public final <Type> BuilderType clearExtension( |
| 1431 final Extension<MessageType, ?> extension) { |
| 1432 return clearExtension((ExtensionLite<MessageType, ?>) extension); |
| 1433 } |
| 1434 /** Clear an extension. */ |
| 1435 public <Type> BuilderType clearExtension( |
| 1436 final GeneratedExtension<MessageType, ?> extension) { |
| 1437 return clearExtension((ExtensionLite<MessageType, ?>) extension); |
| 1438 } |
| 1439 |
1272 /** Called by subclasses to check if all extensions are initialized. */ | 1440 /** Called by subclasses to check if all extensions are initialized. */ |
1273 protected boolean extensionsAreInitialized() { | 1441 protected boolean extensionsAreInitialized() { |
1274 return extensions.isInitialized(); | 1442 return extensions.isInitialized(); |
1275 } | 1443 } |
1276 | 1444 |
1277 /** | 1445 /** |
1278 * Called by the build code path to create a copy of the extensions for | 1446 * Called by the build code path to create a copy of the extensions for |
1279 * building the message. | 1447 * building the message. |
1280 */ | 1448 */ |
1281 private FieldSet<FieldDescriptor> buildExtensions() { | 1449 private FieldSet<FieldDescriptor> buildExtensions() { |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1437 // ----------------------------------------------------------------- | 1605 // ----------------------------------------------------------------- |
1438 | 1606 |
1439 /** | 1607 /** |
1440 * Gets the descriptor for an extension. The implementation depends on whether | 1608 * Gets the descriptor for an extension. The implementation depends on whether |
1441 * the extension is scoped in the top level of a file or scoped in a Message. | 1609 * the extension is scoped in the top level of a file or scoped in a Message. |
1442 */ | 1610 */ |
1443 static interface ExtensionDescriptorRetriever { | 1611 static interface ExtensionDescriptorRetriever { |
1444 FieldDescriptor getDescriptor(); | 1612 FieldDescriptor getDescriptor(); |
1445 } | 1613 } |
1446 | 1614 |
1447 /** For use by generated code only. */ | |
1448 public static <ContainingType extends Message, Type> | |
1449 GeneratedExtension<ContainingType, Type> | |
1450 newMessageScopedGeneratedExtension(final Message scope, | |
1451 final int descriptorIndex, | |
1452 final Class singularType, | |
1453 final Message defaultInstance) { | |
1454 // For extensions scoped within a Message, we use the Message to resolve | |
1455 // the outer class's descriptor, from which the extension descriptor is | |
1456 // obtained. | |
1457 return new GeneratedExtension<ContainingType, Type>( | |
1458 new CachedDescriptorRetriever() { | |
1459 @Override | |
1460 public FieldDescriptor loadDescriptor() { | |
1461 return scope.getDescriptorForType().getExtensions().get(descriptorIn
dex); | |
1462 } | |
1463 }, | |
1464 singularType, | |
1465 defaultInstance, | |
1466 Extension.ExtensionType.IMMUTABLE); | |
1467 } | |
1468 | |
1469 /** For use by generated code only. */ | |
1470 public static <ContainingType extends Message, Type> | |
1471 GeneratedExtension<ContainingType, Type> | |
1472 newFileScopedGeneratedExtension(final Class singularType, | |
1473 final Message defaultInstance) { | |
1474 // For extensions scoped within a file, we rely on the outer class's | |
1475 // static initializer to call internalInit() on the extension when the | |
1476 // descriptor is available. | |
1477 return new GeneratedExtension<ContainingType, Type>( | |
1478 null, // ExtensionDescriptorRetriever is initialized in internalInit(); | |
1479 singularType, | |
1480 defaultInstance, | |
1481 Extension.ExtensionType.IMMUTABLE); | |
1482 } | |
1483 | |
1484 private abstract static class CachedDescriptorRetriever | |
1485 implements ExtensionDescriptorRetriever { | |
1486 private volatile FieldDescriptor descriptor; | |
1487 protected abstract FieldDescriptor loadDescriptor(); | |
1488 | |
1489 @Override | |
1490 public FieldDescriptor getDescriptor() { | |
1491 if (descriptor == null) { | |
1492 synchronized (this) { | |
1493 if (descriptor == null) { | |
1494 descriptor = loadDescriptor(); | |
1495 } | |
1496 } | |
1497 } | |
1498 return descriptor; | |
1499 } | |
1500 } | |
1501 | |
1502 /** | |
1503 * Used in proto1 generated code only. | |
1504 * | |
1505 * After enabling bridge, we can define proto2 extensions (the extended type | |
1506 * is a proto2 mutable message) in a proto1 .proto file. For these extensions | |
1507 * we should generate proto2 GeneratedExtensions. | |
1508 */ | |
1509 public static <ContainingType extends Message, Type> | |
1510 GeneratedExtension<ContainingType, Type> | |
1511 newMessageScopedGeneratedExtension( | |
1512 final Message scope, final String name, | |
1513 final Class singularType, final Message defaultInstance) { | |
1514 // For extensions scoped within a Message, we use the Message to resolve | |
1515 // the outer class's descriptor, from which the extension descriptor is | |
1516 // obtained. | |
1517 return new GeneratedExtension<ContainingType, Type>( | |
1518 new CachedDescriptorRetriever() { | |
1519 @Override | |
1520 protected FieldDescriptor loadDescriptor() { | |
1521 return scope.getDescriptorForType().findFieldByName(name); | |
1522 } | |
1523 }, | |
1524 singularType, | |
1525 defaultInstance, | |
1526 Extension.ExtensionType.MUTABLE); | |
1527 } | |
1528 | |
1529 /** | |
1530 * Used in proto1 generated code only. | |
1531 * | |
1532 * After enabling bridge, we can define proto2 extensions (the extended type | |
1533 * is a proto2 mutable message) in a proto1 .proto file. For these extensions | |
1534 * we should generate proto2 GeneratedExtensions. | |
1535 */ | |
1536 public static <ContainingType extends Message, Type> | |
1537 GeneratedExtension<ContainingType, Type> | |
1538 newFileScopedGeneratedExtension( | |
1539 final Class singularType, final Message defaultInstance, | |
1540 final String descriptorOuterClass, final String extensionName) { | |
1541 // For extensions scoped within a file, we load the descriptor outer | |
1542 // class and rely on it to get the FileDescriptor which then can be | |
1543 // used to obtain the extension's FieldDescriptor. | |
1544 return new GeneratedExtension<ContainingType, Type>( | |
1545 new CachedDescriptorRetriever() { | |
1546 @Override | |
1547 protected FieldDescriptor loadDescriptor() { | |
1548 try { | |
1549 Class clazz = singularType.getClassLoader().loadClass(descriptorOu
terClass); | |
1550 FileDescriptor file = (FileDescriptor) clazz.getField("descriptor"
).get(null); | |
1551 return file.findExtensionByName(extensionName); | |
1552 } catch (Exception e) { | |
1553 throw new RuntimeException( | |
1554 "Cannot load descriptors: " | |
1555 + descriptorOuterClass | |
1556 + " is not a valid descriptor class name", | |
1557 e); | |
1558 } | |
1559 } | |
1560 }, | |
1561 singularType, | |
1562 defaultInstance, | |
1563 Extension.ExtensionType.MUTABLE); | |
1564 } | |
1565 | |
1566 /** | |
1567 * Type used to represent generated extensions. The protocol compiler | |
1568 * generates a static singleton instance of this class for each extension. | |
1569 * | |
1570 * <p>For example, imagine you have the {@code .proto} file: | |
1571 * | |
1572 * <pre> | |
1573 * option java_class = "MyProto"; | |
1574 * | |
1575 * message Foo { | |
1576 * extensions 1000 to max; | |
1577 * } | |
1578 * | |
1579 * extend Foo { | |
1580 * optional int32 bar; | |
1581 * } | |
1582 * </pre> | |
1583 * | |
1584 * <p>Then, {@code MyProto.Foo.bar} has type | |
1585 * {@code GeneratedExtension<MyProto.Foo, Integer>}. | |
1586 * | |
1587 * <p>In general, users should ignore the details of this type, and simply use | |
1588 * these static singletons as parameters to the extension accessors defined | |
1589 * in {@link ExtendableMessage} and {@link ExtendableBuilder}. | |
1590 */ | |
1591 public static class GeneratedExtension< | |
1592 ContainingType extends Message, Type> extends | |
1593 Extension<ContainingType, Type> { | |
1594 // TODO(kenton): Find ways to avoid using Java reflection within this | |
1595 // class. Also try to avoid suppressing unchecked warnings. | |
1596 | |
1597 // We can't always initialize the descriptor of a GeneratedExtension when | |
1598 // we first construct it due to initialization order difficulties (namely, | |
1599 // the descriptor may not have been constructed yet, since it is often | |
1600 // constructed by the initializer of a separate module). | |
1601 // | |
1602 // In the case of nested extensions, we initialize the | |
1603 // ExtensionDescriptorRetriever with an instance that uses the scoping | |
1604 // Message's default instance to retrieve the extension's descriptor. | |
1605 // | |
1606 // In the case of non-nested extensions, we initialize the | |
1607 // ExtensionDescriptorRetriever to null and rely on the outer class's static | |
1608 // initializer to call internalInit() after the descriptor has been parsed. | |
1609 GeneratedExtension(ExtensionDescriptorRetriever descriptorRetriever, | |
1610 Class singularType, | |
1611 Message messageDefaultInstance, | |
1612 ExtensionType extensionType) { | |
1613 if (Message.class.isAssignableFrom(singularType) && | |
1614 !singularType.isInstance(messageDefaultInstance)) { | |
1615 throw new IllegalArgumentException( | |
1616 "Bad messageDefaultInstance for " + singularType.getName()); | |
1617 } | |
1618 this.descriptorRetriever = descriptorRetriever; | |
1619 this.singularType = singularType; | |
1620 this.messageDefaultInstance = messageDefaultInstance; | |
1621 | |
1622 if (ProtocolMessageEnum.class.isAssignableFrom(singularType)) { | |
1623 this.enumValueOf = getMethodOrDie(singularType, "valueOf", | |
1624 EnumValueDescriptor.class); | |
1625 this.enumGetValueDescriptor = | |
1626 getMethodOrDie(singularType, "getValueDescriptor"); | |
1627 } else { | |
1628 this.enumValueOf = null; | |
1629 this.enumGetValueDescriptor = null; | |
1630 } | |
1631 this.extensionType = extensionType; | |
1632 } | |
1633 | |
1634 /** For use by generated code only. */ | |
1635 public void internalInit(final FieldDescriptor descriptor) { | |
1636 if (descriptorRetriever != null) { | |
1637 throw new IllegalStateException("Already initialized."); | |
1638 } | |
1639 descriptorRetriever = | |
1640 new ExtensionDescriptorRetriever() { | |
1641 @Override | |
1642 public FieldDescriptor getDescriptor() { | |
1643 return descriptor; | |
1644 } | |
1645 }; | |
1646 } | |
1647 | |
1648 private ExtensionDescriptorRetriever descriptorRetriever; | |
1649 private final Class singularType; | |
1650 private final Message messageDefaultInstance; | |
1651 private final Method enumValueOf; | |
1652 private final Method enumGetValueDescriptor; | |
1653 private final ExtensionType extensionType; | |
1654 | |
1655 @Override | |
1656 public FieldDescriptor getDescriptor() { | |
1657 if (descriptorRetriever == null) { | |
1658 throw new IllegalStateException( | |
1659 "getDescriptor() called before internalInit()"); | |
1660 } | |
1661 return descriptorRetriever.getDescriptor(); | |
1662 } | |
1663 | |
1664 /** | |
1665 * If the extension is an embedded message or group, returns the default | |
1666 * instance of the message. | |
1667 */ | |
1668 @Override | |
1669 public Message getMessageDefaultInstance() { | |
1670 return messageDefaultInstance; | |
1671 } | |
1672 | |
1673 @Override | |
1674 protected ExtensionType getExtensionType() { | |
1675 return extensionType; | |
1676 } | |
1677 | |
1678 /** | |
1679 * Convert from the type used by the reflection accessors to the type used | |
1680 * by native accessors. E.g., for enums, the reflection accessors use | |
1681 * EnumValueDescriptors but the native accessors use the generated enum | |
1682 * type. | |
1683 */ | |
1684 @Override | |
1685 @SuppressWarnings("unchecked") | |
1686 protected Object fromReflectionType(final Object value) { | |
1687 FieldDescriptor descriptor = getDescriptor(); | |
1688 if (descriptor.isRepeated()) { | |
1689 if (descriptor.getJavaType() == FieldDescriptor.JavaType.MESSAGE || | |
1690 descriptor.getJavaType() == FieldDescriptor.JavaType.ENUM) { | |
1691 // Must convert the whole list. | |
1692 final List result = new ArrayList(); | |
1693 for (final Object element : (List) value) { | |
1694 result.add(singularFromReflectionType(element)); | |
1695 } | |
1696 return result; | |
1697 } else { | |
1698 return value; | |
1699 } | |
1700 } else { | |
1701 return singularFromReflectionType(value); | |
1702 } | |
1703 } | |
1704 | |
1705 /** | |
1706 * Like {@link #fromReflectionType(Object)}, but if the type is a repeated | |
1707 * type, this converts a single element. | |
1708 */ | |
1709 @Override | |
1710 protected Object singularFromReflectionType(final Object value) { | |
1711 FieldDescriptor descriptor = getDescriptor(); | |
1712 switch (descriptor.getJavaType()) { | |
1713 case MESSAGE: | |
1714 if (singularType.isInstance(value)) { | |
1715 return value; | |
1716 } else { | |
1717 return messageDefaultInstance.newBuilderForType() | |
1718 .mergeFrom((Message) value).build(); | |
1719 } | |
1720 case ENUM: | |
1721 return invokeOrDie(enumValueOf, null, (EnumValueDescriptor) value); | |
1722 default: | |
1723 return value; | |
1724 } | |
1725 } | |
1726 | |
1727 /** | |
1728 * Convert from the type used by the native accessors to the type used | |
1729 * by reflection accessors. E.g., for enums, the reflection accessors use | |
1730 * EnumValueDescriptors but the native accessors use the generated enum | |
1731 * type. | |
1732 */ | |
1733 @Override | |
1734 @SuppressWarnings("unchecked") | |
1735 protected Object toReflectionType(final Object value) { | |
1736 FieldDescriptor descriptor = getDescriptor(); | |
1737 if (descriptor.isRepeated()) { | |
1738 if (descriptor.getJavaType() == FieldDescriptor.JavaType.ENUM) { | |
1739 // Must convert the whole list. | |
1740 final List result = new ArrayList(); | |
1741 for (final Object element : (List) value) { | |
1742 result.add(singularToReflectionType(element)); | |
1743 } | |
1744 return result; | |
1745 } else { | |
1746 return value; | |
1747 } | |
1748 } else { | |
1749 return singularToReflectionType(value); | |
1750 } | |
1751 } | |
1752 | |
1753 /** | |
1754 * Like {@link #toReflectionType(Object)}, but if the type is a repeated | |
1755 * type, this converts a single element. | |
1756 */ | |
1757 @Override | |
1758 protected Object singularToReflectionType(final Object value) { | |
1759 FieldDescriptor descriptor = getDescriptor(); | |
1760 switch (descriptor.getJavaType()) { | |
1761 case ENUM: | |
1762 return invokeOrDie(enumGetValueDescriptor, value); | |
1763 default: | |
1764 return value; | |
1765 } | |
1766 } | |
1767 | |
1768 @Override | |
1769 public int getNumber() { | |
1770 return getDescriptor().getNumber(); | |
1771 } | |
1772 | |
1773 @Override | |
1774 public WireFormat.FieldType getLiteType() { | |
1775 return getDescriptor().getLiteType(); | |
1776 } | |
1777 | |
1778 @Override | |
1779 public boolean isRepeated() { | |
1780 return getDescriptor().isRepeated(); | |
1781 } | |
1782 | |
1783 @Override | |
1784 @SuppressWarnings("unchecked") | |
1785 public Type getDefaultValue() { | |
1786 if (isRepeated()) { | |
1787 return (Type) Collections.emptyList(); | |
1788 } | |
1789 if (getDescriptor().getJavaType() == FieldDescriptor.JavaType.MESSAGE) { | |
1790 return (Type) messageDefaultInstance; | |
1791 } | |
1792 return (Type) singularFromReflectionType( | |
1793 getDescriptor().getDefaultValue()); | |
1794 } | |
1795 } | |
1796 | 1615 |
1797 // ================================================================= | 1616 // ================================================================= |
1798 | 1617 |
1799 /** Calls Class.getMethod and throws a RuntimeException if it fails. */ | 1618 /** Calls Class.getMethod and throws a RuntimeException if it fails. */ |
1800 @SuppressWarnings("unchecked") | 1619 @SuppressWarnings("unchecked") |
1801 private static Method getMethodOrDie( | 1620 private static Method getMethodOrDie( |
1802 final Class clazz, final String name, final Class... params) { | 1621 final Class clazz, final String name, final Class... params) { |
1803 try { | 1622 try { |
1804 return clazz.getMethod(name, params); | 1623 return clazz.getMethod(name, params); |
1805 } catch (NoSuchMethodException e) { | 1624 } catch (NoSuchMethodException e) { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1862 * | 1681 * |
1863 * @param descriptor The type's descriptor. | 1682 * @param descriptor The type's descriptor. |
1864 * @param camelCaseNames The camelcase names of all fields in the message. | 1683 * @param camelCaseNames The camelcase names of all fields in the message. |
1865 * These are used to derive the accessor method names. | 1684 * These are used to derive the accessor method names. |
1866 * @param messageClass The message type. | 1685 * @param messageClass The message type. |
1867 * @param builderClass The builder type. | 1686 * @param builderClass The builder type. |
1868 */ | 1687 */ |
1869 public FieldAccessorTable( | 1688 public FieldAccessorTable( |
1870 final Descriptor descriptor, | 1689 final Descriptor descriptor, |
1871 final String[] camelCaseNames, | 1690 final String[] camelCaseNames, |
1872 final Class<? extends GeneratedMessage> messageClass, | 1691 final Class<? extends GeneratedMessageV3> messageClass, |
1873 final Class<? extends Builder> builderClass) { | 1692 final Class<? extends Builder> builderClass) { |
1874 this(descriptor, camelCaseNames); | 1693 this(descriptor, camelCaseNames); |
1875 ensureFieldAccessorsInitialized(messageClass, builderClass); | 1694 ensureFieldAccessorsInitialized(messageClass, builderClass); |
1876 } | 1695 } |
1877 | 1696 |
1878 /** | 1697 /** |
1879 * Construct a FieldAccessorTable for a particular message class without | 1698 * Construct a FieldAccessorTable for a particular message class without |
1880 * initializing FieldAccessors. | 1699 * initializing FieldAccessors. |
1881 */ | 1700 */ |
1882 public FieldAccessorTable( | 1701 public FieldAccessorTable( |
1883 final Descriptor descriptor, | 1702 final Descriptor descriptor, |
1884 final String[] camelCaseNames) { | 1703 final String[] camelCaseNames) { |
1885 this.descriptor = descriptor; | 1704 this.descriptor = descriptor; |
1886 this.camelCaseNames = camelCaseNames; | 1705 this.camelCaseNames = camelCaseNames; |
1887 fields = new FieldAccessor[descriptor.getFields().size()]; | 1706 fields = new FieldAccessor[descriptor.getFields().size()]; |
1888 oneofs = new OneofAccessor[descriptor.getOneofs().size()]; | 1707 oneofs = new OneofAccessor[descriptor.getOneofs().size()]; |
1889 initialized = false; | 1708 initialized = false; |
1890 } | 1709 } |
1891 | 1710 |
1892 private boolean isMapFieldEnabled(FieldDescriptor field) { | |
1893 boolean result = true; | |
1894 return result; | |
1895 } | |
1896 | |
1897 /** | 1711 /** |
1898 * Ensures the field accessors are initialized. This method is thread-safe. | 1712 * Ensures the field accessors are initialized. This method is thread-safe. |
1899 * | 1713 * |
1900 * @param messageClass The message type. | 1714 * @param messageClass The message type. |
1901 * @param builderClass The builder type. | 1715 * @param builderClass The builder type. |
1902 * @return this | 1716 * @return this |
1903 */ | 1717 */ |
1904 public FieldAccessorTable ensureFieldAccessorsInitialized( | 1718 public FieldAccessorTable ensureFieldAccessorsInitialized( |
1905 Class<? extends GeneratedMessage> messageClass, | 1719 Class<? extends GeneratedMessageV3> messageClass, |
1906 Class<? extends Builder> builderClass) { | 1720 Class<? extends Builder> builderClass) { |
1907 if (initialized) { return this; } | 1721 if (initialized) { return this; } |
1908 synchronized (this) { | 1722 synchronized (this) { |
1909 if (initialized) { return this; } | 1723 if (initialized) { return this; } |
1910 int fieldsSize = fields.length; | 1724 int fieldsSize = fields.length; |
1911 for (int i = 0; i < fieldsSize; i++) { | 1725 for (int i = 0; i < fieldsSize; i++) { |
1912 FieldDescriptor field = descriptor.getFields().get(i); | 1726 FieldDescriptor field = descriptor.getFields().get(i); |
1913 String containingOneofCamelCaseName = null; | 1727 String containingOneofCamelCaseName = null; |
1914 if (field.getContainingOneof() != null) { | 1728 if (field.getContainingOneof() != null) { |
1915 containingOneofCamelCaseName = | 1729 containingOneofCamelCaseName = |
1916 camelCaseNames[fieldsSize + field.getContainingOneof().getIndex(
)]; | 1730 camelCaseNames[fieldsSize + field.getContainingOneof().getIndex(
)]; |
1917 } | 1731 } |
1918 if (field.isRepeated()) { | 1732 if (field.isRepeated()) { |
1919 if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { | 1733 if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { |
1920 if (field.isMapField() && isMapFieldEnabled(field)) { | 1734 if (field.isMapField()) { |
1921 fields[i] = new MapFieldAccessor( | 1735 fields[i] = new MapFieldAccessor( |
1922 field, camelCaseNames[i], messageClass, builderClass); | 1736 field, camelCaseNames[i], messageClass, builderClass); |
1923 } else { | 1737 } else { |
1924 fields[i] = new RepeatedMessageFieldAccessor( | 1738 fields[i] = new RepeatedMessageFieldAccessor( |
1925 field, camelCaseNames[i], messageClass, builderClass); | 1739 field, camelCaseNames[i], messageClass, builderClass); |
1926 } | 1740 } |
1927 } else if (field.getJavaType() == FieldDescriptor.JavaType.ENUM) { | 1741 } else if (field.getJavaType() == FieldDescriptor.JavaType.ENUM) { |
1928 fields[i] = new RepeatedEnumFieldAccessor( | 1742 fields[i] = new RepeatedEnumFieldAccessor( |
1929 field, camelCaseNames[i], messageClass, builderClass); | 1743 field, camelCaseNames[i], messageClass, builderClass); |
1930 } else { | 1744 } else { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1991 "OneofDescriptor does not match message type."); | 1805 "OneofDescriptor does not match message type."); |
1992 } | 1806 } |
1993 return oneofs[oneof.getIndex()]; | 1807 return oneofs[oneof.getIndex()]; |
1994 } | 1808 } |
1995 | 1809 |
1996 /** | 1810 /** |
1997 * Abstract interface that provides access to a single field. This is | 1811 * Abstract interface that provides access to a single field. This is |
1998 * implemented differently depending on the field type and cardinality. | 1812 * implemented differently depending on the field type and cardinality. |
1999 */ | 1813 */ |
2000 private interface FieldAccessor { | 1814 private interface FieldAccessor { |
2001 Object get(GeneratedMessage message); | 1815 Object get(GeneratedMessageV3 message); |
2002 Object get(GeneratedMessage.Builder builder); | 1816 Object get(GeneratedMessageV3.Builder builder); |
2003 Object getRaw(GeneratedMessage message); | 1817 Object getRaw(GeneratedMessageV3 message); |
2004 Object getRaw(GeneratedMessage.Builder builder); | 1818 Object getRaw(GeneratedMessageV3.Builder builder); |
2005 void set(Builder builder, Object value); | 1819 void set(Builder builder, Object value); |
2006 Object getRepeated(GeneratedMessage message, int index); | 1820 Object getRepeated(GeneratedMessageV3 message, int index); |
2007 Object getRepeated(GeneratedMessage.Builder builder, int index); | 1821 Object getRepeated(GeneratedMessageV3.Builder builder, int index); |
2008 Object getRepeatedRaw(GeneratedMessage message, int index); | 1822 Object getRepeatedRaw(GeneratedMessageV3 message, int index); |
2009 Object getRepeatedRaw(GeneratedMessage.Builder builder, int index); | 1823 Object getRepeatedRaw(GeneratedMessageV3.Builder builder, int index); |
2010 void setRepeated(Builder builder, | 1824 void setRepeated(Builder builder, |
2011 int index, Object value); | 1825 int index, Object value); |
2012 void addRepeated(Builder builder, Object value); | 1826 void addRepeated(Builder builder, Object value); |
2013 boolean has(GeneratedMessage message); | 1827 boolean has(GeneratedMessageV3 message); |
2014 boolean has(GeneratedMessage.Builder builder); | 1828 boolean has(GeneratedMessageV3.Builder builder); |
2015 int getRepeatedCount(GeneratedMessage message); | 1829 int getRepeatedCount(GeneratedMessageV3 message); |
2016 int getRepeatedCount(GeneratedMessage.Builder builder); | 1830 int getRepeatedCount(GeneratedMessageV3.Builder builder); |
2017 void clear(Builder builder); | 1831 void clear(Builder builder); |
2018 Message.Builder newBuilder(); | 1832 Message.Builder newBuilder(); |
2019 Message.Builder getBuilder(GeneratedMessage.Builder builder); | 1833 Message.Builder getBuilder(GeneratedMessageV3.Builder builder); |
2020 Message.Builder getRepeatedBuilder(GeneratedMessage.Builder builder, | 1834 Message.Builder getRepeatedBuilder(GeneratedMessageV3.Builder builder, |
2021 int index); | 1835 int index); |
2022 } | 1836 } |
2023 | 1837 |
2024 /** OneofAccessor provides access to a single oneof. */ | 1838 /** OneofAccessor provides access to a single oneof. */ |
2025 private static class OneofAccessor { | 1839 private static class OneofAccessor { |
2026 OneofAccessor( | 1840 OneofAccessor( |
2027 final Descriptor descriptor, final String camelCaseName, | 1841 final Descriptor descriptor, final String camelCaseName, |
2028 final Class<? extends GeneratedMessage> messageClass, | 1842 final Class<? extends GeneratedMessageV3> messageClass, |
2029 final Class<? extends Builder> builderClass) { | 1843 final Class<? extends Builder> builderClass) { |
2030 this.descriptor = descriptor; | 1844 this.descriptor = descriptor; |
2031 caseMethod = | 1845 caseMethod = |
2032 getMethodOrDie(messageClass, "get" + camelCaseName + "Case"); | 1846 getMethodOrDie(messageClass, "get" + camelCaseName + "Case"); |
2033 caseMethodBuilder = | 1847 caseMethodBuilder = |
2034 getMethodOrDie(builderClass, "get" + camelCaseName + "Case"); | 1848 getMethodOrDie(builderClass, "get" + camelCaseName + "Case"); |
2035 clearMethod = getMethodOrDie(builderClass, "clear" + camelCaseName); | 1849 clearMethod = getMethodOrDie(builderClass, "clear" + camelCaseName); |
2036 } | 1850 } |
2037 | 1851 |
2038 private final Descriptor descriptor; | 1852 private final Descriptor descriptor; |
2039 private final Method caseMethod; | 1853 private final Method caseMethod; |
2040 private final Method caseMethodBuilder; | 1854 private final Method caseMethodBuilder; |
2041 private final Method clearMethod; | 1855 private final Method clearMethod; |
2042 | 1856 |
2043 public boolean has(final GeneratedMessage message) { | 1857 public boolean has(final GeneratedMessageV3 message) { |
2044 if (((Internal.EnumLite) invokeOrDie(caseMethod, message)).getNumber() =
= 0) { | 1858 if (((Internal.EnumLite) invokeOrDie(caseMethod, message)).getNumber() =
= 0) { |
2045 return false; | 1859 return false; |
2046 } | 1860 } |
2047 return true; | 1861 return true; |
2048 } | 1862 } |
2049 | 1863 |
2050 public boolean has(GeneratedMessage.Builder builder) { | 1864 public boolean has(GeneratedMessageV3.Builder builder) { |
2051 if (((Internal.EnumLite) invokeOrDie(caseMethodBuilder, builder)).getNum
ber() == 0) { | 1865 if (((Internal.EnumLite) invokeOrDie(caseMethodBuilder, builder)).getNum
ber() == 0) { |
2052 return false; | 1866 return false; |
2053 } | 1867 } |
2054 return true; | 1868 return true; |
2055 } | 1869 } |
2056 | 1870 |
2057 public FieldDescriptor get(final GeneratedMessage message) { | 1871 public FieldDescriptor get(final GeneratedMessageV3 message) { |
2058 int fieldNumber = ((Internal.EnumLite) invokeOrDie(caseMethod, message))
.getNumber(); | 1872 int fieldNumber = ((Internal.EnumLite) invokeOrDie(caseMethod, message))
.getNumber(); |
2059 if (fieldNumber > 0) { | 1873 if (fieldNumber > 0) { |
2060 return descriptor.findFieldByNumber(fieldNumber); | 1874 return descriptor.findFieldByNumber(fieldNumber); |
2061 } | 1875 } |
2062 return null; | 1876 return null; |
2063 } | 1877 } |
2064 | 1878 |
2065 public FieldDescriptor get(GeneratedMessage.Builder builder) { | 1879 public FieldDescriptor get(GeneratedMessageV3.Builder builder) { |
2066 int fieldNumber = ((Internal.EnumLite) invokeOrDie(caseMethodBuilder, bu
ilder)).getNumber(); | 1880 int fieldNumber = ((Internal.EnumLite) invokeOrDie(caseMethodBuilder, bu
ilder)).getNumber(); |
2067 if (fieldNumber > 0) { | 1881 if (fieldNumber > 0) { |
2068 return descriptor.findFieldByNumber(fieldNumber); | 1882 return descriptor.findFieldByNumber(fieldNumber); |
2069 } | 1883 } |
2070 return null; | 1884 return null; |
2071 } | 1885 } |
2072 | 1886 |
2073 public void clear(final Builder builder) { | 1887 public void clear(final Builder builder) { |
2074 invokeOrDie(clearMethod, builder); | 1888 invokeOrDie(clearMethod, builder); |
2075 } | 1889 } |
2076 } | 1890 } |
2077 | 1891 |
2078 private static boolean supportFieldPresence(FileDescriptor file) { | 1892 private static boolean supportFieldPresence(FileDescriptor file) { |
2079 return file.getSyntax() == FileDescriptor.Syntax.PROTO2; | 1893 return file.getSyntax() == FileDescriptor.Syntax.PROTO2; |
2080 } | 1894 } |
2081 | 1895 |
2082 // --------------------------------------------------------------- | 1896 // --------------------------------------------------------------- |
2083 | 1897 |
2084 private static class SingularFieldAccessor implements FieldAccessor { | 1898 private static class SingularFieldAccessor implements FieldAccessor { |
2085 SingularFieldAccessor( | 1899 SingularFieldAccessor( |
2086 final FieldDescriptor descriptor, final String camelCaseName, | 1900 final FieldDescriptor descriptor, final String camelCaseName, |
2087 final Class<? extends GeneratedMessage> messageClass, | 1901 final Class<? extends GeneratedMessageV3> messageClass, |
2088 final Class<? extends Builder> builderClass, | 1902 final Class<? extends Builder> builderClass, |
2089 final String containingOneofCamelCaseName) { | 1903 final String containingOneofCamelCaseName) { |
2090 field = descriptor; | 1904 field = descriptor; |
2091 isOneofField = descriptor.getContainingOneof() != null; | 1905 isOneofField = descriptor.getContainingOneof() != null; |
2092 hasHasMethod = supportFieldPresence(descriptor.getFile()) | 1906 hasHasMethod = supportFieldPresence(descriptor.getFile()) |
2093 || (!isOneofField && descriptor.getJavaType() == FieldDescriptor.Jav
aType.MESSAGE); | 1907 || (!isOneofField && descriptor.getJavaType() == FieldDescriptor.Jav
aType.MESSAGE); |
2094 getMethod = getMethodOrDie(messageClass, "get" + camelCaseName); | 1908 getMethod = getMethodOrDie(messageClass, "get" + camelCaseName); |
2095 getMethodBuilder = getMethodOrDie(builderClass, "get" + camelCaseName); | 1909 getMethodBuilder = getMethodOrDie(builderClass, "get" + camelCaseName); |
2096 type = getMethod.getReturnType(); | 1910 type = getMethod.getReturnType(); |
2097 setMethod = getMethodOrDie(builderClass, "set" + camelCaseName, type); | 1911 setMethod = getMethodOrDie(builderClass, "set" + camelCaseName, type); |
(...skipping 17 matching lines...) Expand all Loading... |
2115 protected final Method setMethod; | 1929 protected final Method setMethod; |
2116 protected final Method hasMethod; | 1930 protected final Method hasMethod; |
2117 protected final Method hasMethodBuilder; | 1931 protected final Method hasMethodBuilder; |
2118 protected final Method clearMethod; | 1932 protected final Method clearMethod; |
2119 protected final Method caseMethod; | 1933 protected final Method caseMethod; |
2120 protected final Method caseMethodBuilder; | 1934 protected final Method caseMethodBuilder; |
2121 protected final FieldDescriptor field; | 1935 protected final FieldDescriptor field; |
2122 protected final boolean isOneofField; | 1936 protected final boolean isOneofField; |
2123 protected final boolean hasHasMethod; | 1937 protected final boolean hasHasMethod; |
2124 | 1938 |
2125 private int getOneofFieldNumber(final GeneratedMessage message) { | 1939 private int getOneofFieldNumber(final GeneratedMessageV3 message) { |
2126 return ((Internal.EnumLite) invokeOrDie(caseMethod, message)).getNumber(
); | 1940 return ((Internal.EnumLite) invokeOrDie(caseMethod, message)).getNumber(
); |
2127 } | 1941 } |
2128 | 1942 |
2129 private int getOneofFieldNumber(final GeneratedMessage.Builder builder) { | 1943 private int getOneofFieldNumber(final GeneratedMessageV3.Builder builder)
{ |
2130 return ((Internal.EnumLite) invokeOrDie(caseMethodBuilder, builder)).get
Number(); | 1944 return ((Internal.EnumLite) invokeOrDie(caseMethodBuilder, builder)).get
Number(); |
2131 } | 1945 } |
2132 | 1946 |
2133 @Override | 1947 @Override |
2134 public Object get(final GeneratedMessage message) { | 1948 public Object get(final GeneratedMessageV3 message) { |
2135 return invokeOrDie(getMethod, message); | 1949 return invokeOrDie(getMethod, message); |
2136 } | 1950 } |
2137 @Override | 1951 @Override |
2138 public Object get(GeneratedMessage.Builder builder) { | 1952 public Object get(GeneratedMessageV3.Builder builder) { |
2139 return invokeOrDie(getMethodBuilder, builder); | 1953 return invokeOrDie(getMethodBuilder, builder); |
2140 } | 1954 } |
2141 @Override | 1955 @Override |
2142 public Object getRaw(final GeneratedMessage message) { | 1956 public Object getRaw(final GeneratedMessageV3 message) { |
2143 return get(message); | 1957 return get(message); |
2144 } | 1958 } |
2145 @Override | 1959 @Override |
2146 public Object getRaw(GeneratedMessage.Builder builder) { | 1960 public Object getRaw(GeneratedMessageV3.Builder builder) { |
2147 return get(builder); | 1961 return get(builder); |
2148 } | 1962 } |
2149 @Override | 1963 @Override |
2150 public void set(final Builder builder, final Object value) { | 1964 public void set(final Builder builder, final Object value) { |
2151 invokeOrDie(setMethod, builder, value); | 1965 invokeOrDie(setMethod, builder, value); |
2152 } | 1966 } |
2153 @Override | 1967 @Override |
2154 public Object getRepeated(final GeneratedMessage message, final int index)
{ | 1968 public Object getRepeated(final GeneratedMessageV3 message, final int inde
x) { |
2155 throw new UnsupportedOperationException( | 1969 throw new UnsupportedOperationException( |
2156 "getRepeatedField() called on a singular field."); | 1970 "getRepeatedField() called on a singular field."); |
2157 } | 1971 } |
2158 @Override | 1972 @Override |
2159 public Object getRepeatedRaw(final GeneratedMessage message, final int ind
ex) { | 1973 public Object getRepeatedRaw(final GeneratedMessageV3 message, final int i
ndex) { |
2160 throw new UnsupportedOperationException( | 1974 throw new UnsupportedOperationException( |
2161 "getRepeatedFieldRaw() called on a singular field."); | 1975 "getRepeatedFieldRaw() called on a singular field."); |
2162 } | 1976 } |
2163 @Override | 1977 @Override |
2164 public Object getRepeated(GeneratedMessage.Builder builder, int index) { | 1978 public Object getRepeated(GeneratedMessageV3.Builder builder, int index) { |
2165 throw new UnsupportedOperationException( | 1979 throw new UnsupportedOperationException( |
2166 "getRepeatedField() called on a singular field."); | 1980 "getRepeatedField() called on a singular field."); |
2167 } | 1981 } |
2168 @Override | 1982 @Override |
2169 public Object getRepeatedRaw(GeneratedMessage.Builder builder, int index)
{ | 1983 public Object getRepeatedRaw(GeneratedMessageV3.Builder builder, int index
) { |
2170 throw new UnsupportedOperationException( | 1984 throw new UnsupportedOperationException( |
2171 "getRepeatedFieldRaw() called on a singular field."); | 1985 "getRepeatedFieldRaw() called on a singular field."); |
2172 } | 1986 } |
2173 @Override | 1987 @Override |
2174 public void setRepeated(final Builder builder, final int index, final Obje
ct value) { | 1988 public void setRepeated(final Builder builder, final int index, final Obje
ct value) { |
2175 throw new UnsupportedOperationException( | 1989 throw new UnsupportedOperationException( |
2176 "setRepeatedField() called on a singular field."); | 1990 "setRepeatedField() called on a singular field."); |
2177 } | 1991 } |
2178 @Override | 1992 @Override |
2179 public void addRepeated(final Builder builder, final Object value) { | 1993 public void addRepeated(final Builder builder, final Object value) { |
2180 throw new UnsupportedOperationException( | 1994 throw new UnsupportedOperationException( |
2181 "addRepeatedField() called on a singular field."); | 1995 "addRepeatedField() called on a singular field."); |
2182 } | 1996 } |
2183 @Override | 1997 @Override |
2184 public boolean has(final GeneratedMessage message) { | 1998 public boolean has(final GeneratedMessageV3 message) { |
2185 if (!hasHasMethod) { | 1999 if (!hasHasMethod) { |
2186 if (isOneofField) { | 2000 if (isOneofField) { |
2187 return getOneofFieldNumber(message) == field.getNumber(); | 2001 return getOneofFieldNumber(message) == field.getNumber(); |
2188 } | 2002 } |
2189 return !get(message).equals(field.getDefaultValue()); | 2003 return !get(message).equals(field.getDefaultValue()); |
2190 } | 2004 } |
2191 return (Boolean) invokeOrDie(hasMethod, message); | 2005 return (Boolean) invokeOrDie(hasMethod, message); |
2192 } | 2006 } |
2193 @Override | 2007 @Override |
2194 public boolean has(GeneratedMessage.Builder builder) { | 2008 public boolean has(GeneratedMessageV3.Builder builder) { |
2195 if (!hasHasMethod) { | 2009 if (!hasHasMethod) { |
2196 if (isOneofField) { | 2010 if (isOneofField) { |
2197 return getOneofFieldNumber(builder) == field.getNumber(); | 2011 return getOneofFieldNumber(builder) == field.getNumber(); |
2198 } | 2012 } |
2199 return !get(builder).equals(field.getDefaultValue()); | 2013 return !get(builder).equals(field.getDefaultValue()); |
2200 } | 2014 } |
2201 return (Boolean) invokeOrDie(hasMethodBuilder, builder); | 2015 return (Boolean) invokeOrDie(hasMethodBuilder, builder); |
2202 } | 2016 } |
2203 @Override | 2017 @Override |
2204 public int getRepeatedCount(final GeneratedMessage message) { | 2018 public int getRepeatedCount(final GeneratedMessageV3 message) { |
2205 throw new UnsupportedOperationException( | 2019 throw new UnsupportedOperationException( |
2206 "getRepeatedFieldSize() called on a singular field."); | 2020 "getRepeatedFieldSize() called on a singular field."); |
2207 } | 2021 } |
2208 @Override | 2022 @Override |
2209 public int getRepeatedCount(GeneratedMessage.Builder builder) { | 2023 public int getRepeatedCount(GeneratedMessageV3.Builder builder) { |
2210 throw new UnsupportedOperationException( | 2024 throw new UnsupportedOperationException( |
2211 "getRepeatedFieldSize() called on a singular field."); | 2025 "getRepeatedFieldSize() called on a singular field."); |
2212 } | 2026 } |
2213 @Override | 2027 @Override |
2214 public void clear(final Builder builder) { | 2028 public void clear(final Builder builder) { |
2215 invokeOrDie(clearMethod, builder); | 2029 invokeOrDie(clearMethod, builder); |
2216 } | 2030 } |
2217 @Override | 2031 @Override |
2218 public Message.Builder newBuilder() { | 2032 public Message.Builder newBuilder() { |
2219 throw new UnsupportedOperationException( | 2033 throw new UnsupportedOperationException( |
2220 "newBuilderForField() called on a non-Message type."); | 2034 "newBuilderForField() called on a non-Message type."); |
2221 } | 2035 } |
2222 @Override | 2036 @Override |
2223 public Message.Builder getBuilder(GeneratedMessage.Builder builder) { | 2037 public Message.Builder getBuilder(GeneratedMessageV3.Builder builder) { |
2224 throw new UnsupportedOperationException( | 2038 throw new UnsupportedOperationException( |
2225 "getFieldBuilder() called on a non-Message type."); | 2039 "getFieldBuilder() called on a non-Message type."); |
2226 } | 2040 } |
2227 @Override | 2041 @Override |
2228 public Message.Builder getRepeatedBuilder(GeneratedMessage.Builder builder
, int index) { | 2042 public Message.Builder getRepeatedBuilder(GeneratedMessageV3.Builder build
er, int index) { |
2229 throw new UnsupportedOperationException( | 2043 throw new UnsupportedOperationException( |
2230 "getRepeatedFieldBuilder() called on a non-Message type."); | 2044 "getRepeatedFieldBuilder() called on a non-Message type."); |
2231 } | 2045 } |
2232 } | 2046 } |
2233 | 2047 |
2234 private static class RepeatedFieldAccessor implements FieldAccessor { | 2048 private static class RepeatedFieldAccessor implements FieldAccessor { |
2235 protected final Class type; | 2049 protected final Class type; |
2236 protected final Method getMethod; | 2050 protected final Method getMethod; |
2237 protected final Method getMethodBuilder; | 2051 protected final Method getMethodBuilder; |
2238 protected final Method getRepeatedMethod; | 2052 protected final Method getRepeatedMethod; |
2239 protected final Method getRepeatedMethodBuilder; | 2053 protected final Method getRepeatedMethodBuilder; |
2240 protected final Method setRepeatedMethod; | 2054 protected final Method setRepeatedMethod; |
2241 protected final Method addRepeatedMethod; | 2055 protected final Method addRepeatedMethod; |
2242 protected final Method getCountMethod; | 2056 protected final Method getCountMethod; |
2243 protected final Method getCountMethodBuilder; | 2057 protected final Method getCountMethodBuilder; |
2244 protected final Method clearMethod; | 2058 protected final Method clearMethod; |
2245 | 2059 |
2246 RepeatedFieldAccessor( | 2060 RepeatedFieldAccessor( |
2247 final FieldDescriptor descriptor, final String camelCaseName, | 2061 final FieldDescriptor descriptor, final String camelCaseName, |
2248 final Class<? extends GeneratedMessage> messageClass, | 2062 final Class<? extends GeneratedMessageV3> messageClass, |
2249 final Class<? extends Builder> builderClass) { | 2063 final Class<? extends Builder> builderClass) { |
2250 getMethod = getMethodOrDie(messageClass, | 2064 getMethod = getMethodOrDie(messageClass, |
2251 "get" + camelCaseName + "List"); | 2065 "get" + camelCaseName + "List"); |
2252 getMethodBuilder = getMethodOrDie(builderClass, | 2066 getMethodBuilder = getMethodOrDie(builderClass, |
2253 "get" + camelCaseName + "List"); | 2067 "get" + camelCaseName + "List"); |
2254 getRepeatedMethod = | 2068 getRepeatedMethod = |
2255 getMethodOrDie(messageClass, "get" + camelCaseName, Integer.TYPE); | 2069 getMethodOrDie(messageClass, "get" + camelCaseName, Integer.TYPE); |
2256 getRepeatedMethodBuilder = | 2070 getRepeatedMethodBuilder = |
2257 getMethodOrDie(builderClass, "get" + camelCaseName, Integer.TYPE); | 2071 getMethodOrDie(builderClass, "get" + camelCaseName, Integer.TYPE); |
2258 type = getRepeatedMethod.getReturnType(); | 2072 type = getRepeatedMethod.getReturnType(); |
2259 setRepeatedMethod = | 2073 setRepeatedMethod = |
2260 getMethodOrDie(builderClass, "set" + camelCaseName, | 2074 getMethodOrDie(builderClass, "set" + camelCaseName, |
2261 Integer.TYPE, type); | 2075 Integer.TYPE, type); |
2262 addRepeatedMethod = | 2076 addRepeatedMethod = |
2263 getMethodOrDie(builderClass, "add" + camelCaseName, type); | 2077 getMethodOrDie(builderClass, "add" + camelCaseName, type); |
2264 getCountMethod = | 2078 getCountMethod = |
2265 getMethodOrDie(messageClass, "get" + camelCaseName + "Count"); | 2079 getMethodOrDie(messageClass, "get" + camelCaseName + "Count"); |
2266 getCountMethodBuilder = | 2080 getCountMethodBuilder = |
2267 getMethodOrDie(builderClass, "get" + camelCaseName + "Count"); | 2081 getMethodOrDie(builderClass, "get" + camelCaseName + "Count"); |
2268 | 2082 |
2269 clearMethod = getMethodOrDie(builderClass, "clear" + camelCaseName); | 2083 clearMethod = getMethodOrDie(builderClass, "clear" + camelCaseName); |
2270 } | 2084 } |
2271 | 2085 |
2272 @Override | 2086 @Override |
2273 public Object get(final GeneratedMessage message) { | 2087 public Object get(final GeneratedMessageV3 message) { |
2274 return invokeOrDie(getMethod, message); | 2088 return invokeOrDie(getMethod, message); |
2275 } | 2089 } |
2276 @Override | 2090 @Override |
2277 public Object get(GeneratedMessage.Builder builder) { | 2091 public Object get(GeneratedMessageV3.Builder builder) { |
2278 return invokeOrDie(getMethodBuilder, builder); | 2092 return invokeOrDie(getMethodBuilder, builder); |
2279 } | 2093 } |
2280 @Override | 2094 @Override |
2281 public Object getRaw(final GeneratedMessage message) { | 2095 public Object getRaw(final GeneratedMessageV3 message) { |
2282 return get(message); | 2096 return get(message); |
2283 } | 2097 } |
2284 @Override | 2098 @Override |
2285 public Object getRaw(GeneratedMessage.Builder builder) { | 2099 public Object getRaw(GeneratedMessageV3.Builder builder) { |
2286 return get(builder); | 2100 return get(builder); |
2287 } | 2101 } |
2288 @Override | 2102 @Override |
2289 public void set(final Builder builder, final Object value) { | 2103 public void set(final Builder builder, final Object value) { |
2290 // Add all the elements individually. This serves two purposes: | 2104 // Add all the elements individually. This serves two purposes: |
2291 // 1) Verifies that each element has the correct type. | 2105 // 1) Verifies that each element has the correct type. |
2292 // 2) Insures that the caller cannot modify the list later on and | 2106 // 2) Insures that the caller cannot modify the list later on and |
2293 // have the modifications be reflected in the message. | 2107 // have the modifications be reflected in the message. |
2294 clear(builder); | 2108 clear(builder); |
2295 for (final Object element : (List<?>) value) { | 2109 for (final Object element : (List<?>) value) { |
2296 addRepeated(builder, element); | 2110 addRepeated(builder, element); |
2297 } | 2111 } |
2298 } | 2112 } |
2299 @Override | 2113 @Override |
2300 public Object getRepeated(final GeneratedMessage message, final int index)
{ | 2114 public Object getRepeated(final GeneratedMessageV3 message, final int inde
x) { |
2301 return invokeOrDie(getRepeatedMethod, message, index); | 2115 return invokeOrDie(getRepeatedMethod, message, index); |
2302 } | 2116 } |
2303 @Override | 2117 @Override |
2304 public Object getRepeated(GeneratedMessage.Builder builder, int index) { | 2118 public Object getRepeated(GeneratedMessageV3.Builder builder, int index) { |
2305 return invokeOrDie(getRepeatedMethodBuilder, builder, index); | 2119 return invokeOrDie(getRepeatedMethodBuilder, builder, index); |
2306 } | 2120 } |
2307 @Override | 2121 @Override |
2308 public Object getRepeatedRaw(GeneratedMessage message, int index) { | 2122 public Object getRepeatedRaw(GeneratedMessageV3 message, int index) { |
2309 return getRepeated(message, index); | 2123 return getRepeated(message, index); |
2310 } | 2124 } |
2311 @Override | 2125 @Override |
2312 public Object getRepeatedRaw(GeneratedMessage.Builder builder, int index)
{ | 2126 public Object getRepeatedRaw(GeneratedMessageV3.Builder builder, int index
) { |
2313 return getRepeated(builder, index); | 2127 return getRepeated(builder, index); |
2314 } | 2128 } |
2315 @Override | 2129 @Override |
2316 public void setRepeated(final Builder builder, final int index, final Obje
ct value) { | 2130 public void setRepeated(final Builder builder, final int index, final Obje
ct value) { |
2317 invokeOrDie(setRepeatedMethod, builder, index, value); | 2131 invokeOrDie(setRepeatedMethod, builder, index, value); |
2318 } | 2132 } |
2319 @Override | 2133 @Override |
2320 public void addRepeated(final Builder builder, final Object value) { | 2134 public void addRepeated(final Builder builder, final Object value) { |
2321 invokeOrDie(addRepeatedMethod, builder, value); | 2135 invokeOrDie(addRepeatedMethod, builder, value); |
2322 } | 2136 } |
2323 @Override | 2137 @Override |
2324 public boolean has(final GeneratedMessage message) { | 2138 public boolean has(final GeneratedMessageV3 message) { |
2325 throw new UnsupportedOperationException( | 2139 throw new UnsupportedOperationException( |
2326 "hasField() called on a repeated field."); | 2140 "hasField() called on a repeated field."); |
2327 } | 2141 } |
2328 @Override | 2142 @Override |
2329 public boolean has(GeneratedMessage.Builder builder) { | 2143 public boolean has(GeneratedMessageV3.Builder builder) { |
2330 throw new UnsupportedOperationException( | 2144 throw new UnsupportedOperationException( |
2331 "hasField() called on a repeated field."); | 2145 "hasField() called on a repeated field."); |
2332 } | 2146 } |
2333 @Override | 2147 @Override |
2334 public int getRepeatedCount(final GeneratedMessage message) { | 2148 public int getRepeatedCount(final GeneratedMessageV3 message) { |
2335 return (Integer) invokeOrDie(getCountMethod, message); | 2149 return (Integer) invokeOrDie(getCountMethod, message); |
2336 } | 2150 } |
2337 @Override | 2151 @Override |
2338 public int getRepeatedCount(GeneratedMessage.Builder builder) { | 2152 public int getRepeatedCount(GeneratedMessageV3.Builder builder) { |
2339 return (Integer) invokeOrDie(getCountMethodBuilder, builder); | 2153 return (Integer) invokeOrDie(getCountMethodBuilder, builder); |
2340 } | 2154 } |
2341 @Override | 2155 @Override |
2342 public void clear(final Builder builder) { | 2156 public void clear(final Builder builder) { |
2343 invokeOrDie(clearMethod, builder); | 2157 invokeOrDie(clearMethod, builder); |
2344 } | 2158 } |
2345 @Override | 2159 @Override |
2346 public Message.Builder newBuilder() { | 2160 public Message.Builder newBuilder() { |
2347 throw new UnsupportedOperationException( | 2161 throw new UnsupportedOperationException( |
2348 "newBuilderForField() called on a non-Message type."); | 2162 "newBuilderForField() called on a non-Message type."); |
2349 } | 2163 } |
2350 @Override | 2164 @Override |
2351 public Message.Builder getBuilder(GeneratedMessage.Builder builder) { | 2165 public Message.Builder getBuilder(GeneratedMessageV3.Builder builder) { |
2352 throw new UnsupportedOperationException( | 2166 throw new UnsupportedOperationException( |
2353 "getFieldBuilder() called on a non-Message type."); | 2167 "getFieldBuilder() called on a non-Message type."); |
2354 } | 2168 } |
2355 @Override | 2169 @Override |
2356 public Message.Builder getRepeatedBuilder(GeneratedMessage.Builder builder
, int index) { | 2170 public Message.Builder getRepeatedBuilder(GeneratedMessageV3.Builder build
er, int index) { |
2357 throw new UnsupportedOperationException( | 2171 throw new UnsupportedOperationException( |
2358 "getRepeatedFieldBuilder() called on a non-Message type."); | 2172 "getRepeatedFieldBuilder() called on a non-Message type."); |
2359 } | 2173 } |
2360 } | 2174 } |
2361 | 2175 |
2362 private static class MapFieldAccessor implements FieldAccessor { | 2176 private static class MapFieldAccessor implements FieldAccessor { |
2363 MapFieldAccessor( | 2177 MapFieldAccessor( |
2364 final FieldDescriptor descriptor, final String camelCaseName, | 2178 final FieldDescriptor descriptor, final String camelCaseName, |
2365 final Class<? extends GeneratedMessage> messageClass, | 2179 final Class<? extends GeneratedMessageV3> messageClass, |
2366 final Class<? extends Builder> builderClass) { | 2180 final Class<? extends Builder> builderClass) { |
2367 field = descriptor; | 2181 field = descriptor; |
2368 Method getDefaultInstanceMethod = | 2182 Method getDefaultInstanceMethod = |
2369 getMethodOrDie(messageClass, "getDefaultInstance"); | 2183 getMethodOrDie(messageClass, "getDefaultInstance"); |
2370 MapField defaultMapField = getMapField( | 2184 MapField defaultMapField = getMapField( |
2371 (GeneratedMessage) invokeOrDie(getDefaultInstanceMethod, null)); | 2185 (GeneratedMessageV3) invokeOrDie(getDefaultInstanceMethod, null)); |
2372 mapEntryMessageDefaultInstance = | 2186 mapEntryMessageDefaultInstance = |
2373 defaultMapField.getMapEntryMessageDefaultInstance(); | 2187 defaultMapField.getMapEntryMessageDefaultInstance(); |
2374 } | 2188 } |
2375 | 2189 |
2376 private final FieldDescriptor field; | 2190 private final FieldDescriptor field; |
2377 private final Message mapEntryMessageDefaultInstance; | 2191 private final Message mapEntryMessageDefaultInstance; |
2378 | 2192 |
2379 private MapField<?, ?> getMapField(GeneratedMessage message) { | 2193 private MapField<?, ?> getMapField(GeneratedMessageV3 message) { |
2380 return (MapField<?, ?>) message.internalGetMapField(field.getNumber()); | 2194 return (MapField<?, ?>) message.internalGetMapField(field.getNumber()); |
2381 } | 2195 } |
2382 | 2196 |
2383 private MapField<?, ?> getMapField(GeneratedMessage.Builder builder) { | 2197 private MapField<?, ?> getMapField(GeneratedMessageV3.Builder builder) { |
2384 return (MapField<?, ?>) builder.internalGetMapField(field.getNumber()); | 2198 return (MapField<?, ?>) builder.internalGetMapField(field.getNumber()); |
2385 } | 2199 } |
2386 | 2200 |
2387 private MapField<?, ?> getMutableMapField( | 2201 private MapField<?, ?> getMutableMapField( |
2388 GeneratedMessage.Builder builder) { | 2202 GeneratedMessageV3.Builder builder) { |
2389 return (MapField<?, ?>) builder.internalGetMutableMapField( | 2203 return (MapField<?, ?>) builder.internalGetMutableMapField( |
2390 field.getNumber()); | 2204 field.getNumber()); |
2391 } | 2205 } |
2392 | 2206 |
| 2207 private Message coerceType(Message value) { |
| 2208 if (value == null) { |
| 2209 return null; |
| 2210 } |
| 2211 if (mapEntryMessageDefaultInstance.getClass().isInstance(value)) { |
| 2212 return value; |
| 2213 } |
| 2214 // The value is not the exact right message type. However, if it |
| 2215 // is an alternative implementation of the same type -- e.g. a |
| 2216 // DynamicMessage -- we should accept it. In this case we can make |
| 2217 // a copy of the message. |
| 2218 return mapEntryMessageDefaultInstance.toBuilder().mergeFrom(value).build
(); |
| 2219 } |
| 2220 |
2393 @Override | 2221 @Override |
2394 public Object get(GeneratedMessage message) { | 2222 public Object get(GeneratedMessageV3 message) { |
2395 List result = new ArrayList(); | 2223 List result = new ArrayList(); |
2396 for (int i = 0; i < getRepeatedCount(message); i++) { | 2224 for (int i = 0; i < getRepeatedCount(message); i++) { |
2397 result.add(getRepeated(message, i)); | 2225 result.add(getRepeated(message, i)); |
2398 } | 2226 } |
2399 return Collections.unmodifiableList(result); | 2227 return Collections.unmodifiableList(result); |
2400 } | 2228 } |
2401 | 2229 |
2402 @Override | 2230 @Override |
2403 public Object get(Builder builder) { | 2231 public Object get(Builder builder) { |
2404 List result = new ArrayList(); | 2232 List result = new ArrayList(); |
2405 for (int i = 0; i < getRepeatedCount(builder); i++) { | 2233 for (int i = 0; i < getRepeatedCount(builder); i++) { |
2406 result.add(getRepeated(builder, i)); | 2234 result.add(getRepeated(builder, i)); |
2407 } | 2235 } |
2408 return Collections.unmodifiableList(result); | 2236 return Collections.unmodifiableList(result); |
2409 } | 2237 } |
2410 | 2238 |
2411 @Override | 2239 @Override |
2412 public Object getRaw(GeneratedMessage message) { | 2240 public Object getRaw(GeneratedMessageV3 message) { |
2413 return get(message); | 2241 return get(message); |
2414 } | 2242 } |
2415 | 2243 |
2416 @Override | 2244 @Override |
2417 public Object getRaw(GeneratedMessage.Builder builder) { | 2245 public Object getRaw(GeneratedMessageV3.Builder builder) { |
2418 return get(builder); | 2246 return get(builder); |
2419 } | 2247 } |
2420 | 2248 |
2421 @Override | 2249 @Override |
2422 public void set(Builder builder, Object value) { | 2250 public void set(Builder builder, Object value) { |
2423 clear(builder); | 2251 clear(builder); |
2424 for (Object entry : (List) value) { | 2252 for (Object entry : (List) value) { |
2425 addRepeated(builder, entry); | 2253 addRepeated(builder, entry); |
2426 } | 2254 } |
2427 } | 2255 } |
2428 | 2256 |
2429 @Override | 2257 @Override |
2430 public Object getRepeated(GeneratedMessage message, int index) { | 2258 public Object getRepeated(GeneratedMessageV3 message, int index) { |
2431 return getMapField(message).getList().get(index); | 2259 return getMapField(message).getList().get(index); |
2432 } | 2260 } |
2433 | 2261 |
2434 @Override | 2262 @Override |
2435 public Object getRepeated(Builder builder, int index) { | 2263 public Object getRepeated(Builder builder, int index) { |
2436 return getMapField(builder).getList().get(index); | 2264 return getMapField(builder).getList().get(index); |
2437 } | 2265 } |
2438 | 2266 |
2439 @Override | 2267 @Override |
2440 public Object getRepeatedRaw(GeneratedMessage message, int index) { | 2268 public Object getRepeatedRaw(GeneratedMessageV3 message, int index) { |
2441 return getRepeated(message, index); | 2269 return getRepeated(message, index); |
2442 } | 2270 } |
2443 | 2271 |
2444 @Override | 2272 @Override |
2445 public Object getRepeatedRaw(Builder builder, int index) { | 2273 public Object getRepeatedRaw(Builder builder, int index) { |
2446 return getRepeated(builder, index); | 2274 return getRepeated(builder, index); |
2447 } | 2275 } |
2448 | 2276 |
2449 @Override | 2277 @Override |
2450 public void setRepeated(Builder builder, int index, Object value) { | 2278 public void setRepeated(Builder builder, int index, Object value) { |
2451 getMutableMapField(builder).getMutableList().set(index, (Message) value)
; | 2279 getMutableMapField(builder).getMutableList().set(index, coerceType((Mess
age) value)); |
2452 } | 2280 } |
2453 | 2281 |
2454 @Override | 2282 @Override |
2455 public void addRepeated(Builder builder, Object value) { | 2283 public void addRepeated(Builder builder, Object value) { |
2456 getMutableMapField(builder).getMutableList().add((Message) value); | 2284 getMutableMapField(builder).getMutableList().add(coerceType((Message) va
lue)); |
2457 } | 2285 } |
2458 | 2286 |
2459 @Override | 2287 @Override |
2460 public boolean has(GeneratedMessage message) { | 2288 public boolean has(GeneratedMessageV3 message) { |
2461 throw new UnsupportedOperationException( | 2289 throw new UnsupportedOperationException( |
2462 "hasField() is not supported for repeated fields."); | 2290 "hasField() is not supported for repeated fields."); |
2463 } | 2291 } |
2464 | 2292 |
2465 @Override | 2293 @Override |
2466 public boolean has(Builder builder) { | 2294 public boolean has(Builder builder) { |
2467 throw new UnsupportedOperationException( | 2295 throw new UnsupportedOperationException( |
2468 "hasField() is not supported for repeated fields."); | 2296 "hasField() is not supported for repeated fields."); |
2469 } | 2297 } |
2470 | 2298 |
2471 @Override | 2299 @Override |
2472 public int getRepeatedCount(GeneratedMessage message) { | 2300 public int getRepeatedCount(GeneratedMessageV3 message) { |
2473 return getMapField(message).getList().size(); | 2301 return getMapField(message).getList().size(); |
2474 } | 2302 } |
2475 | 2303 |
2476 @Override | 2304 @Override |
2477 public int getRepeatedCount(Builder builder) { | 2305 public int getRepeatedCount(Builder builder) { |
2478 return getMapField(builder).getList().size(); | 2306 return getMapField(builder).getList().size(); |
2479 } | 2307 } |
2480 | 2308 |
2481 @Override | 2309 @Override |
2482 public void clear(Builder builder) { | 2310 public void clear(Builder builder) { |
(...skipping 17 matching lines...) Expand all Loading... |
2500 "Nested builder not supported for map fields."); | 2328 "Nested builder not supported for map fields."); |
2501 } | 2329 } |
2502 } | 2330 } |
2503 | 2331 |
2504 // --------------------------------------------------------------- | 2332 // --------------------------------------------------------------- |
2505 | 2333 |
2506 private static final class SingularEnumFieldAccessor | 2334 private static final class SingularEnumFieldAccessor |
2507 extends SingularFieldAccessor { | 2335 extends SingularFieldAccessor { |
2508 SingularEnumFieldAccessor( | 2336 SingularEnumFieldAccessor( |
2509 final FieldDescriptor descriptor, final String camelCaseName, | 2337 final FieldDescriptor descriptor, final String camelCaseName, |
2510 final Class<? extends GeneratedMessage> messageClass, | 2338 final Class<? extends GeneratedMessageV3> messageClass, |
2511 final Class<? extends Builder> builderClass, | 2339 final Class<? extends Builder> builderClass, |
2512 final String containingOneofCamelCaseName) { | 2340 final String containingOneofCamelCaseName) { |
2513 super(descriptor, camelCaseName, messageClass, builderClass, containingO
neofCamelCaseName); | 2341 super(descriptor, camelCaseName, messageClass, builderClass, containingO
neofCamelCaseName); |
2514 | 2342 |
2515 enumDescriptor = descriptor.getEnumType(); | 2343 enumDescriptor = descriptor.getEnumType(); |
2516 | 2344 |
2517 valueOfMethod = getMethodOrDie(type, "valueOf", | 2345 valueOfMethod = getMethodOrDie(type, "valueOf", |
2518 EnumValueDescriptor.class); | 2346 EnumValueDescriptor.class); |
2519 getValueDescriptorMethod = | 2347 getValueDescriptorMethod = |
2520 getMethodOrDie(type, "getValueDescriptor"); | 2348 getMethodOrDie(type, "getValueDescriptor"); |
(...skipping 13 matching lines...) Expand all Loading... |
2534 | 2362 |
2535 private Method valueOfMethod; | 2363 private Method valueOfMethod; |
2536 private Method getValueDescriptorMethod; | 2364 private Method getValueDescriptorMethod; |
2537 | 2365 |
2538 private boolean supportUnknownEnumValue; | 2366 private boolean supportUnknownEnumValue; |
2539 private Method getValueMethod; | 2367 private Method getValueMethod; |
2540 private Method getValueMethodBuilder; | 2368 private Method getValueMethodBuilder; |
2541 private Method setValueMethod; | 2369 private Method setValueMethod; |
2542 | 2370 |
2543 @Override | 2371 @Override |
2544 public Object get(final GeneratedMessage message) { | 2372 public Object get(final GeneratedMessageV3 message) { |
2545 if (supportUnknownEnumValue) { | 2373 if (supportUnknownEnumValue) { |
2546 int value = (Integer) invokeOrDie(getValueMethod, message); | 2374 int value = (Integer) invokeOrDie(getValueMethod, message); |
2547 return enumDescriptor.findValueByNumberCreatingIfUnknown(value); | 2375 return enumDescriptor.findValueByNumberCreatingIfUnknown(value); |
2548 } | 2376 } |
2549 return invokeOrDie(getValueDescriptorMethod, super.get(message)); | 2377 return invokeOrDie(getValueDescriptorMethod, super.get(message)); |
2550 } | 2378 } |
2551 | 2379 |
2552 @Override | 2380 @Override |
2553 public Object get(final GeneratedMessage.Builder builder) { | 2381 public Object get(final GeneratedMessageV3.Builder builder) { |
2554 if (supportUnknownEnumValue) { | 2382 if (supportUnknownEnumValue) { |
2555 int value = (Integer) invokeOrDie(getValueMethodBuilder, builder); | 2383 int value = (Integer) invokeOrDie(getValueMethodBuilder, builder); |
2556 return enumDescriptor.findValueByNumberCreatingIfUnknown(value); | 2384 return enumDescriptor.findValueByNumberCreatingIfUnknown(value); |
2557 } | 2385 } |
2558 return invokeOrDie(getValueDescriptorMethod, super.get(builder)); | 2386 return invokeOrDie(getValueDescriptorMethod, super.get(builder)); |
2559 } | 2387 } |
2560 | 2388 |
2561 @Override | 2389 @Override |
2562 public void set(final Builder builder, final Object value) { | 2390 public void set(final Builder builder, final Object value) { |
2563 if (supportUnknownEnumValue) { | 2391 if (supportUnknownEnumValue) { |
2564 invokeOrDie(setValueMethod, builder, | 2392 invokeOrDie(setValueMethod, builder, |
2565 ((EnumValueDescriptor) value).getNumber()); | 2393 ((EnumValueDescriptor) value).getNumber()); |
2566 return; | 2394 return; |
2567 } | 2395 } |
2568 super.set(builder, invokeOrDie(valueOfMethod, null, value)); | 2396 super.set(builder, invokeOrDie(valueOfMethod, null, value)); |
2569 } | 2397 } |
2570 } | 2398 } |
2571 | 2399 |
2572 private static final class RepeatedEnumFieldAccessor | 2400 private static final class RepeatedEnumFieldAccessor |
2573 extends RepeatedFieldAccessor { | 2401 extends RepeatedFieldAccessor { |
2574 RepeatedEnumFieldAccessor( | 2402 RepeatedEnumFieldAccessor( |
2575 final FieldDescriptor descriptor, final String camelCaseName, | 2403 final FieldDescriptor descriptor, final String camelCaseName, |
2576 final Class<? extends GeneratedMessage> messageClass, | 2404 final Class<? extends GeneratedMessageV3> messageClass, |
2577 final Class<? extends Builder> builderClass) { | 2405 final Class<? extends Builder> builderClass) { |
2578 super(descriptor, camelCaseName, messageClass, builderClass); | 2406 super(descriptor, camelCaseName, messageClass, builderClass); |
2579 | 2407 |
2580 enumDescriptor = descriptor.getEnumType(); | 2408 enumDescriptor = descriptor.getEnumType(); |
2581 | 2409 |
2582 valueOfMethod = getMethodOrDie(type, "valueOf", | 2410 valueOfMethod = getMethodOrDie(type, "valueOf", |
2583 EnumValueDescriptor.class); | 2411 EnumValueDescriptor.class); |
2584 getValueDescriptorMethod = | 2412 getValueDescriptorMethod = |
2585 getMethodOrDie(type, "getValueDescriptor"); | 2413 getMethodOrDie(type, "getValueDescriptor"); |
2586 | 2414 |
(...skipping 15 matching lines...) Expand all Loading... |
2602 private final Method getValueDescriptorMethod; | 2430 private final Method getValueDescriptorMethod; |
2603 | 2431 |
2604 private boolean supportUnknownEnumValue; | 2432 private boolean supportUnknownEnumValue; |
2605 private Method getRepeatedValueMethod; | 2433 private Method getRepeatedValueMethod; |
2606 private Method getRepeatedValueMethodBuilder; | 2434 private Method getRepeatedValueMethodBuilder; |
2607 private Method setRepeatedValueMethod; | 2435 private Method setRepeatedValueMethod; |
2608 private Method addRepeatedValueMethod; | 2436 private Method addRepeatedValueMethod; |
2609 | 2437 |
2610 @Override | 2438 @Override |
2611 @SuppressWarnings("unchecked") | 2439 @SuppressWarnings("unchecked") |
2612 public Object get(final GeneratedMessage message) { | 2440 public Object get(final GeneratedMessageV3 message) { |
2613 final List newList = new ArrayList(); | 2441 final List newList = new ArrayList(); |
2614 final int size = getRepeatedCount(message); | 2442 final int size = getRepeatedCount(message); |
2615 for (int i = 0; i < size; i++) { | 2443 for (int i = 0; i < size; i++) { |
2616 newList.add(getRepeated(message, i)); | 2444 newList.add(getRepeated(message, i)); |
2617 } | 2445 } |
2618 return Collections.unmodifiableList(newList); | 2446 return Collections.unmodifiableList(newList); |
2619 } | 2447 } |
2620 | 2448 |
2621 @Override | 2449 @Override |
2622 @SuppressWarnings("unchecked") | 2450 @SuppressWarnings("unchecked") |
2623 public Object get(final GeneratedMessage.Builder builder) { | 2451 public Object get(final GeneratedMessageV3.Builder builder) { |
2624 final List newList = new ArrayList(); | 2452 final List newList = new ArrayList(); |
2625 final int size = getRepeatedCount(builder); | 2453 final int size = getRepeatedCount(builder); |
2626 for (int i = 0; i < size; i++) { | 2454 for (int i = 0; i < size; i++) { |
2627 newList.add(getRepeated(builder, i)); | 2455 newList.add(getRepeated(builder, i)); |
2628 } | 2456 } |
2629 return Collections.unmodifiableList(newList); | 2457 return Collections.unmodifiableList(newList); |
2630 } | 2458 } |
2631 | 2459 |
2632 @Override | 2460 @Override |
2633 public Object getRepeated(final GeneratedMessage message, | 2461 public Object getRepeated(final GeneratedMessageV3 message, |
2634 final int index) { | 2462 final int index) { |
2635 if (supportUnknownEnumValue) { | 2463 if (supportUnknownEnumValue) { |
2636 int value = (Integer) invokeOrDie(getRepeatedValueMethod, message, ind
ex); | 2464 int value = (Integer) invokeOrDie(getRepeatedValueMethod, message, ind
ex); |
2637 return enumDescriptor.findValueByNumberCreatingIfUnknown(value); | 2465 return enumDescriptor.findValueByNumberCreatingIfUnknown(value); |
2638 } | 2466 } |
2639 return invokeOrDie(getValueDescriptorMethod, | 2467 return invokeOrDie(getValueDescriptorMethod, |
2640 super.getRepeated(message, index)); | 2468 super.getRepeated(message, index)); |
2641 } | 2469 } |
2642 @Override | 2470 @Override |
2643 public Object getRepeated(final GeneratedMessage.Builder builder, | 2471 public Object getRepeated(final GeneratedMessageV3.Builder builder, |
2644 final int index) { | 2472 final int index) { |
2645 if (supportUnknownEnumValue) { | 2473 if (supportUnknownEnumValue) { |
2646 int value = (Integer) invokeOrDie(getRepeatedValueMethodBuilder, build
er, index); | 2474 int value = (Integer) invokeOrDie(getRepeatedValueMethodBuilder, build
er, index); |
2647 return enumDescriptor.findValueByNumberCreatingIfUnknown(value); | 2475 return enumDescriptor.findValueByNumberCreatingIfUnknown(value); |
2648 } | 2476 } |
2649 return invokeOrDie(getValueDescriptorMethod, | 2477 return invokeOrDie(getValueDescriptorMethod, |
2650 super.getRepeated(builder, index)); | 2478 super.getRepeated(builder, index)); |
2651 } | 2479 } |
2652 @Override | 2480 @Override |
2653 public void setRepeated(final Builder builder, | 2481 public void setRepeated(final Builder builder, |
(...skipping 27 matching lines...) Expand all Loading... |
2681 * access the raw bytes of the field to preserve non-UTF8 bytes in the | 2509 * access the raw bytes of the field to preserve non-UTF8 bytes in the |
2682 * string. | 2510 * string. |
2683 * | 2511 * |
2684 * <p>This ensures the serialize/parse round-trip safety, which is important | 2512 * <p>This ensures the serialize/parse round-trip safety, which is important |
2685 * for servers which forward messages. | 2513 * for servers which forward messages. |
2686 */ | 2514 */ |
2687 private static final class SingularStringFieldAccessor | 2515 private static final class SingularStringFieldAccessor |
2688 extends SingularFieldAccessor { | 2516 extends SingularFieldAccessor { |
2689 SingularStringFieldAccessor( | 2517 SingularStringFieldAccessor( |
2690 final FieldDescriptor descriptor, final String camelCaseName, | 2518 final FieldDescriptor descriptor, final String camelCaseName, |
2691 final Class<? extends GeneratedMessage> messageClass, | 2519 final Class<? extends GeneratedMessageV3> messageClass, |
2692 final Class<? extends Builder> builderClass, | 2520 final Class<? extends Builder> builderClass, |
2693 final String containingOneofCamelCaseName) { | 2521 final String containingOneofCamelCaseName) { |
2694 super(descriptor, camelCaseName, messageClass, builderClass, | 2522 super(descriptor, camelCaseName, messageClass, builderClass, |
2695 containingOneofCamelCaseName); | 2523 containingOneofCamelCaseName); |
2696 getBytesMethod = getMethodOrDie(messageClass, | 2524 getBytesMethod = getMethodOrDie(messageClass, |
2697 "get" + camelCaseName + "Bytes"); | 2525 "get" + camelCaseName + "Bytes"); |
2698 getBytesMethodBuilder = getMethodOrDie(builderClass, | 2526 getBytesMethodBuilder = getMethodOrDie(builderClass, |
2699 "get" + camelCaseName + "Bytes"); | 2527 "get" + camelCaseName + "Bytes"); |
2700 setBytesMethodBuilder = getMethodOrDie(builderClass, | 2528 setBytesMethodBuilder = getMethodOrDie(builderClass, |
2701 "set" + camelCaseName + "Bytes", ByteString.class); | 2529 "set" + camelCaseName + "Bytes", ByteString.class); |
2702 } | 2530 } |
2703 | 2531 |
2704 private final Method getBytesMethod; | 2532 private final Method getBytesMethod; |
2705 private final Method getBytesMethodBuilder; | 2533 private final Method getBytesMethodBuilder; |
2706 private final Method setBytesMethodBuilder; | 2534 private final Method setBytesMethodBuilder; |
2707 | 2535 |
2708 @Override | 2536 @Override |
2709 public Object getRaw(final GeneratedMessage message) { | 2537 public Object getRaw(final GeneratedMessageV3 message) { |
2710 return invokeOrDie(getBytesMethod, message); | 2538 return invokeOrDie(getBytesMethod, message); |
2711 } | 2539 } |
2712 | 2540 |
2713 @Override | 2541 @Override |
2714 public Object getRaw(GeneratedMessage.Builder builder) { | 2542 public Object getRaw(GeneratedMessageV3.Builder builder) { |
2715 return invokeOrDie(getBytesMethodBuilder, builder); | 2543 return invokeOrDie(getBytesMethodBuilder, builder); |
2716 } | 2544 } |
2717 | 2545 |
2718 @Override | 2546 @Override |
2719 public void set(GeneratedMessage.Builder builder, Object value) { | 2547 public void set(GeneratedMessageV3.Builder builder, Object value) { |
2720 if (value instanceof ByteString) { | 2548 if (value instanceof ByteString) { |
2721 invokeOrDie(setBytesMethodBuilder, builder, value); | 2549 invokeOrDie(setBytesMethodBuilder, builder, value); |
2722 } else { | 2550 } else { |
2723 super.set(builder, value); | 2551 super.set(builder, value); |
2724 } | 2552 } |
2725 } | 2553 } |
2726 } | 2554 } |
2727 | 2555 |
2728 // --------------------------------------------------------------- | 2556 // --------------------------------------------------------------- |
2729 | 2557 |
2730 private static final class SingularMessageFieldAccessor | 2558 private static final class SingularMessageFieldAccessor |
2731 extends SingularFieldAccessor { | 2559 extends SingularFieldAccessor { |
2732 SingularMessageFieldAccessor( | 2560 SingularMessageFieldAccessor( |
2733 final FieldDescriptor descriptor, final String camelCaseName, | 2561 final FieldDescriptor descriptor, final String camelCaseName, |
2734 final Class<? extends GeneratedMessage> messageClass, | 2562 final Class<? extends GeneratedMessageV3> messageClass, |
2735 final Class<? extends Builder> builderClass, | 2563 final Class<? extends Builder> builderClass, |
2736 final String containingOneofCamelCaseName) { | 2564 final String containingOneofCamelCaseName) { |
2737 super(descriptor, camelCaseName, messageClass, builderClass, | 2565 super(descriptor, camelCaseName, messageClass, builderClass, |
2738 containingOneofCamelCaseName); | 2566 containingOneofCamelCaseName); |
2739 | 2567 |
2740 newBuilderMethod = getMethodOrDie(type, "newBuilder"); | 2568 newBuilderMethod = getMethodOrDie(type, "newBuilder"); |
2741 getBuilderMethodBuilder = | 2569 getBuilderMethodBuilder = |
2742 getMethodOrDie(builderClass, "get" + camelCaseName + "Builder"); | 2570 getMethodOrDie(builderClass, "get" + camelCaseName + "Builder"); |
2743 } | 2571 } |
2744 | 2572 |
(...skipping 15 matching lines...) Expand all Loading... |
2760 | 2588 |
2761 @Override | 2589 @Override |
2762 public void set(final Builder builder, final Object value) { | 2590 public void set(final Builder builder, final Object value) { |
2763 super.set(builder, coerceType(value)); | 2591 super.set(builder, coerceType(value)); |
2764 } | 2592 } |
2765 @Override | 2593 @Override |
2766 public Message.Builder newBuilder() { | 2594 public Message.Builder newBuilder() { |
2767 return (Message.Builder) invokeOrDie(newBuilderMethod, null); | 2595 return (Message.Builder) invokeOrDie(newBuilderMethod, null); |
2768 } | 2596 } |
2769 @Override | 2597 @Override |
2770 public Message.Builder getBuilder(GeneratedMessage.Builder builder) { | 2598 public Message.Builder getBuilder(GeneratedMessageV3.Builder builder) { |
2771 return (Message.Builder) invokeOrDie(getBuilderMethodBuilder, builder); | 2599 return (Message.Builder) invokeOrDie(getBuilderMethodBuilder, builder); |
2772 } | 2600 } |
2773 } | 2601 } |
2774 | 2602 |
2775 private static final class RepeatedMessageFieldAccessor | 2603 private static final class RepeatedMessageFieldAccessor |
2776 extends RepeatedFieldAccessor { | 2604 extends RepeatedFieldAccessor { |
2777 RepeatedMessageFieldAccessor( | 2605 RepeatedMessageFieldAccessor( |
2778 final FieldDescriptor descriptor, final String camelCaseName, | 2606 final FieldDescriptor descriptor, final String camelCaseName, |
2779 final Class<? extends GeneratedMessage> messageClass, | 2607 final Class<? extends GeneratedMessageV3> messageClass, |
2780 final Class<? extends Builder> builderClass) { | 2608 final Class<? extends Builder> builderClass) { |
2781 super(descriptor, camelCaseName, messageClass, builderClass); | 2609 super(descriptor, camelCaseName, messageClass, builderClass); |
2782 | 2610 |
2783 newBuilderMethod = getMethodOrDie(type, "newBuilder"); | 2611 newBuilderMethod = getMethodOrDie(type, "newBuilder"); |
2784 getBuilderMethodBuilder = getMethodOrDie(builderClass, | 2612 getBuilderMethodBuilder = getMethodOrDie(builderClass, |
2785 "get" + camelCaseName + "Builder", Integer.TYPE); | 2613 "get" + camelCaseName + "Builder", Integer.TYPE); |
2786 } | 2614 } |
2787 | 2615 |
2788 private final Method newBuilderMethod; | 2616 private final Method newBuilderMethod; |
2789 private final Method getBuilderMethodBuilder; | 2617 private final Method getBuilderMethodBuilder; |
(...skipping 19 matching lines...) Expand all Loading... |
2809 @Override | 2637 @Override |
2810 public void addRepeated(final Builder builder, final Object value) { | 2638 public void addRepeated(final Builder builder, final Object value) { |
2811 super.addRepeated(builder, coerceType(value)); | 2639 super.addRepeated(builder, coerceType(value)); |
2812 } | 2640 } |
2813 @Override | 2641 @Override |
2814 public Message.Builder newBuilder() { | 2642 public Message.Builder newBuilder() { |
2815 return (Message.Builder) invokeOrDie(newBuilderMethod, null); | 2643 return (Message.Builder) invokeOrDie(newBuilderMethod, null); |
2816 } | 2644 } |
2817 @Override | 2645 @Override |
2818 public Message.Builder getRepeatedBuilder( | 2646 public Message.Builder getRepeatedBuilder( |
2819 final GeneratedMessage.Builder builder, final int index) { | 2647 final GeneratedMessageV3.Builder builder, final int index) { |
2820 return (Message.Builder) invokeOrDie( | 2648 return (Message.Builder) invokeOrDie( |
2821 getBuilderMethodBuilder, builder, index); | 2649 getBuilderMethodBuilder, builder, index); |
2822 } | 2650 } |
2823 } | 2651 } |
2824 } | 2652 } |
2825 | 2653 |
2826 /** | 2654 /** |
2827 * Replaces this object in the output stream with a serialized form. | 2655 * Replaces this object in the output stream with a serialized form. |
2828 * Part of Java's serialization magic. Generated sub-classes must override | 2656 * Part of Java's serialization magic. Generated sub-classes must override |
2829 * this method by calling {@code return super.writeReplace();} | 2657 * this method by calling {@code return super.writeReplace();} |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2873 } | 2701 } |
2874 | 2702 |
2875 protected static void writeStringNoTag( | 2703 protected static void writeStringNoTag( |
2876 CodedOutputStream output, final Object value) throws IOException { | 2704 CodedOutputStream output, final Object value) throws IOException { |
2877 if (value instanceof String) { | 2705 if (value instanceof String) { |
2878 output.writeStringNoTag((String) value); | 2706 output.writeStringNoTag((String) value); |
2879 } else { | 2707 } else { |
2880 output.writeBytesNoTag((ByteString) value); | 2708 output.writeBytesNoTag((ByteString) value); |
2881 } | 2709 } |
2882 } | 2710 } |
| 2711 |
| 2712 protected static <V> void serializeIntegerMapTo( |
| 2713 CodedOutputStream out, |
| 2714 MapField<Integer, V> field, |
| 2715 MapEntry<Integer, V> defaultEntry, |
| 2716 int fieldNumber) throws IOException { |
| 2717 Map<Integer, V> m = field.getMap(); |
| 2718 if (!out.isSerializationDeterministic()) { |
| 2719 serializeMapTo(out, m, defaultEntry, fieldNumber); |
| 2720 return; |
| 2721 } |
| 2722 // Sorting the unboxed keys and then look up the values during serialziation
is 2x faster |
| 2723 // than sorting map entries with a custom comparator directly. |
| 2724 int[] keys = new int[m.size()]; |
| 2725 int index = 0; |
| 2726 for (int k : m.keySet()) { |
| 2727 keys[index++] = k; |
| 2728 } |
| 2729 Arrays.sort(keys); |
| 2730 for (int key : keys) { |
| 2731 out.writeMessage(fieldNumber, |
| 2732 defaultEntry.newBuilderForType() |
| 2733 .setKey(key) |
| 2734 .setValue(m.get(key)) |
| 2735 .build()); |
| 2736 } |
| 2737 } |
| 2738 |
| 2739 protected static <V> void serializeLongMapTo( |
| 2740 CodedOutputStream out, |
| 2741 MapField<Long, V> field, |
| 2742 MapEntry<Long, V> defaultEntry, |
| 2743 int fieldNumber) |
| 2744 throws IOException { |
| 2745 Map<Long, V> m = field.getMap(); |
| 2746 if (!out.isSerializationDeterministic()) { |
| 2747 serializeMapTo(out, m, defaultEntry, fieldNumber); |
| 2748 return; |
| 2749 } |
| 2750 |
| 2751 long[] keys = new long[m.size()]; |
| 2752 int index = 0; |
| 2753 for (long k : m.keySet()) { |
| 2754 keys[index++] = k; |
| 2755 } |
| 2756 Arrays.sort(keys); |
| 2757 for (long key : keys) { |
| 2758 out.writeMessage(fieldNumber, |
| 2759 defaultEntry.newBuilderForType() |
| 2760 .setKey(key) |
| 2761 .setValue(m.get(key)) |
| 2762 .build()); |
| 2763 } |
| 2764 } |
| 2765 |
| 2766 protected static <V> void serializeStringMapTo( |
| 2767 CodedOutputStream out, |
| 2768 MapField<String, V> field, |
| 2769 MapEntry<String, V> defaultEntry, |
| 2770 int fieldNumber) |
| 2771 throws IOException { |
| 2772 Map<String, V> m = field.getMap(); |
| 2773 if (!out.isSerializationDeterministic()) { |
| 2774 serializeMapTo(out, m, defaultEntry, fieldNumber); |
| 2775 return; |
| 2776 } |
| 2777 |
| 2778 // Sorting the String keys and then look up the values during serialziation
is 25% faster than |
| 2779 // sorting map entries with a custom comparator directly. |
| 2780 String[] keys = new String[m.size()]; |
| 2781 keys = m.keySet().toArray(keys); |
| 2782 Arrays.sort(keys); |
| 2783 for (String key : keys) { |
| 2784 out.writeMessage(fieldNumber, |
| 2785 defaultEntry.newBuilderForType() |
| 2786 .setKey(key) |
| 2787 .setValue(m.get(key)) |
| 2788 .build()); |
| 2789 } |
| 2790 } |
| 2791 |
| 2792 protected static <V> void serializeBooleanMapTo( |
| 2793 CodedOutputStream out, |
| 2794 MapField<Boolean, V> field, |
| 2795 MapEntry<Boolean, V> defaultEntry, |
| 2796 int fieldNumber) |
| 2797 throws IOException { |
| 2798 Map<Boolean, V> m = field.getMap(); |
| 2799 if (!out.isSerializationDeterministic()) { |
| 2800 serializeMapTo(out, m, defaultEntry, fieldNumber); |
| 2801 return; |
| 2802 } |
| 2803 maybeSerializeBooleanEntryTo(out, m, defaultEntry, fieldNumber, false); |
| 2804 maybeSerializeBooleanEntryTo(out, m, defaultEntry, fieldNumber, true); |
| 2805 } |
| 2806 |
| 2807 private static <V> void maybeSerializeBooleanEntryTo( |
| 2808 CodedOutputStream out, |
| 2809 Map<Boolean, V> m, |
| 2810 MapEntry<Boolean, V> defaultEntry, |
| 2811 int fieldNumber, |
| 2812 boolean key) |
| 2813 throws IOException { |
| 2814 if (m.containsKey(key)) { |
| 2815 out.writeMessage(fieldNumber, |
| 2816 defaultEntry.newBuilderForType() |
| 2817 .setKey(key) |
| 2818 .setValue(m.get(key)) |
| 2819 .build()); |
| 2820 } |
| 2821 } |
| 2822 |
| 2823 /** Serialize the map using the iteration order. */ |
| 2824 private static <K, V> void serializeMapTo( |
| 2825 CodedOutputStream out, |
| 2826 Map<K, V> m, |
| 2827 MapEntry<K, V> defaultEntry, |
| 2828 int fieldNumber) |
| 2829 throws IOException { |
| 2830 for (Map.Entry<K, V> entry : m.entrySet()) { |
| 2831 out.writeMessage(fieldNumber, |
| 2832 defaultEntry.newBuilderForType() |
| 2833 .setKey(entry.getKey()) |
| 2834 .setValue(entry.getValue()) |
| 2835 .build()); |
| 2836 } |
| 2837 } |
2883 } | 2838 } |
OLD | NEW |