| OLD | NEW |
| (Empty) |
| 1 // Protocol Buffers - Google's data interchange format | |
| 2 // Copyright 2008 Google Inc. All rights reserved. | |
| 3 // https://developers.google.com/protocol-buffers/ | |
| 4 // | |
| 5 // Redistribution and use in source and binary forms, with or without | |
| 6 // modification, are permitted provided that the following conditions are | |
| 7 // met: | |
| 8 // | |
| 9 // * Redistributions of source code must retain the above copyright | |
| 10 // notice, this list of conditions and the following disclaimer. | |
| 11 // * Redistributions in binary form must reproduce the above | |
| 12 // copyright notice, this list of conditions and the following disclaimer | |
| 13 // in the documentation and/or other materials provided with the | |
| 14 // distribution. | |
| 15 // * Neither the name of Google Inc. nor the names of its | |
| 16 // contributors may be used to endorse or promote products derived from | |
| 17 // this software without specific prior written permission. | |
| 18 // | |
| 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 30 | |
| 31 package com.google.protobuf; | |
| 32 | |
| 33 import com.google.protobuf.Descriptors.Descriptor; | |
| 34 import com.google.protobuf.Descriptors.EnumDescriptor; | |
| 35 import com.google.protobuf.Descriptors.EnumValueDescriptor; | |
| 36 import com.google.protobuf.Descriptors.FieldDescriptor; | |
| 37 import com.google.protobuf.Descriptors.FileDescriptor; | |
| 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; | |
| 50 | |
| 51 import java.io.IOException; | |
| 52 import java.io.InputStream; | |
| 53 import java.io.ObjectStreamException; | |
| 54 import java.io.Serializable; | |
| 55 import java.lang.reflect.InvocationTargetException; | |
| 56 import java.lang.reflect.Method; | |
| 57 import java.util.ArrayList; | |
| 58 import java.util.Arrays; | |
| 59 import java.util.Collections; | |
| 60 import java.util.Iterator; | |
| 61 import java.util.List; | |
| 62 import java.util.Map; | |
| 63 import java.util.TreeMap; | |
| 64 | |
| 65 /** | |
| 66 * All generated protocol message classes extend this class. This class | |
| 67 * implements most of the Message and Builder interfaces using Java reflection. | |
| 68 * Users can ignore this class and pretend that generated messages implement | |
| 69 * the Message interface directly. | |
| 70 * | |
| 71 * @author kenton@google.com Kenton Varda | |
| 72 */ | |
| 73 public abstract class GeneratedMessageV3 extends AbstractMessage | |
| 74 implements Serializable { | |
| 75 private static final long serialVersionUID = 1L; | |
| 76 | |
| 77 /** | |
| 78 * For testing. Allows a test to disable the optimization that avoids using | |
| 79 * field builders for nested messages until they are requested. By disabling | |
| 80 * this optimization, existing tests can be reused to test the field builders. | |
| 81 */ | |
| 82 protected static boolean alwaysUseFieldBuilders = false; | |
| 83 | |
| 84 /** For use by generated code only. */ | |
| 85 protected UnknownFieldSet unknownFields; | |
| 86 | |
| 87 protected GeneratedMessageV3() { | |
| 88 unknownFields = UnknownFieldSet.getDefaultInstance(); | |
| 89 } | |
| 90 | |
| 91 protected GeneratedMessageV3(Builder<?> builder) { | |
| 92 unknownFields = builder.getUnknownFields(); | |
| 93 } | |
| 94 | |
| 95 @Override | |
| 96 public Parser<? extends GeneratedMessageV3> getParserForType() { | |
| 97 throw new UnsupportedOperationException( | |
| 98 "This is supposed to be overridden by subclasses."); | |
| 99 } | |
| 100 | |
| 101 /** | |
| 102 * For testing. Allows a test to disable the optimization that avoids using | |
| 103 * field builders for nested messages until they are requested. By disabling | |
| 104 * this optimization, existing tests can be reused to test the field builders. | |
| 105 * See {@link RepeatedFieldBuilder} and {@link SingleFieldBuilder}. | |
| 106 */ | |
| 107 static void enableAlwaysUseFieldBuildersForTesting() { | |
| 108 alwaysUseFieldBuilders = true; | |
| 109 } | |
| 110 | |
| 111 /** | |
| 112 * Get the FieldAccessorTable for this type. We can't have the message | |
| 113 * class pass this in to the constructor because of bootstrapping trouble | |
| 114 * with DescriptorProtos. | |
| 115 */ | |
| 116 protected abstract FieldAccessorTable internalGetFieldAccessorTable(); | |
| 117 | |
| 118 @Override | |
| 119 public Descriptor getDescriptorForType() { | |
| 120 return internalGetFieldAccessorTable().descriptor; | |
| 121 } | |
| 122 | |
| 123 /** | |
| 124 * Internal helper to return a modifiable map containing all the fields. | |
| 125 * The returned Map is modifialbe so that the caller can add additional | |
| 126 * extension fields to implement {@link #getAllFields()}. | |
| 127 * | |
| 128 * @param getBytesForString whether to generate ByteString for string fields | |
| 129 */ | |
| 130 private Map<FieldDescriptor, Object> getAllFieldsMutable( | |
| 131 boolean getBytesForString) { | |
| 132 final TreeMap<FieldDescriptor, Object> result = | |
| 133 new TreeMap<FieldDescriptor, Object>(); | |
| 134 final Descriptor descriptor = internalGetFieldAccessorTable().descriptor; | |
| 135 final List<FieldDescriptor> fields = descriptor.getFields(); | |
| 136 | |
| 137 for (int i = 0; i < fields.size(); i++) { | |
| 138 FieldDescriptor field = fields.get(i); | |
| 139 final OneofDescriptor oneofDescriptor = field.getContainingOneof(); | |
| 140 | |
| 141 /* | |
| 142 * If the field is part of a Oneof, then at maximum one field in the Oneof
is set | |
| 143 * and it is not repeated. There is no need to iterate through the others. | |
| 144 */ | |
| 145 if (oneofDescriptor != null) { | |
| 146 // Skip other fields in the Oneof we know are not set | |
| 147 i += oneofDescriptor.getFieldCount() - 1; | |
| 148 if (!hasOneof(oneofDescriptor)) { | |
| 149 // If no field is set in the Oneof, skip all the fields in the Oneof | |
| 150 continue; | |
| 151 } | |
| 152 // Get the pointer to the only field which is set in the Oneof | |
| 153 field = getOneofFieldDescriptor(oneofDescriptor); | |
| 154 } else { | |
| 155 // If we are not in a Oneof, we need to check if the field is set and if
it is repeated | |
| 156 if (field.isRepeated()) { | |
| 157 final List<?> value = (List<?>) getField(field); | |
| 158 if (!value.isEmpty()) { | |
| 159 result.put(field, value); | |
| 160 } | |
| 161 continue; | |
| 162 } | |
| 163 if (!hasField(field)) { | |
| 164 continue; | |
| 165 } | |
| 166 } | |
| 167 // Add the field to the map | |
| 168 if (getBytesForString && field.getJavaType() == FieldDescriptor.JavaType.S
TRING) { | |
| 169 result.put(field, getFieldRaw(field)); | |
| 170 } else { | |
| 171 result.put(field, getField(field)); | |
| 172 } | |
| 173 } | |
| 174 return result; | |
| 175 } | |
| 176 | |
| 177 @Override | |
| 178 public boolean isInitialized() { | |
| 179 for (final FieldDescriptor field : getDescriptorForType().getFields()) { | |
| 180 // Check that all required fields are present. | |
| 181 if (field.isRequired()) { | |
| 182 if (!hasField(field)) { | |
| 183 return false; | |
| 184 } | |
| 185 } | |
| 186 // Check that embedded messages are initialized. | |
| 187 if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { | |
| 188 if (field.isRepeated()) { | |
| 189 @SuppressWarnings("unchecked") final | |
| 190 List<Message> messageList = (List<Message>) getField(field); | |
| 191 for (final Message element : messageList) { | |
| 192 if (!element.isInitialized()) { | |
| 193 return false; | |
| 194 } | |
| 195 } | |
| 196 } else { | |
| 197 if (hasField(field) && !((Message) getField(field)).isInitialized()) { | |
| 198 return false; | |
| 199 } | |
| 200 } | |
| 201 } | |
| 202 } | |
| 203 | |
| 204 return true; | |
| 205 } | |
| 206 | |
| 207 @Override | |
| 208 public Map<FieldDescriptor, Object> getAllFields() { | |
| 209 return Collections.unmodifiableMap( | |
| 210 getAllFieldsMutable(/* getBytesForString = */ false)); | |
| 211 } | |
| 212 | |
| 213 /** | |
| 214 * Returns a collection of all the fields in this message which are set | |
| 215 * and their corresponding values. A singular ("required" or "optional") | |
| 216 * field is set iff hasField() returns true for that field. A "repeated" | |
| 217 * field is set iff getRepeatedFieldCount() is greater than zero. The | |
| 218 * values are exactly what would be returned by calling | |
| 219 * {@link #getFieldRaw(Descriptors.FieldDescriptor)} for each field. The map | |
| 220 * is guaranteed to be a sorted map, so iterating over it will return fields | |
| 221 * in order by field number. | |
| 222 */ | |
| 223 Map<FieldDescriptor, Object> getAllFieldsRaw() { | |
| 224 return Collections.unmodifiableMap( | |
| 225 getAllFieldsMutable(/* getBytesForString = */ true)); | |
| 226 } | |
| 227 | |
| 228 @Override | |
| 229 public boolean hasOneof(final OneofDescriptor oneof) { | |
| 230 return internalGetFieldAccessorTable().getOneof(oneof).has(this); | |
| 231 } | |
| 232 | |
| 233 @Override | |
| 234 public FieldDescriptor getOneofFieldDescriptor(final OneofDescriptor oneof) { | |
| 235 return internalGetFieldAccessorTable().getOneof(oneof).get(this); | |
| 236 } | |
| 237 | |
| 238 @Override | |
| 239 public boolean hasField(final FieldDescriptor field) { | |
| 240 return internalGetFieldAccessorTable().getField(field).has(this); | |
| 241 } | |
| 242 | |
| 243 @Override | |
| 244 public Object getField(final FieldDescriptor field) { | |
| 245 return internalGetFieldAccessorTable().getField(field).get(this); | |
| 246 } | |
| 247 | |
| 248 /** | |
| 249 * Obtains the value of the given field, or the default value if it is | |
| 250 * not set. For primitive fields, the boxed primitive value is returned. | |
| 251 * For enum fields, the EnumValueDescriptor for the value is returned. For | |
| 252 * embedded message fields, the sub-message is returned. For repeated | |
| 253 * fields, a java.util.List is returned. For present string fields, a | |
| 254 * ByteString is returned representing the bytes that the field contains. | |
| 255 */ | |
| 256 Object getFieldRaw(final FieldDescriptor field) { | |
| 257 return internalGetFieldAccessorTable().getField(field).getRaw(this); | |
| 258 } | |
| 259 | |
| 260 @Override | |
| 261 public int getRepeatedFieldCount(final FieldDescriptor field) { | |
| 262 return internalGetFieldAccessorTable().getField(field) | |
| 263 .getRepeatedCount(this); | |
| 264 } | |
| 265 | |
| 266 @Override | |
| 267 public Object getRepeatedField(final FieldDescriptor field, final int index) { | |
| 268 return internalGetFieldAccessorTable().getField(field) | |
| 269 .getRepeated(this, index); | |
| 270 } | |
| 271 | |
| 272 @Override | |
| 273 public UnknownFieldSet getUnknownFields() { | |
| 274 throw new UnsupportedOperationException( | |
| 275 "This is supposed to be overridden by subclasses."); | |
| 276 } | |
| 277 | |
| 278 /** | |
| 279 * Called by subclasses to parse an unknown field. | |
| 280 * @return {@code true} unless the tag is an end-group tag. | |
| 281 */ | |
| 282 protected boolean parseUnknownField( | |
| 283 CodedInputStream input, | |
| 284 UnknownFieldSet.Builder unknownFields, | |
| 285 ExtensionRegistryLite extensionRegistry, | |
| 286 int tag) throws IOException { | |
| 287 return unknownFields.mergeFieldFrom(tag, input); | |
| 288 } | |
| 289 | |
| 290 protected static <M extends Message> M parseWithIOException(Parser<M> parser,
InputStream input) | |
| 291 throws IOException { | |
| 292 try { | |
| 293 return parser.parseFrom(input); | |
| 294 } catch (InvalidProtocolBufferException e) { | |
| 295 throw e.unwrapIOException(); | |
| 296 } | |
| 297 } | |
| 298 | |
| 299 protected static <M extends Message> M parseWithIOException(Parser<M> parser,
InputStream input, | |
| 300 ExtensionRegistryLite extensions) throws IOException { | |
| 301 try { | |
| 302 return parser.parseFrom(input, extensions); | |
| 303 } catch (InvalidProtocolBufferException e) { | |
| 304 throw e.unwrapIOException(); | |
| 305 } | |
| 306 } | |
| 307 | |
| 308 protected static <M extends Message> M parseWithIOException(Parser<M> parser, | |
| 309 CodedInputStream input) throws IOException { | |
| 310 try { | |
| 311 return parser.parseFrom(input); | |
| 312 } catch (InvalidProtocolBufferException e) { | |
| 313 throw e.unwrapIOException(); | |
| 314 } | |
| 315 } | |
| 316 | |
| 317 protected static <M extends Message> M parseWithIOException(Parser<M> parser, | |
| 318 CodedInputStream input, ExtensionRegistryLite extensions) throws IOExcepti
on { | |
| 319 try { | |
| 320 return parser.parseFrom(input, extensions); | |
| 321 } catch (InvalidProtocolBufferException e) { | |
| 322 throw e.unwrapIOException(); | |
| 323 } | |
| 324 } | |
| 325 | |
| 326 protected static <M extends Message> M parseDelimitedWithIOException(Parser<M>
parser, | |
| 327 InputStream input) throws IOException { | |
| 328 try { | |
| 329 return parser.parseDelimitedFrom(input); | |
| 330 } catch (InvalidProtocolBufferException e) { | |
| 331 throw e.unwrapIOException(); | |
| 332 } | |
| 333 } | |
| 334 | |
| 335 protected static <M extends Message> M parseDelimitedWithIOException(Parser<M>
parser, | |
| 336 InputStream input, ExtensionRegistryLite extensions) throws IOException { | |
| 337 try { | |
| 338 return parser.parseDelimitedFrom(input, extensions); | |
| 339 } catch (InvalidProtocolBufferException e) { | |
| 340 throw e.unwrapIOException(); | |
| 341 } | |
| 342 } | |
| 343 | |
| 344 @Override | |
| 345 public void writeTo(final CodedOutputStream output) throws IOException { | |
| 346 MessageReflection.writeMessageTo(this, getAllFieldsRaw(), output, false); | |
| 347 } | |
| 348 | |
| 349 @Override | |
| 350 public int getSerializedSize() { | |
| 351 int size = memoizedSize; | |
| 352 if (size != -1) { | |
| 353 return size; | |
| 354 } | |
| 355 | |
| 356 memoizedSize = MessageReflection.getSerializedSize( | |
| 357 this, getAllFieldsRaw()); | |
| 358 return memoizedSize; | |
| 359 } | |
| 360 | |
| 361 | |
| 362 | |
| 363 /** | |
| 364 * Used by parsing constructors in generated classes. | |
| 365 */ | |
| 366 protected void makeExtensionsImmutable() { | |
| 367 // Noop for messages without extensions. | |
| 368 } | |
| 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 */ | |
| 381 protected abstract Message.Builder newBuilderForType(BuilderParent parent); | |
| 382 | |
| 383 @Override | |
| 384 protected Message.Builder newBuilderForType(final AbstractMessage.BuilderParen
t parent) { | |
| 385 return newBuilderForType(new BuilderParent() { | |
| 386 @Override | |
| 387 public void markDirty() { | |
| 388 parent.markDirty(); | |
| 389 } | |
| 390 }); | |
| 391 } | |
| 392 | |
| 393 | |
| 394 @SuppressWarnings("unchecked") | |
| 395 public abstract static class Builder <BuilderType extends Builder<BuilderType>
> | |
| 396 extends AbstractMessage.Builder<BuilderType> { | |
| 397 | |
| 398 private BuilderParent builderParent; | |
| 399 | |
| 400 private BuilderParentImpl meAsParent; | |
| 401 | |
| 402 // Indicates that we've built a message and so we are now obligated | |
| 403 // to dispatch dirty invalidations. See GeneratedMessageV3.BuilderListener. | |
| 404 private boolean isClean; | |
| 405 | |
| 406 private UnknownFieldSet unknownFields = | |
| 407 UnknownFieldSet.getDefaultInstance(); | |
| 408 | |
| 409 protected Builder() { | |
| 410 this(null); | |
| 411 } | |
| 412 | |
| 413 protected Builder(BuilderParent builderParent) { | |
| 414 this.builderParent = builderParent; | |
| 415 } | |
| 416 | |
| 417 @Override | |
| 418 void dispose() { | |
| 419 builderParent = null; | |
| 420 } | |
| 421 | |
| 422 /** | |
| 423 * Called by the subclass when a message is built. | |
| 424 */ | |
| 425 protected void onBuilt() { | |
| 426 if (builderParent != null) { | |
| 427 markClean(); | |
| 428 } | |
| 429 } | |
| 430 | |
| 431 /** | |
| 432 * Called by the subclass or a builder to notify us that a message was | |
| 433 * built and may be cached and therefore invalidations are needed. | |
| 434 */ | |
| 435 @Override | |
| 436 protected void markClean() { | |
| 437 this.isClean = true; | |
| 438 } | |
| 439 | |
| 440 /** | |
| 441 * Gets whether invalidations are needed | |
| 442 * | |
| 443 * @return whether invalidations are needed | |
| 444 */ | |
| 445 protected boolean isClean() { | |
| 446 return isClean; | |
| 447 } | |
| 448 | |
| 449 @Override | |
| 450 public BuilderType clone() { | |
| 451 BuilderType builder = | |
| 452 (BuilderType) getDefaultInstanceForType().newBuilderForType(); | |
| 453 builder.mergeFrom(buildPartial()); | |
| 454 return builder; | |
| 455 } | |
| 456 | |
| 457 /** | |
| 458 * Called by the initialization and clear code paths to allow subclasses to | |
| 459 * reset any of their builtin fields back to the initial values. | |
| 460 */ | |
| 461 @Override | |
| 462 public BuilderType clear() { | |
| 463 unknownFields = UnknownFieldSet.getDefaultInstance(); | |
| 464 onChanged(); | |
| 465 return (BuilderType) this; | |
| 466 } | |
| 467 | |
| 468 /** | |
| 469 * Get the FieldAccessorTable for this type. We can't have the message | |
| 470 * class pass this in to the constructor because of bootstrapping trouble | |
| 471 * with DescriptorProtos. | |
| 472 */ | |
| 473 protected abstract FieldAccessorTable internalGetFieldAccessorTable(); | |
| 474 | |
| 475 @Override | |
| 476 public Descriptor getDescriptorForType() { | |
| 477 return internalGetFieldAccessorTable().descriptor; | |
| 478 } | |
| 479 | |
| 480 @Override | |
| 481 public Map<FieldDescriptor, Object> getAllFields() { | |
| 482 return Collections.unmodifiableMap(getAllFieldsMutable()); | |
| 483 } | |
| 484 | |
| 485 /** Internal helper which returns a mutable map. */ | |
| 486 private Map<FieldDescriptor, Object> getAllFieldsMutable() { | |
| 487 final TreeMap<FieldDescriptor, Object> result = | |
| 488 new TreeMap<FieldDescriptor, Object>(); | |
| 489 final Descriptor descriptor = internalGetFieldAccessorTable().descriptor; | |
| 490 final List<FieldDescriptor> fields = descriptor.getFields(); | |
| 491 | |
| 492 for (int i = 0; i < fields.size(); i++) { | |
| 493 FieldDescriptor field = fields.get(i); | |
| 494 final OneofDescriptor oneofDescriptor = field.getContainingOneof(); | |
| 495 | |
| 496 /* | |
| 497 * If the field is part of a Oneof, then at maximum one field in the One
of is set | |
| 498 * and it is not repeated. There is no need to iterate through the other
s. | |
| 499 */ | |
| 500 if (oneofDescriptor != null) { | |
| 501 // Skip other fields in the Oneof we know are not set | |
| 502 i += oneofDescriptor.getFieldCount() - 1; | |
| 503 if (!hasOneof(oneofDescriptor)) { | |
| 504 // If no field is set in the Oneof, skip all the fields in the Oneof | |
| 505 continue; | |
| 506 } | |
| 507 // Get the pointer to the only field which is set in the Oneof | |
| 508 field = getOneofFieldDescriptor(oneofDescriptor); | |
| 509 } else { | |
| 510 // If we are not in a Oneof, we need to check if the field is set and
if it is repeated | |
| 511 if (field.isRepeated()) { | |
| 512 final List<?> value = (List<?>) getField(field); | |
| 513 if (!value.isEmpty()) { | |
| 514 result.put(field, value); | |
| 515 } | |
| 516 continue; | |
| 517 } | |
| 518 if (!hasField(field)) { | |
| 519 continue; | |
| 520 } | |
| 521 } | |
| 522 // Add the field to the map | |
| 523 result.put(field, getField(field)); | |
| 524 } | |
| 525 return result; | |
| 526 } | |
| 527 | |
| 528 @Override | |
| 529 public Message.Builder newBuilderForField(final FieldDescriptor field) { | |
| 530 return internalGetFieldAccessorTable().getField(field).newBuilder(); | |
| 531 } | |
| 532 | |
| 533 @Override | |
| 534 public Message.Builder getFieldBuilder(final FieldDescriptor field) { | |
| 535 return internalGetFieldAccessorTable().getField(field).getBuilder(this); | |
| 536 } | |
| 537 | |
| 538 @Override | |
| 539 public Message.Builder getRepeatedFieldBuilder(final FieldDescriptor field,
int index) { | |
| 540 return internalGetFieldAccessorTable().getField(field).getRepeatedBuilder( | |
| 541 this, index); | |
| 542 } | |
| 543 | |
| 544 @Override | |
| 545 public boolean hasOneof(final OneofDescriptor oneof) { | |
| 546 return internalGetFieldAccessorTable().getOneof(oneof).has(this); | |
| 547 } | |
| 548 | |
| 549 @Override | |
| 550 public FieldDescriptor getOneofFieldDescriptor(final OneofDescriptor oneof)
{ | |
| 551 return internalGetFieldAccessorTable().getOneof(oneof).get(this); | |
| 552 } | |
| 553 | |
| 554 @Override | |
| 555 public boolean hasField(final FieldDescriptor field) { | |
| 556 return internalGetFieldAccessorTable().getField(field).has(this); | |
| 557 } | |
| 558 | |
| 559 @Override | |
| 560 public Object getField(final FieldDescriptor field) { | |
| 561 Object object = internalGetFieldAccessorTable().getField(field).get(this); | |
| 562 if (field.isRepeated()) { | |
| 563 // The underlying list object is still modifiable at this point. | |
| 564 // Make sure not to expose the modifiable list to the caller. | |
| 565 return Collections.unmodifiableList((List) object); | |
| 566 } else { | |
| 567 return object; | |
| 568 } | |
| 569 } | |
| 570 | |
| 571 @Override | |
| 572 public BuilderType setField(final FieldDescriptor field, final Object value)
{ | |
| 573 internalGetFieldAccessorTable().getField(field).set(this, value); | |
| 574 return (BuilderType) this; | |
| 575 } | |
| 576 | |
| 577 @Override | |
| 578 public BuilderType clearField(final FieldDescriptor field) { | |
| 579 internalGetFieldAccessorTable().getField(field).clear(this); | |
| 580 return (BuilderType) this; | |
| 581 } | |
| 582 | |
| 583 @Override | |
| 584 public BuilderType clearOneof(final OneofDescriptor oneof) { | |
| 585 internalGetFieldAccessorTable().getOneof(oneof).clear(this); | |
| 586 return (BuilderType) this; | |
| 587 } | |
| 588 | |
| 589 @Override | |
| 590 public int getRepeatedFieldCount(final FieldDescriptor field) { | |
| 591 return internalGetFieldAccessorTable().getField(field) | |
| 592 .getRepeatedCount(this); | |
| 593 } | |
| 594 | |
| 595 @Override | |
| 596 public Object getRepeatedField(final FieldDescriptor field, final int index)
{ | |
| 597 return internalGetFieldAccessorTable().getField(field) | |
| 598 .getRepeated(this, index); | |
| 599 } | |
| 600 | |
| 601 @Override | |
| 602 public BuilderType setRepeatedField( | |
| 603 final FieldDescriptor field, final int index, final Object value) { | |
| 604 internalGetFieldAccessorTable().getField(field) | |
| 605 .setRepeated(this, index, value); | |
| 606 return (BuilderType) this; | |
| 607 } | |
| 608 | |
| 609 @Override | |
| 610 public BuilderType addRepeatedField(final FieldDescriptor field, final Objec
t value) { | |
| 611 internalGetFieldAccessorTable().getField(field).addRepeated(this, value); | |
| 612 return (BuilderType) this; | |
| 613 } | |
| 614 | |
| 615 @Override | |
| 616 public BuilderType setUnknownFields(final UnknownFieldSet unknownFields) { | |
| 617 this.unknownFields = unknownFields; | |
| 618 onChanged(); | |
| 619 return (BuilderType) this; | |
| 620 } | |
| 621 | |
| 622 @Override | |
| 623 public BuilderType mergeUnknownFields( | |
| 624 final UnknownFieldSet unknownFields) { | |
| 625 this.unknownFields = | |
| 626 UnknownFieldSet.newBuilder(this.unknownFields) | |
| 627 .mergeFrom(unknownFields) | |
| 628 .build(); | |
| 629 onChanged(); | |
| 630 return (BuilderType) this; | |
| 631 } | |
| 632 | |
| 633 @Override | |
| 634 public boolean isInitialized() { | |
| 635 for (final FieldDescriptor field : getDescriptorForType().getFields()) { | |
| 636 // Check that all required fields are present. | |
| 637 if (field.isRequired()) { | |
| 638 if (!hasField(field)) { | |
| 639 return false; | |
| 640 } | |
| 641 } | |
| 642 // Check that embedded messages are initialized. | |
| 643 if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { | |
| 644 if (field.isRepeated()) { | |
| 645 @SuppressWarnings("unchecked") final | |
| 646 List<Message> messageList = (List<Message>) getField(field); | |
| 647 for (final Message element : messageList) { | |
| 648 if (!element.isInitialized()) { | |
| 649 return false; | |
| 650 } | |
| 651 } | |
| 652 } else { | |
| 653 if (hasField(field) && | |
| 654 !((Message) getField(field)).isInitialized()) { | |
| 655 return false; | |
| 656 } | |
| 657 } | |
| 658 } | |
| 659 } | |
| 660 return true; | |
| 661 } | |
| 662 | |
| 663 @Override | |
| 664 public final UnknownFieldSet getUnknownFields() { | |
| 665 return unknownFields; | |
| 666 } | |
| 667 | |
| 668 /** | |
| 669 * Called by subclasses to parse an unknown field. | |
| 670 * @return {@code true} unless the tag is an end-group tag. | |
| 671 */ | |
| 672 protected boolean parseUnknownField( | |
| 673 final CodedInputStream input, | |
| 674 final UnknownFieldSet.Builder unknownFields, | |
| 675 final ExtensionRegistryLite extensionRegistry, | |
| 676 final int tag) throws IOException { | |
| 677 return unknownFields.mergeFieldFrom(tag, input); | |
| 678 } | |
| 679 | |
| 680 /** | |
| 681 * Implementation of {@link BuilderParent} for giving to our children. This | |
| 682 * small inner class makes it so we don't publicly expose the BuilderParent | |
| 683 * methods. | |
| 684 */ | |
| 685 private class BuilderParentImpl implements BuilderParent { | |
| 686 | |
| 687 @Override | |
| 688 public void markDirty() { | |
| 689 onChanged(); | |
| 690 } | |
| 691 } | |
| 692 | |
| 693 /** | |
| 694 * Gets the {@link BuilderParent} for giving to our children. | |
| 695 * @return The builder parent for our children. | |
| 696 */ | |
| 697 protected BuilderParent getParentForChildren() { | |
| 698 if (meAsParent == null) { | |
| 699 meAsParent = new BuilderParentImpl(); | |
| 700 } | |
| 701 return meAsParent; | |
| 702 } | |
| 703 | |
| 704 /** | |
| 705 * Called when a the builder or one of its nested children has changed | |
| 706 * and any parent should be notified of its invalidation. | |
| 707 */ | |
| 708 protected final void onChanged() { | |
| 709 if (isClean && builderParent != null) { | |
| 710 builderParent.markDirty(); | |
| 711 | |
| 712 // Don't keep dispatching invalidations until build is called again. | |
| 713 isClean = false; | |
| 714 } | |
| 715 } | |
| 716 | |
| 717 /** | |
| 718 * Gets the map field with the given field number. This method should be | |
| 719 * overridden in the generated message class if the message contains map | |
| 720 * fields. | |
| 721 * | |
| 722 * Unlike other field types, reflection support for map fields can't be | |
| 723 * implemented based on generated public API because we need to access a | |
| 724 * map field as a list in reflection API but the generated API only allows | |
| 725 * us to access it as a map. This method returns the underlying map field | |
| 726 * directly and thus enables us to access the map field as a list. | |
| 727 */ | |
| 728 @SuppressWarnings({"unused", "rawtypes"}) | |
| 729 protected MapField internalGetMapField(int fieldNumber) { | |
| 730 // Note that we can't use descriptor names here because this method will | |
| 731 // be called when descriptor is being initialized. | |
| 732 throw new RuntimeException( | |
| 733 "No map fields found in " + getClass().getName()); | |
| 734 } | |
| 735 | |
| 736 /** Like {@link #internalGetMapField} but return a mutable version. */ | |
| 737 @SuppressWarnings({"unused", "rawtypes"}) | |
| 738 protected MapField internalGetMutableMapField(int fieldNumber) { | |
| 739 // Note that we can't use descriptor names here because this method will | |
| 740 // be called when descriptor is being initialized. | |
| 741 throw new RuntimeException( | |
| 742 "No map fields found in " + getClass().getName()); | |
| 743 } | |
| 744 } | |
| 745 | |
| 746 // ================================================================= | |
| 747 // Extensions-related stuff | |
| 748 | |
| 749 public interface ExtendableMessageOrBuilder< | |
| 750 MessageType extends ExtendableMessage> extends MessageOrBuilder { | |
| 751 // Re-define for return type covariance. | |
| 752 @Override | |
| 753 Message getDefaultInstanceForType(); | |
| 754 | |
| 755 /** Check if a singular extension is present. */ | |
| 756 <Type> boolean hasExtension( | |
| 757 ExtensionLite<MessageType, Type> extension); | |
| 758 | |
| 759 /** Get the number of elements in a repeated extension. */ | |
| 760 <Type> int getExtensionCount( | |
| 761 ExtensionLite<MessageType, List<Type>> extension); | |
| 762 | |
| 763 /** Get the value of an extension. */ | |
| 764 <Type> Type getExtension( | |
| 765 ExtensionLite<MessageType, Type> extension); | |
| 766 | |
| 767 /** Get one element of a repeated extension. */ | |
| 768 <Type> Type getExtension( | |
| 769 ExtensionLite<MessageType, List<Type>> extension, | |
| 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); | |
| 798 } | |
| 799 | |
| 800 /** | |
| 801 * Generated message classes for message types that contain extension ranges | |
| 802 * subclass this. | |
| 803 * | |
| 804 * <p>This class implements type-safe accessors for extensions. They | |
| 805 * implement all the same operations that you can do with normal fields -- | |
| 806 * e.g. "has", "get", and "getCount" -- but for extensions. The extensions | |
| 807 * are identified using instances of the class {@link GeneratedExtension}; | |
| 808 * the protocol compiler generates a static instance of this class for every | |
| 809 * extension in its input. Through the magic of generics, all is made | |
| 810 * type-safe. | |
| 811 * | |
| 812 * <p>For example, imagine you have the {@code .proto} file: | |
| 813 * | |
| 814 * <pre> | |
| 815 * option java_class = "MyProto"; | |
| 816 * | |
| 817 * message Foo { | |
| 818 * extensions 1000 to max; | |
| 819 * } | |
| 820 * | |
| 821 * extend Foo { | |
| 822 * optional int32 bar; | |
| 823 * } | |
| 824 * </pre> | |
| 825 * | |
| 826 * <p>Then you might write code like: | |
| 827 * | |
| 828 * <pre> | |
| 829 * MyProto.Foo foo = getFoo(); | |
| 830 * int i = foo.getExtension(MyProto.bar); | |
| 831 * </pre> | |
| 832 * | |
| 833 * <p>See also {@link ExtendableBuilder}. | |
| 834 */ | |
| 835 public abstract static class ExtendableMessage< | |
| 836 MessageType extends ExtendableMessage> | |
| 837 extends GeneratedMessageV3 | |
| 838 implements ExtendableMessageOrBuilder<MessageType> { | |
| 839 | |
| 840 private static final long serialVersionUID = 1L; | |
| 841 | |
| 842 private final FieldSet<FieldDescriptor> extensions; | |
| 843 | |
| 844 protected ExtendableMessage() { | |
| 845 this.extensions = FieldSet.newFieldSet(); | |
| 846 } | |
| 847 | |
| 848 protected ExtendableMessage( | |
| 849 ExtendableBuilder<MessageType, ?> builder) { | |
| 850 super(builder); | |
| 851 this.extensions = builder.buildExtensions(); | |
| 852 } | |
| 853 | |
| 854 private void verifyExtensionContainingType( | |
| 855 final Extension<MessageType, ?> extension) { | |
| 856 if (extension.getDescriptor().getContainingType() != | |
| 857 getDescriptorForType()) { | |
| 858 // This can only happen if someone uses unchecked operations. | |
| 859 throw new IllegalArgumentException( | |
| 860 "Extension is for type \"" + | |
| 861 extension.getDescriptor().getContainingType().getFullName() + | |
| 862 "\" which does not match message type \"" + | |
| 863 getDescriptorForType().getFullName() + "\"."); | |
| 864 } | |
| 865 } | |
| 866 | |
| 867 /** Check if a singular extension is present. */ | |
| 868 @Override | |
| 869 public final <Type> boolean hasExtension(final ExtensionLite<MessageType, Ty
pe> extensionLite) { | |
| 870 Extension<MessageType, Type> extension = checkNotLite(extensionLite); | |
| 871 | |
| 872 verifyExtensionContainingType(extension); | |
| 873 return extensions.hasField(extension.getDescriptor()); | |
| 874 } | |
| 875 | |
| 876 /** Get the number of elements in a repeated extension. */ | |
| 877 @Override | |
| 878 public final <Type> int getExtensionCount( | |
| 879 final ExtensionLite<MessageType, List<Type>> extensionLite) { | |
| 880 Extension<MessageType, List<Type>> extension = checkNotLite(extensionLite)
; | |
| 881 | |
| 882 verifyExtensionContainingType(extension); | |
| 883 final FieldDescriptor descriptor = extension.getDescriptor(); | |
| 884 return extensions.getRepeatedFieldCount(descriptor); | |
| 885 } | |
| 886 | |
| 887 /** Get the value of an extension. */ | |
| 888 @Override | |
| 889 @SuppressWarnings("unchecked") | |
| 890 public final <Type> Type getExtension(final ExtensionLite<MessageType, Type>
extensionLite) { | |
| 891 Extension<MessageType, Type> extension = checkNotLite(extensionLite); | |
| 892 | |
| 893 verifyExtensionContainingType(extension); | |
| 894 FieldDescriptor descriptor = extension.getDescriptor(); | |
| 895 final Object value = extensions.getField(descriptor); | |
| 896 if (value == null) { | |
| 897 if (descriptor.isRepeated()) { | |
| 898 return (Type) Collections.emptyList(); | |
| 899 } else if (descriptor.getJavaType() == | |
| 900 FieldDescriptor.JavaType.MESSAGE) { | |
| 901 return (Type) extension.getMessageDefaultInstance(); | |
| 902 } else { | |
| 903 return (Type) extension.fromReflectionType( | |
| 904 descriptor.getDefaultValue()); | |
| 905 } | |
| 906 } else { | |
| 907 return (Type) extension.fromReflectionType(value); | |
| 908 } | |
| 909 } | |
| 910 | |
| 911 /** Get one element of a repeated extension. */ | |
| 912 @Override | |
| 913 @SuppressWarnings("unchecked") | |
| 914 public final <Type> Type getExtension( | |
| 915 final ExtensionLite<MessageType, List<Type>> extensionLite, final int in
dex) { | |
| 916 Extension<MessageType, List<Type>> extension = checkNotLite(extensionLite)
; | |
| 917 | |
| 918 verifyExtensionContainingType(extension); | |
| 919 FieldDescriptor descriptor = extension.getDescriptor(); | |
| 920 return (Type) extension.singularFromReflectionType( | |
| 921 extensions.getRepeatedField(descriptor, index)); | |
| 922 } | |
| 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 | |
| 971 /** Called by subclasses to check if all extensions are initialized. */ | |
| 972 protected boolean extensionsAreInitialized() { | |
| 973 return extensions.isInitialized(); | |
| 974 } | |
| 975 | |
| 976 @Override | |
| 977 public boolean isInitialized() { | |
| 978 return super.isInitialized() && extensionsAreInitialized(); | |
| 979 } | |
| 980 | |
| 981 @Override | |
| 982 protected boolean parseUnknownField( | |
| 983 CodedInputStream input, | |
| 984 UnknownFieldSet.Builder unknownFields, | |
| 985 ExtensionRegistryLite extensionRegistry, | |
| 986 int tag) throws IOException { | |
| 987 return MessageReflection.mergeFieldFrom( | |
| 988 input, unknownFields, extensionRegistry, getDescriptorForType(), | |
| 989 new MessageReflection.ExtensionAdapter(extensions), tag); | |
| 990 } | |
| 991 | |
| 992 | |
| 993 /** | |
| 994 * Used by parsing constructors in generated classes. | |
| 995 */ | |
| 996 @Override | |
| 997 protected void makeExtensionsImmutable() { | |
| 998 extensions.makeImmutable(); | |
| 999 } | |
| 1000 | |
| 1001 /** | |
| 1002 * Used by subclasses to serialize extensions. Extension ranges may be | |
| 1003 * interleaved with field numbers, but we must write them in canonical | |
| 1004 * (sorted by field number) order. ExtensionWriter helps us write | |
| 1005 * individual ranges of extensions at once. | |
| 1006 */ | |
| 1007 protected class ExtensionWriter { | |
| 1008 // Imagine how much simpler this code would be if Java iterators had | |
| 1009 // a way to get the next element without advancing the iterator. | |
| 1010 | |
| 1011 private final Iterator<Map.Entry<FieldDescriptor, Object>> iter = | |
| 1012 extensions.iterator(); | |
| 1013 private Map.Entry<FieldDescriptor, Object> next; | |
| 1014 private final boolean messageSetWireFormat; | |
| 1015 | |
| 1016 private ExtensionWriter(final boolean messageSetWireFormat) { | |
| 1017 if (iter.hasNext()) { | |
| 1018 next = iter.next(); | |
| 1019 } | |
| 1020 this.messageSetWireFormat = messageSetWireFormat; | |
| 1021 } | |
| 1022 | |
| 1023 public void writeUntil(final int end, final CodedOutputStream output) | |
| 1024 throws IOException { | |
| 1025 while (next != null && next.getKey().getNumber() < end) { | |
| 1026 FieldDescriptor descriptor = next.getKey(); | |
| 1027 if (messageSetWireFormat && descriptor.getLiteJavaType() == | |
| 1028 WireFormat.JavaType.MESSAGE && | |
| 1029 !descriptor.isRepeated()) { | |
| 1030 if (next instanceof LazyField.LazyEntry<?>) { | |
| 1031 output.writeRawMessageSetExtension(descriptor.getNumber(), | |
| 1032 ((LazyField.LazyEntry<?>) next).getField().toByteString()); | |
| 1033 } else { | |
| 1034 output.writeMessageSetExtension(descriptor.getNumber(), | |
| 1035 (Message) next.getValue()); | |
| 1036 } | |
| 1037 } else { | |
| 1038 // TODO(xiangl): Taken care of following code, it may cause | |
| 1039 // problem when we use LazyField for normal fields/extensions. | |
| 1040 // Due to the optional field can be duplicated at the end of | |
| 1041 // serialized bytes, which will make the serialized size change | |
| 1042 // after lazy field parsed. So when we use LazyField globally, | |
| 1043 // we need to change the following write method to write cached | |
| 1044 // bytes directly rather than write the parsed message. | |
| 1045 FieldSet.writeField(descriptor, next.getValue(), output); | |
| 1046 } | |
| 1047 if (iter.hasNext()) { | |
| 1048 next = iter.next(); | |
| 1049 } else { | |
| 1050 next = null; | |
| 1051 } | |
| 1052 } | |
| 1053 } | |
| 1054 } | |
| 1055 | |
| 1056 protected ExtensionWriter newExtensionWriter() { | |
| 1057 return new ExtensionWriter(false); | |
| 1058 } | |
| 1059 protected ExtensionWriter newMessageSetExtensionWriter() { | |
| 1060 return new ExtensionWriter(true); | |
| 1061 } | |
| 1062 | |
| 1063 /** Called by subclasses to compute the size of extensions. */ | |
| 1064 protected int extensionsSerializedSize() { | |
| 1065 return extensions.getSerializedSize(); | |
| 1066 } | |
| 1067 protected int extensionsSerializedSizeAsMessageSet() { | |
| 1068 return extensions.getMessageSetSerializedSize(); | |
| 1069 } | |
| 1070 | |
| 1071 // --------------------------------------------------------------- | |
| 1072 // Reflection | |
| 1073 | |
| 1074 protected Map<FieldDescriptor, Object> getExtensionFields() { | |
| 1075 return extensions.getAllFields(); | |
| 1076 } | |
| 1077 | |
| 1078 @Override | |
| 1079 public Map<FieldDescriptor, Object> getAllFields() { | |
| 1080 final Map<FieldDescriptor, Object> result = | |
| 1081 super.getAllFieldsMutable(/* getBytesForString = */ false); | |
| 1082 result.putAll(getExtensionFields()); | |
| 1083 return Collections.unmodifiableMap(result); | |
| 1084 } | |
| 1085 | |
| 1086 @Override | |
| 1087 public Map<FieldDescriptor, Object> getAllFieldsRaw() { | |
| 1088 final Map<FieldDescriptor, Object> result = | |
| 1089 super.getAllFieldsMutable(/* getBytesForString = */ false); | |
| 1090 result.putAll(getExtensionFields()); | |
| 1091 return Collections.unmodifiableMap(result); | |
| 1092 } | |
| 1093 | |
| 1094 @Override | |
| 1095 public boolean hasField(final FieldDescriptor field) { | |
| 1096 if (field.isExtension()) { | |
| 1097 verifyContainingType(field); | |
| 1098 return extensions.hasField(field); | |
| 1099 } else { | |
| 1100 return super.hasField(field); | |
| 1101 } | |
| 1102 } | |
| 1103 | |
| 1104 @Override | |
| 1105 public Object getField(final FieldDescriptor field) { | |
| 1106 if (field.isExtension()) { | |
| 1107 verifyContainingType(field); | |
| 1108 final Object value = extensions.getField(field); | |
| 1109 if (value == null) { | |
| 1110 if (field.isRepeated()) { | |
| 1111 return Collections.emptyList(); | |
| 1112 } else if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { | |
| 1113 // Lacking an ExtensionRegistry, we have no way to determine the | |
| 1114 // extension's real type, so we return a DynamicMessage. | |
| 1115 return DynamicMessage.getDefaultInstance(field.getMessageType()); | |
| 1116 } else { | |
| 1117 return field.getDefaultValue(); | |
| 1118 } | |
| 1119 } else { | |
| 1120 return value; | |
| 1121 } | |
| 1122 } else { | |
| 1123 return super.getField(field); | |
| 1124 } | |
| 1125 } | |
| 1126 | |
| 1127 @Override | |
| 1128 public int getRepeatedFieldCount(final FieldDescriptor field) { | |
| 1129 if (field.isExtension()) { | |
| 1130 verifyContainingType(field); | |
| 1131 return extensions.getRepeatedFieldCount(field); | |
| 1132 } else { | |
| 1133 return super.getRepeatedFieldCount(field); | |
| 1134 } | |
| 1135 } | |
| 1136 | |
| 1137 @Override | |
| 1138 public Object getRepeatedField(final FieldDescriptor field, | |
| 1139 final int index) { | |
| 1140 if (field.isExtension()) { | |
| 1141 verifyContainingType(field); | |
| 1142 return extensions.getRepeatedField(field, index); | |
| 1143 } else { | |
| 1144 return super.getRepeatedField(field, index); | |
| 1145 } | |
| 1146 } | |
| 1147 | |
| 1148 private void verifyContainingType(final FieldDescriptor field) { | |
| 1149 if (field.getContainingType() != getDescriptorForType()) { | |
| 1150 throw new IllegalArgumentException( | |
| 1151 "FieldDescriptor does not match message type."); | |
| 1152 } | |
| 1153 } | |
| 1154 } | |
| 1155 | |
| 1156 /** | |
| 1157 * Generated message builders for message types that contain extension ranges | |
| 1158 * subclass this. | |
| 1159 * | |
| 1160 * <p>This class implements type-safe accessors for extensions. They | |
| 1161 * implement all the same operations that you can do with normal fields -- | |
| 1162 * e.g. "get", "set", and "add" -- but for extensions. The extensions are | |
| 1163 * identified using instances of the class {@link GeneratedExtension}; the | |
| 1164 * protocol compiler generates a static instance of this class for every | |
| 1165 * extension in its input. Through the magic of generics, all is made | |
| 1166 * type-safe. | |
| 1167 * | |
| 1168 * <p>For example, imagine you have the {@code .proto} file: | |
| 1169 * | |
| 1170 * <pre> | |
| 1171 * option java_class = "MyProto"; | |
| 1172 * | |
| 1173 * message Foo { | |
| 1174 * extensions 1000 to max; | |
| 1175 * } | |
| 1176 * | |
| 1177 * extend Foo { | |
| 1178 * optional int32 bar; | |
| 1179 * } | |
| 1180 * </pre> | |
| 1181 * | |
| 1182 * <p>Then you might write code like: | |
| 1183 * | |
| 1184 * <pre> | |
| 1185 * MyProto.Foo foo = | |
| 1186 * MyProto.Foo.newBuilder() | |
| 1187 * .setExtension(MyProto.bar, 123) | |
| 1188 * .build(); | |
| 1189 * </pre> | |
| 1190 * | |
| 1191 * <p>See also {@link ExtendableMessage}. | |
| 1192 */ | |
| 1193 @SuppressWarnings("unchecked") | |
| 1194 public abstract static class ExtendableBuilder< | |
| 1195 MessageType extends ExtendableMessage, | |
| 1196 BuilderType extends ExtendableBuilder<MessageType, BuilderType>> | |
| 1197 extends Builder<BuilderType> | |
| 1198 implements ExtendableMessageOrBuilder<MessageType> { | |
| 1199 | |
| 1200 private FieldSet<FieldDescriptor> extensions = FieldSet.emptySet(); | |
| 1201 | |
| 1202 protected ExtendableBuilder() {} | |
| 1203 | |
| 1204 protected ExtendableBuilder( | |
| 1205 BuilderParent parent) { | |
| 1206 super(parent); | |
| 1207 } | |
| 1208 | |
| 1209 // For immutable message conversion. | |
| 1210 void internalSetExtensionSet(FieldSet<FieldDescriptor> extensions) { | |
| 1211 this.extensions = extensions; | |
| 1212 } | |
| 1213 | |
| 1214 @Override | |
| 1215 public BuilderType clear() { | |
| 1216 extensions = FieldSet.emptySet(); | |
| 1217 return super.clear(); | |
| 1218 } | |
| 1219 | |
| 1220 private void ensureExtensionsIsMutable() { | |
| 1221 if (extensions.isImmutable()) { | |
| 1222 extensions = extensions.clone(); | |
| 1223 } | |
| 1224 } | |
| 1225 | |
| 1226 private void verifyExtensionContainingType( | |
| 1227 final Extension<MessageType, ?> extension) { | |
| 1228 if (extension.getDescriptor().getContainingType() != | |
| 1229 getDescriptorForType()) { | |
| 1230 // This can only happen if someone uses unchecked operations. | |
| 1231 throw new IllegalArgumentException( | |
| 1232 "Extension is for type \"" + | |
| 1233 extension.getDescriptor().getContainingType().getFullName() + | |
| 1234 "\" which does not match message type \"" + | |
| 1235 getDescriptorForType().getFullName() + "\"."); | |
| 1236 } | |
| 1237 } | |
| 1238 | |
| 1239 /** Check if a singular extension is present. */ | |
| 1240 @Override | |
| 1241 public final <Type> boolean hasExtension(final ExtensionLite<MessageType, Ty
pe> extensionLite) { | |
| 1242 Extension<MessageType, Type> extension = checkNotLite(extensionLite); | |
| 1243 | |
| 1244 verifyExtensionContainingType(extension); | |
| 1245 return extensions.hasField(extension.getDescriptor()); | |
| 1246 } | |
| 1247 | |
| 1248 /** Get the number of elements in a repeated extension. */ | |
| 1249 @Override | |
| 1250 public final <Type> int getExtensionCount( | |
| 1251 final ExtensionLite<MessageType, List<Type>> extensionLite) { | |
| 1252 Extension<MessageType, List<Type>> extension = checkNotLite(extensionLite)
; | |
| 1253 | |
| 1254 verifyExtensionContainingType(extension); | |
| 1255 final FieldDescriptor descriptor = extension.getDescriptor(); | |
| 1256 return extensions.getRepeatedFieldCount(descriptor); | |
| 1257 } | |
| 1258 | |
| 1259 /** Get the value of an extension. */ | |
| 1260 @Override | |
| 1261 public final <Type> Type getExtension(final ExtensionLite<MessageType, Type>
extensionLite) { | |
| 1262 Extension<MessageType, Type> extension = checkNotLite(extensionLite); | |
| 1263 | |
| 1264 verifyExtensionContainingType(extension); | |
| 1265 FieldDescriptor descriptor = extension.getDescriptor(); | |
| 1266 final Object value = extensions.getField(descriptor); | |
| 1267 if (value == null) { | |
| 1268 if (descriptor.isRepeated()) { | |
| 1269 return (Type) Collections.emptyList(); | |
| 1270 } else if (descriptor.getJavaType() == | |
| 1271 FieldDescriptor.JavaType.MESSAGE) { | |
| 1272 return (Type) extension.getMessageDefaultInstance(); | |
| 1273 } else { | |
| 1274 return (Type) extension.fromReflectionType( | |
| 1275 descriptor.getDefaultValue()); | |
| 1276 } | |
| 1277 } else { | |
| 1278 return (Type) extension.fromReflectionType(value); | |
| 1279 } | |
| 1280 } | |
| 1281 | |
| 1282 /** Get one element of a repeated extension. */ | |
| 1283 @Override | |
| 1284 public final <Type> Type getExtension( | |
| 1285 final ExtensionLite<MessageType, List<Type>> extensionLite, final int in
dex) { | |
| 1286 Extension<MessageType, List<Type>> extension = checkNotLite(extensionLite)
; | |
| 1287 | |
| 1288 verifyExtensionContainingType(extension); | |
| 1289 FieldDescriptor descriptor = extension.getDescriptor(); | |
| 1290 return (Type) extension.singularFromReflectionType( | |
| 1291 extensions.getRepeatedField(descriptor, index)); | |
| 1292 } | |
| 1293 | |
| 1294 /** Set the value of an extension. */ | |
| 1295 public final <Type> BuilderType setExtension( | |
| 1296 final ExtensionLite<MessageType, Type> extensionLite, | |
| 1297 final Type value) { | |
| 1298 Extension<MessageType, Type> extension = checkNotLite(extensionLite); | |
| 1299 | |
| 1300 verifyExtensionContainingType(extension); | |
| 1301 ensureExtensionsIsMutable(); | |
| 1302 final FieldDescriptor descriptor = extension.getDescriptor(); | |
| 1303 extensions.setField(descriptor, extension.toReflectionType(value)); | |
| 1304 onChanged(); | |
| 1305 return (BuilderType) this; | |
| 1306 } | |
| 1307 | |
| 1308 /** Set the value of one element of a repeated extension. */ | |
| 1309 public final <Type> BuilderType setExtension( | |
| 1310 final ExtensionLite<MessageType, List<Type>> extensionLite, | |
| 1311 final int index, final Type value) { | |
| 1312 Extension<MessageType, List<Type>> extension = checkNotLite(extensionLite)
; | |
| 1313 | |
| 1314 verifyExtensionContainingType(extension); | |
| 1315 ensureExtensionsIsMutable(); | |
| 1316 final FieldDescriptor descriptor = extension.getDescriptor(); | |
| 1317 extensions.setRepeatedField( | |
| 1318 descriptor, index, | |
| 1319 extension.singularToReflectionType(value)); | |
| 1320 onChanged(); | |
| 1321 return (BuilderType) this; | |
| 1322 } | |
| 1323 | |
| 1324 /** Append a value to a repeated extension. */ | |
| 1325 public final <Type> BuilderType addExtension( | |
| 1326 final ExtensionLite<MessageType, List<Type>> extensionLite, | |
| 1327 final Type value) { | |
| 1328 Extension<MessageType, List<Type>> extension = checkNotLite(extensionLite)
; | |
| 1329 | |
| 1330 verifyExtensionContainingType(extension); | |
| 1331 ensureExtensionsIsMutable(); | |
| 1332 final FieldDescriptor descriptor = extension.getDescriptor(); | |
| 1333 extensions.addRepeatedField( | |
| 1334 descriptor, extension.singularToReflectionType(value)); | |
| 1335 onChanged(); | |
| 1336 return (BuilderType) this; | |
| 1337 } | |
| 1338 | |
| 1339 /** Clear an extension. */ | |
| 1340 public final <Type> BuilderType clearExtension( | |
| 1341 final ExtensionLite<MessageType, ?> extensionLite) { | |
| 1342 Extension<MessageType, ?> extension = checkNotLite(extensionLite); | |
| 1343 | |
| 1344 verifyExtensionContainingType(extension); | |
| 1345 ensureExtensionsIsMutable(); | |
| 1346 extensions.clearField(extension.getDescriptor()); | |
| 1347 onChanged(); | |
| 1348 return (BuilderType) this; | |
| 1349 } | |
| 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 | |
| 1440 /** Called by subclasses to check if all extensions are initialized. */ | |
| 1441 protected boolean extensionsAreInitialized() { | |
| 1442 return extensions.isInitialized(); | |
| 1443 } | |
| 1444 | |
| 1445 /** | |
| 1446 * Called by the build code path to create a copy of the extensions for | |
| 1447 * building the message. | |
| 1448 */ | |
| 1449 private FieldSet<FieldDescriptor> buildExtensions() { | |
| 1450 extensions.makeImmutable(); | |
| 1451 return extensions; | |
| 1452 } | |
| 1453 | |
| 1454 @Override | |
| 1455 public boolean isInitialized() { | |
| 1456 return super.isInitialized() && extensionsAreInitialized(); | |
| 1457 } | |
| 1458 | |
| 1459 /** | |
| 1460 * Called by subclasses to parse an unknown field or an extension. | |
| 1461 * @return {@code true} unless the tag is an end-group tag. | |
| 1462 */ | |
| 1463 @Override | |
| 1464 protected boolean parseUnknownField( | |
| 1465 final CodedInputStream input, | |
| 1466 final UnknownFieldSet.Builder unknownFields, | |
| 1467 final ExtensionRegistryLite extensionRegistry, | |
| 1468 final int tag) throws IOException { | |
| 1469 return MessageReflection.mergeFieldFrom( | |
| 1470 input, unknownFields, extensionRegistry, getDescriptorForType(), | |
| 1471 new MessageReflection.BuilderAdapter(this), tag); | |
| 1472 } | |
| 1473 | |
| 1474 // --------------------------------------------------------------- | |
| 1475 // Reflection | |
| 1476 | |
| 1477 @Override | |
| 1478 public Map<FieldDescriptor, Object> getAllFields() { | |
| 1479 final Map<FieldDescriptor, Object> result = super.getAllFieldsMutable(); | |
| 1480 result.putAll(extensions.getAllFields()); | |
| 1481 return Collections.unmodifiableMap(result); | |
| 1482 } | |
| 1483 | |
| 1484 @Override | |
| 1485 public Object getField(final FieldDescriptor field) { | |
| 1486 if (field.isExtension()) { | |
| 1487 verifyContainingType(field); | |
| 1488 final Object value = extensions.getField(field); | |
| 1489 if (value == null) { | |
| 1490 if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { | |
| 1491 // Lacking an ExtensionRegistry, we have no way to determine the | |
| 1492 // extension's real type, so we return a DynamicMessage. | |
| 1493 return DynamicMessage.getDefaultInstance(field.getMessageType()); | |
| 1494 } else { | |
| 1495 return field.getDefaultValue(); | |
| 1496 } | |
| 1497 } else { | |
| 1498 return value; | |
| 1499 } | |
| 1500 } else { | |
| 1501 return super.getField(field); | |
| 1502 } | |
| 1503 } | |
| 1504 | |
| 1505 @Override | |
| 1506 public int getRepeatedFieldCount(final FieldDescriptor field) { | |
| 1507 if (field.isExtension()) { | |
| 1508 verifyContainingType(field); | |
| 1509 return extensions.getRepeatedFieldCount(field); | |
| 1510 } else { | |
| 1511 return super.getRepeatedFieldCount(field); | |
| 1512 } | |
| 1513 } | |
| 1514 | |
| 1515 @Override | |
| 1516 public Object getRepeatedField(final FieldDescriptor field, | |
| 1517 final int index) { | |
| 1518 if (field.isExtension()) { | |
| 1519 verifyContainingType(field); | |
| 1520 return extensions.getRepeatedField(field, index); | |
| 1521 } else { | |
| 1522 return super.getRepeatedField(field, index); | |
| 1523 } | |
| 1524 } | |
| 1525 | |
| 1526 @Override | |
| 1527 public boolean hasField(final FieldDescriptor field) { | |
| 1528 if (field.isExtension()) { | |
| 1529 verifyContainingType(field); | |
| 1530 return extensions.hasField(field); | |
| 1531 } else { | |
| 1532 return super.hasField(field); | |
| 1533 } | |
| 1534 } | |
| 1535 | |
| 1536 @Override | |
| 1537 public BuilderType setField(final FieldDescriptor field, | |
| 1538 final Object value) { | |
| 1539 if (field.isExtension()) { | |
| 1540 verifyContainingType(field); | |
| 1541 ensureExtensionsIsMutable(); | |
| 1542 extensions.setField(field, value); | |
| 1543 onChanged(); | |
| 1544 return (BuilderType) this; | |
| 1545 } else { | |
| 1546 return super.setField(field, value); | |
| 1547 } | |
| 1548 } | |
| 1549 | |
| 1550 @Override | |
| 1551 public BuilderType clearField(final FieldDescriptor field) { | |
| 1552 if (field.isExtension()) { | |
| 1553 verifyContainingType(field); | |
| 1554 ensureExtensionsIsMutable(); | |
| 1555 extensions.clearField(field); | |
| 1556 onChanged(); | |
| 1557 return (BuilderType) this; | |
| 1558 } else { | |
| 1559 return super.clearField(field); | |
| 1560 } | |
| 1561 } | |
| 1562 | |
| 1563 @Override | |
| 1564 public BuilderType setRepeatedField(final FieldDescriptor field, | |
| 1565 final int index, final Object value) { | |
| 1566 if (field.isExtension()) { | |
| 1567 verifyContainingType(field); | |
| 1568 ensureExtensionsIsMutable(); | |
| 1569 extensions.setRepeatedField(field, index, value); | |
| 1570 onChanged(); | |
| 1571 return (BuilderType) this; | |
| 1572 } else { | |
| 1573 return super.setRepeatedField(field, index, value); | |
| 1574 } | |
| 1575 } | |
| 1576 | |
| 1577 @Override | |
| 1578 public BuilderType addRepeatedField(final FieldDescriptor field, | |
| 1579 final Object value) { | |
| 1580 if (field.isExtension()) { | |
| 1581 verifyContainingType(field); | |
| 1582 ensureExtensionsIsMutable(); | |
| 1583 extensions.addRepeatedField(field, value); | |
| 1584 onChanged(); | |
| 1585 return (BuilderType) this; | |
| 1586 } else { | |
| 1587 return super.addRepeatedField(field, value); | |
| 1588 } | |
| 1589 } | |
| 1590 | |
| 1591 protected final void mergeExtensionFields(final ExtendableMessage other) { | |
| 1592 ensureExtensionsIsMutable(); | |
| 1593 extensions.mergeFrom(other.extensions); | |
| 1594 onChanged(); | |
| 1595 } | |
| 1596 | |
| 1597 private void verifyContainingType(final FieldDescriptor field) { | |
| 1598 if (field.getContainingType() != getDescriptorForType()) { | |
| 1599 throw new IllegalArgumentException( | |
| 1600 "FieldDescriptor does not match message type."); | |
| 1601 } | |
| 1602 } | |
| 1603 } | |
| 1604 | |
| 1605 // ----------------------------------------------------------------- | |
| 1606 | |
| 1607 /** | |
| 1608 * Gets the descriptor for an extension. The implementation depends on whether | |
| 1609 * the extension is scoped in the top level of a file or scoped in a Message. | |
| 1610 */ | |
| 1611 static interface ExtensionDescriptorRetriever { | |
| 1612 FieldDescriptor getDescriptor(); | |
| 1613 } | |
| 1614 | |
| 1615 | |
| 1616 // ================================================================= | |
| 1617 | |
| 1618 /** Calls Class.getMethod and throws a RuntimeException if it fails. */ | |
| 1619 @SuppressWarnings("unchecked") | |
| 1620 private static Method getMethodOrDie( | |
| 1621 final Class clazz, final String name, final Class... params) { | |
| 1622 try { | |
| 1623 return clazz.getMethod(name, params); | |
| 1624 } catch (NoSuchMethodException e) { | |
| 1625 throw new RuntimeException( | |
| 1626 "Generated message class \"" + clazz.getName() + | |
| 1627 "\" missing method \"" + name + "\".", e); | |
| 1628 } | |
| 1629 } | |
| 1630 | |
| 1631 /** Calls invoke and throws a RuntimeException if it fails. */ | |
| 1632 private static Object invokeOrDie( | |
| 1633 final Method method, final Object object, final Object... params) { | |
| 1634 try { | |
| 1635 return method.invoke(object, params); | |
| 1636 } catch (IllegalAccessException e) { | |
| 1637 throw new RuntimeException( | |
| 1638 "Couldn't use Java reflection to implement protocol message " + | |
| 1639 "reflection.", e); | |
| 1640 } catch (InvocationTargetException e) { | |
| 1641 final Throwable cause = e.getCause(); | |
| 1642 if (cause instanceof RuntimeException) { | |
| 1643 throw (RuntimeException) cause; | |
| 1644 } else if (cause instanceof Error) { | |
| 1645 throw (Error) cause; | |
| 1646 } else { | |
| 1647 throw new RuntimeException( | |
| 1648 "Unexpected exception thrown by generated accessor method.", cause); | |
| 1649 } | |
| 1650 } | |
| 1651 } | |
| 1652 | |
| 1653 /** | |
| 1654 * Gets the map field with the given field number. This method should be | |
| 1655 * overridden in the generated message class if the message contains map | |
| 1656 * fields. | |
| 1657 * | |
| 1658 * Unlike other field types, reflection support for map fields can't be | |
| 1659 * implemented based on generated public API because we need to access a | |
| 1660 * map field as a list in reflection API but the generated API only allows | |
| 1661 * us to access it as a map. This method returns the underlying map field | |
| 1662 * directly and thus enables us to access the map field as a list. | |
| 1663 */ | |
| 1664 @SuppressWarnings({"rawtypes", "unused"}) | |
| 1665 protected MapField internalGetMapField(int fieldNumber) { | |
| 1666 // Note that we can't use descriptor names here because this method will | |
| 1667 // be called when descriptor is being initialized. | |
| 1668 throw new RuntimeException( | |
| 1669 "No map fields found in " + getClass().getName()); | |
| 1670 } | |
| 1671 | |
| 1672 /** | |
| 1673 * Users should ignore this class. This class provides the implementation | |
| 1674 * with access to the fields of a message object using Java reflection. | |
| 1675 */ | |
| 1676 public static final class FieldAccessorTable { | |
| 1677 | |
| 1678 /** | |
| 1679 * Construct a FieldAccessorTable for a particular message class. Only | |
| 1680 * one FieldAccessorTable should ever be constructed per class. | |
| 1681 * | |
| 1682 * @param descriptor The type's descriptor. | |
| 1683 * @param camelCaseNames The camelcase names of all fields in the message. | |
| 1684 * These are used to derive the accessor method names. | |
| 1685 * @param messageClass The message type. | |
| 1686 * @param builderClass The builder type. | |
| 1687 */ | |
| 1688 public FieldAccessorTable( | |
| 1689 final Descriptor descriptor, | |
| 1690 final String[] camelCaseNames, | |
| 1691 final Class<? extends GeneratedMessageV3> messageClass, | |
| 1692 final Class<? extends Builder> builderClass) { | |
| 1693 this(descriptor, camelCaseNames); | |
| 1694 ensureFieldAccessorsInitialized(messageClass, builderClass); | |
| 1695 } | |
| 1696 | |
| 1697 /** | |
| 1698 * Construct a FieldAccessorTable for a particular message class without | |
| 1699 * initializing FieldAccessors. | |
| 1700 */ | |
| 1701 public FieldAccessorTable( | |
| 1702 final Descriptor descriptor, | |
| 1703 final String[] camelCaseNames) { | |
| 1704 this.descriptor = descriptor; | |
| 1705 this.camelCaseNames = camelCaseNames; | |
| 1706 fields = new FieldAccessor[descriptor.getFields().size()]; | |
| 1707 oneofs = new OneofAccessor[descriptor.getOneofs().size()]; | |
| 1708 initialized = false; | |
| 1709 } | |
| 1710 | |
| 1711 /** | |
| 1712 * Ensures the field accessors are initialized. This method is thread-safe. | |
| 1713 * | |
| 1714 * @param messageClass The message type. | |
| 1715 * @param builderClass The builder type. | |
| 1716 * @return this | |
| 1717 */ | |
| 1718 public FieldAccessorTable ensureFieldAccessorsInitialized( | |
| 1719 Class<? extends GeneratedMessageV3> messageClass, | |
| 1720 Class<? extends Builder> builderClass) { | |
| 1721 if (initialized) { return this; } | |
| 1722 synchronized (this) { | |
| 1723 if (initialized) { return this; } | |
| 1724 int fieldsSize = fields.length; | |
| 1725 for (int i = 0; i < fieldsSize; i++) { | |
| 1726 FieldDescriptor field = descriptor.getFields().get(i); | |
| 1727 String containingOneofCamelCaseName = null; | |
| 1728 if (field.getContainingOneof() != null) { | |
| 1729 containingOneofCamelCaseName = | |
| 1730 camelCaseNames[fieldsSize + field.getContainingOneof().getIndex(
)]; | |
| 1731 } | |
| 1732 if (field.isRepeated()) { | |
| 1733 if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { | |
| 1734 if (field.isMapField()) { | |
| 1735 fields[i] = new MapFieldAccessor( | |
| 1736 field, camelCaseNames[i], messageClass, builderClass); | |
| 1737 } else { | |
| 1738 fields[i] = new RepeatedMessageFieldAccessor( | |
| 1739 field, camelCaseNames[i], messageClass, builderClass); | |
| 1740 } | |
| 1741 } else if (field.getJavaType() == FieldDescriptor.JavaType.ENUM) { | |
| 1742 fields[i] = new RepeatedEnumFieldAccessor( | |
| 1743 field, camelCaseNames[i], messageClass, builderClass); | |
| 1744 } else { | |
| 1745 fields[i] = new RepeatedFieldAccessor( | |
| 1746 field, camelCaseNames[i], messageClass, builderClass); | |
| 1747 } | |
| 1748 } else { | |
| 1749 if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { | |
| 1750 fields[i] = new SingularMessageFieldAccessor( | |
| 1751 field, camelCaseNames[i], messageClass, builderClass, | |
| 1752 containingOneofCamelCaseName); | |
| 1753 } else if (field.getJavaType() == FieldDescriptor.JavaType.ENUM) { | |
| 1754 fields[i] = new SingularEnumFieldAccessor( | |
| 1755 field, camelCaseNames[i], messageClass, builderClass, | |
| 1756 containingOneofCamelCaseName); | |
| 1757 } else if (field.getJavaType() == FieldDescriptor.JavaType.STRING) { | |
| 1758 fields[i] = new SingularStringFieldAccessor( | |
| 1759 field, camelCaseNames[i], messageClass, builderClass, | |
| 1760 containingOneofCamelCaseName); | |
| 1761 } else { | |
| 1762 fields[i] = new SingularFieldAccessor( | |
| 1763 field, camelCaseNames[i], messageClass, builderClass, | |
| 1764 containingOneofCamelCaseName); | |
| 1765 } | |
| 1766 } | |
| 1767 } | |
| 1768 | |
| 1769 int oneofsSize = oneofs.length; | |
| 1770 for (int i = 0; i < oneofsSize; i++) { | |
| 1771 oneofs[i] = new OneofAccessor( | |
| 1772 descriptor, camelCaseNames[i + fieldsSize], | |
| 1773 messageClass, builderClass); | |
| 1774 } | |
| 1775 initialized = true; | |
| 1776 camelCaseNames = null; | |
| 1777 return this; | |
| 1778 } | |
| 1779 } | |
| 1780 | |
| 1781 private final Descriptor descriptor; | |
| 1782 private final FieldAccessor[] fields; | |
| 1783 private String[] camelCaseNames; | |
| 1784 private final OneofAccessor[] oneofs; | |
| 1785 private volatile boolean initialized; | |
| 1786 | |
| 1787 /** Get the FieldAccessor for a particular field. */ | |
| 1788 private FieldAccessor getField(final FieldDescriptor field) { | |
| 1789 if (field.getContainingType() != descriptor) { | |
| 1790 throw new IllegalArgumentException( | |
| 1791 "FieldDescriptor does not match message type."); | |
| 1792 } else if (field.isExtension()) { | |
| 1793 // If this type had extensions, it would subclass ExtendableMessage, | |
| 1794 // which overrides the reflection interface to handle extensions. | |
| 1795 throw new IllegalArgumentException( | |
| 1796 "This type does not have extensions."); | |
| 1797 } | |
| 1798 return fields[field.getIndex()]; | |
| 1799 } | |
| 1800 | |
| 1801 /** Get the OneofAccessor for a particular oneof. */ | |
| 1802 private OneofAccessor getOneof(final OneofDescriptor oneof) { | |
| 1803 if (oneof.getContainingType() != descriptor) { | |
| 1804 throw new IllegalArgumentException( | |
| 1805 "OneofDescriptor does not match message type."); | |
| 1806 } | |
| 1807 return oneofs[oneof.getIndex()]; | |
| 1808 } | |
| 1809 | |
| 1810 /** | |
| 1811 * Abstract interface that provides access to a single field. This is | |
| 1812 * implemented differently depending on the field type and cardinality. | |
| 1813 */ | |
| 1814 private interface FieldAccessor { | |
| 1815 Object get(GeneratedMessageV3 message); | |
| 1816 Object get(GeneratedMessageV3.Builder builder); | |
| 1817 Object getRaw(GeneratedMessageV3 message); | |
| 1818 Object getRaw(GeneratedMessageV3.Builder builder); | |
| 1819 void set(Builder builder, Object value); | |
| 1820 Object getRepeated(GeneratedMessageV3 message, int index); | |
| 1821 Object getRepeated(GeneratedMessageV3.Builder builder, int index); | |
| 1822 Object getRepeatedRaw(GeneratedMessageV3 message, int index); | |
| 1823 Object getRepeatedRaw(GeneratedMessageV3.Builder builder, int index); | |
| 1824 void setRepeated(Builder builder, | |
| 1825 int index, Object value); | |
| 1826 void addRepeated(Builder builder, Object value); | |
| 1827 boolean has(GeneratedMessageV3 message); | |
| 1828 boolean has(GeneratedMessageV3.Builder builder); | |
| 1829 int getRepeatedCount(GeneratedMessageV3 message); | |
| 1830 int getRepeatedCount(GeneratedMessageV3.Builder builder); | |
| 1831 void clear(Builder builder); | |
| 1832 Message.Builder newBuilder(); | |
| 1833 Message.Builder getBuilder(GeneratedMessageV3.Builder builder); | |
| 1834 Message.Builder getRepeatedBuilder(GeneratedMessageV3.Builder builder, | |
| 1835 int index); | |
| 1836 } | |
| 1837 | |
| 1838 /** OneofAccessor provides access to a single oneof. */ | |
| 1839 private static class OneofAccessor { | |
| 1840 OneofAccessor( | |
| 1841 final Descriptor descriptor, final String camelCaseName, | |
| 1842 final Class<? extends GeneratedMessageV3> messageClass, | |
| 1843 final Class<? extends Builder> builderClass) { | |
| 1844 this.descriptor = descriptor; | |
| 1845 caseMethod = | |
| 1846 getMethodOrDie(messageClass, "get" + camelCaseName + "Case"); | |
| 1847 caseMethodBuilder = | |
| 1848 getMethodOrDie(builderClass, "get" + camelCaseName + "Case"); | |
| 1849 clearMethod = getMethodOrDie(builderClass, "clear" + camelCaseName); | |
| 1850 } | |
| 1851 | |
| 1852 private final Descriptor descriptor; | |
| 1853 private final Method caseMethod; | |
| 1854 private final Method caseMethodBuilder; | |
| 1855 private final Method clearMethod; | |
| 1856 | |
| 1857 public boolean has(final GeneratedMessageV3 message) { | |
| 1858 if (((Internal.EnumLite) invokeOrDie(caseMethod, message)).getNumber() =
= 0) { | |
| 1859 return false; | |
| 1860 } | |
| 1861 return true; | |
| 1862 } | |
| 1863 | |
| 1864 public boolean has(GeneratedMessageV3.Builder builder) { | |
| 1865 if (((Internal.EnumLite) invokeOrDie(caseMethodBuilder, builder)).getNum
ber() == 0) { | |
| 1866 return false; | |
| 1867 } | |
| 1868 return true; | |
| 1869 } | |
| 1870 | |
| 1871 public FieldDescriptor get(final GeneratedMessageV3 message) { | |
| 1872 int fieldNumber = ((Internal.EnumLite) invokeOrDie(caseMethod, message))
.getNumber(); | |
| 1873 if (fieldNumber > 0) { | |
| 1874 return descriptor.findFieldByNumber(fieldNumber); | |
| 1875 } | |
| 1876 return null; | |
| 1877 } | |
| 1878 | |
| 1879 public FieldDescriptor get(GeneratedMessageV3.Builder builder) { | |
| 1880 int fieldNumber = ((Internal.EnumLite) invokeOrDie(caseMethodBuilder, bu
ilder)).getNumber(); | |
| 1881 if (fieldNumber > 0) { | |
| 1882 return descriptor.findFieldByNumber(fieldNumber); | |
| 1883 } | |
| 1884 return null; | |
| 1885 } | |
| 1886 | |
| 1887 public void clear(final Builder builder) { | |
| 1888 invokeOrDie(clearMethod, builder); | |
| 1889 } | |
| 1890 } | |
| 1891 | |
| 1892 private static boolean supportFieldPresence(FileDescriptor file) { | |
| 1893 return file.getSyntax() == FileDescriptor.Syntax.PROTO2; | |
| 1894 } | |
| 1895 | |
| 1896 // --------------------------------------------------------------- | |
| 1897 | |
| 1898 private static class SingularFieldAccessor implements FieldAccessor { | |
| 1899 SingularFieldAccessor( | |
| 1900 final FieldDescriptor descriptor, final String camelCaseName, | |
| 1901 final Class<? extends GeneratedMessageV3> messageClass, | |
| 1902 final Class<? extends Builder> builderClass, | |
| 1903 final String containingOneofCamelCaseName) { | |
| 1904 field = descriptor; | |
| 1905 isOneofField = descriptor.getContainingOneof() != null; | |
| 1906 hasHasMethod = supportFieldPresence(descriptor.getFile()) | |
| 1907 || (!isOneofField && descriptor.getJavaType() == FieldDescriptor.Jav
aType.MESSAGE); | |
| 1908 getMethod = getMethodOrDie(messageClass, "get" + camelCaseName); | |
| 1909 getMethodBuilder = getMethodOrDie(builderClass, "get" + camelCaseName); | |
| 1910 type = getMethod.getReturnType(); | |
| 1911 setMethod = getMethodOrDie(builderClass, "set" + camelCaseName, type); | |
| 1912 hasMethod = | |
| 1913 hasHasMethod ? getMethodOrDie(messageClass, "has" + camelCaseName) :
null; | |
| 1914 hasMethodBuilder = | |
| 1915 hasHasMethod ? getMethodOrDie(builderClass, "has" + camelCaseName) :
null; | |
| 1916 clearMethod = getMethodOrDie(builderClass, "clear" + camelCaseName); | |
| 1917 caseMethod = isOneofField ? getMethodOrDie( | |
| 1918 messageClass, "get" + containingOneofCamelCaseName + "Case") : null; | |
| 1919 caseMethodBuilder = isOneofField ? getMethodOrDie( | |
| 1920 builderClass, "get" + containingOneofCamelCaseName + "Case") : null; | |
| 1921 } | |
| 1922 | |
| 1923 // Note: We use Java reflection to call public methods rather than | |
| 1924 // access private fields directly as this avoids runtime security | |
| 1925 // checks. | |
| 1926 protected final Class<?> type; | |
| 1927 protected final Method getMethod; | |
| 1928 protected final Method getMethodBuilder; | |
| 1929 protected final Method setMethod; | |
| 1930 protected final Method hasMethod; | |
| 1931 protected final Method hasMethodBuilder; | |
| 1932 protected final Method clearMethod; | |
| 1933 protected final Method caseMethod; | |
| 1934 protected final Method caseMethodBuilder; | |
| 1935 protected final FieldDescriptor field; | |
| 1936 protected final boolean isOneofField; | |
| 1937 protected final boolean hasHasMethod; | |
| 1938 | |
| 1939 private int getOneofFieldNumber(final GeneratedMessageV3 message) { | |
| 1940 return ((Internal.EnumLite) invokeOrDie(caseMethod, message)).getNumber(
); | |
| 1941 } | |
| 1942 | |
| 1943 private int getOneofFieldNumber(final GeneratedMessageV3.Builder builder)
{ | |
| 1944 return ((Internal.EnumLite) invokeOrDie(caseMethodBuilder, builder)).get
Number(); | |
| 1945 } | |
| 1946 | |
| 1947 @Override | |
| 1948 public Object get(final GeneratedMessageV3 message) { | |
| 1949 return invokeOrDie(getMethod, message); | |
| 1950 } | |
| 1951 @Override | |
| 1952 public Object get(GeneratedMessageV3.Builder builder) { | |
| 1953 return invokeOrDie(getMethodBuilder, builder); | |
| 1954 } | |
| 1955 @Override | |
| 1956 public Object getRaw(final GeneratedMessageV3 message) { | |
| 1957 return get(message); | |
| 1958 } | |
| 1959 @Override | |
| 1960 public Object getRaw(GeneratedMessageV3.Builder builder) { | |
| 1961 return get(builder); | |
| 1962 } | |
| 1963 @Override | |
| 1964 public void set(final Builder builder, final Object value) { | |
| 1965 invokeOrDie(setMethod, builder, value); | |
| 1966 } | |
| 1967 @Override | |
| 1968 public Object getRepeated(final GeneratedMessageV3 message, final int inde
x) { | |
| 1969 throw new UnsupportedOperationException( | |
| 1970 "getRepeatedField() called on a singular field."); | |
| 1971 } | |
| 1972 @Override | |
| 1973 public Object getRepeatedRaw(final GeneratedMessageV3 message, final int i
ndex) { | |
| 1974 throw new UnsupportedOperationException( | |
| 1975 "getRepeatedFieldRaw() called on a singular field."); | |
| 1976 } | |
| 1977 @Override | |
| 1978 public Object getRepeated(GeneratedMessageV3.Builder builder, int index) { | |
| 1979 throw new UnsupportedOperationException( | |
| 1980 "getRepeatedField() called on a singular field."); | |
| 1981 } | |
| 1982 @Override | |
| 1983 public Object getRepeatedRaw(GeneratedMessageV3.Builder builder, int index
) { | |
| 1984 throw new UnsupportedOperationException( | |
| 1985 "getRepeatedFieldRaw() called on a singular field."); | |
| 1986 } | |
| 1987 @Override | |
| 1988 public void setRepeated(final Builder builder, final int index, final Obje
ct value) { | |
| 1989 throw new UnsupportedOperationException( | |
| 1990 "setRepeatedField() called on a singular field."); | |
| 1991 } | |
| 1992 @Override | |
| 1993 public void addRepeated(final Builder builder, final Object value) { | |
| 1994 throw new UnsupportedOperationException( | |
| 1995 "addRepeatedField() called on a singular field."); | |
| 1996 } | |
| 1997 @Override | |
| 1998 public boolean has(final GeneratedMessageV3 message) { | |
| 1999 if (!hasHasMethod) { | |
| 2000 if (isOneofField) { | |
| 2001 return getOneofFieldNumber(message) == field.getNumber(); | |
| 2002 } | |
| 2003 return !get(message).equals(field.getDefaultValue()); | |
| 2004 } | |
| 2005 return (Boolean) invokeOrDie(hasMethod, message); | |
| 2006 } | |
| 2007 @Override | |
| 2008 public boolean has(GeneratedMessageV3.Builder builder) { | |
| 2009 if (!hasHasMethod) { | |
| 2010 if (isOneofField) { | |
| 2011 return getOneofFieldNumber(builder) == field.getNumber(); | |
| 2012 } | |
| 2013 return !get(builder).equals(field.getDefaultValue()); | |
| 2014 } | |
| 2015 return (Boolean) invokeOrDie(hasMethodBuilder, builder); | |
| 2016 } | |
| 2017 @Override | |
| 2018 public int getRepeatedCount(final GeneratedMessageV3 message) { | |
| 2019 throw new UnsupportedOperationException( | |
| 2020 "getRepeatedFieldSize() called on a singular field."); | |
| 2021 } | |
| 2022 @Override | |
| 2023 public int getRepeatedCount(GeneratedMessageV3.Builder builder) { | |
| 2024 throw new UnsupportedOperationException( | |
| 2025 "getRepeatedFieldSize() called on a singular field."); | |
| 2026 } | |
| 2027 @Override | |
| 2028 public void clear(final Builder builder) { | |
| 2029 invokeOrDie(clearMethod, builder); | |
| 2030 } | |
| 2031 @Override | |
| 2032 public Message.Builder newBuilder() { | |
| 2033 throw new UnsupportedOperationException( | |
| 2034 "newBuilderForField() called on a non-Message type."); | |
| 2035 } | |
| 2036 @Override | |
| 2037 public Message.Builder getBuilder(GeneratedMessageV3.Builder builder) { | |
| 2038 throw new UnsupportedOperationException( | |
| 2039 "getFieldBuilder() called on a non-Message type."); | |
| 2040 } | |
| 2041 @Override | |
| 2042 public Message.Builder getRepeatedBuilder(GeneratedMessageV3.Builder build
er, int index) { | |
| 2043 throw new UnsupportedOperationException( | |
| 2044 "getRepeatedFieldBuilder() called on a non-Message type."); | |
| 2045 } | |
| 2046 } | |
| 2047 | |
| 2048 private static class RepeatedFieldAccessor implements FieldAccessor { | |
| 2049 protected final Class type; | |
| 2050 protected final Method getMethod; | |
| 2051 protected final Method getMethodBuilder; | |
| 2052 protected final Method getRepeatedMethod; | |
| 2053 protected final Method getRepeatedMethodBuilder; | |
| 2054 protected final Method setRepeatedMethod; | |
| 2055 protected final Method addRepeatedMethod; | |
| 2056 protected final Method getCountMethod; | |
| 2057 protected final Method getCountMethodBuilder; | |
| 2058 protected final Method clearMethod; | |
| 2059 | |
| 2060 RepeatedFieldAccessor( | |
| 2061 final FieldDescriptor descriptor, final String camelCaseName, | |
| 2062 final Class<? extends GeneratedMessageV3> messageClass, | |
| 2063 final Class<? extends Builder> builderClass) { | |
| 2064 getMethod = getMethodOrDie(messageClass, | |
| 2065 "get" + camelCaseName + "List"); | |
| 2066 getMethodBuilder = getMethodOrDie(builderClass, | |
| 2067 "get" + camelCaseName + "List"); | |
| 2068 getRepeatedMethod = | |
| 2069 getMethodOrDie(messageClass, "get" + camelCaseName, Integer.TYPE); | |
| 2070 getRepeatedMethodBuilder = | |
| 2071 getMethodOrDie(builderClass, "get" + camelCaseName, Integer.TYPE); | |
| 2072 type = getRepeatedMethod.getReturnType(); | |
| 2073 setRepeatedMethod = | |
| 2074 getMethodOrDie(builderClass, "set" + camelCaseName, | |
| 2075 Integer.TYPE, type); | |
| 2076 addRepeatedMethod = | |
| 2077 getMethodOrDie(builderClass, "add" + camelCaseName, type); | |
| 2078 getCountMethod = | |
| 2079 getMethodOrDie(messageClass, "get" + camelCaseName + "Count"); | |
| 2080 getCountMethodBuilder = | |
| 2081 getMethodOrDie(builderClass, "get" + camelCaseName + "Count"); | |
| 2082 | |
| 2083 clearMethod = getMethodOrDie(builderClass, "clear" + camelCaseName); | |
| 2084 } | |
| 2085 | |
| 2086 @Override | |
| 2087 public Object get(final GeneratedMessageV3 message) { | |
| 2088 return invokeOrDie(getMethod, message); | |
| 2089 } | |
| 2090 @Override | |
| 2091 public Object get(GeneratedMessageV3.Builder builder) { | |
| 2092 return invokeOrDie(getMethodBuilder, builder); | |
| 2093 } | |
| 2094 @Override | |
| 2095 public Object getRaw(final GeneratedMessageV3 message) { | |
| 2096 return get(message); | |
| 2097 } | |
| 2098 @Override | |
| 2099 public Object getRaw(GeneratedMessageV3.Builder builder) { | |
| 2100 return get(builder); | |
| 2101 } | |
| 2102 @Override | |
| 2103 public void set(final Builder builder, final Object value) { | |
| 2104 // Add all the elements individually. This serves two purposes: | |
| 2105 // 1) Verifies that each element has the correct type. | |
| 2106 // 2) Insures that the caller cannot modify the list later on and | |
| 2107 // have the modifications be reflected in the message. | |
| 2108 clear(builder); | |
| 2109 for (final Object element : (List<?>) value) { | |
| 2110 addRepeated(builder, element); | |
| 2111 } | |
| 2112 } | |
| 2113 @Override | |
| 2114 public Object getRepeated(final GeneratedMessageV3 message, final int inde
x) { | |
| 2115 return invokeOrDie(getRepeatedMethod, message, index); | |
| 2116 } | |
| 2117 @Override | |
| 2118 public Object getRepeated(GeneratedMessageV3.Builder builder, int index) { | |
| 2119 return invokeOrDie(getRepeatedMethodBuilder, builder, index); | |
| 2120 } | |
| 2121 @Override | |
| 2122 public Object getRepeatedRaw(GeneratedMessageV3 message, int index) { | |
| 2123 return getRepeated(message, index); | |
| 2124 } | |
| 2125 @Override | |
| 2126 public Object getRepeatedRaw(GeneratedMessageV3.Builder builder, int index
) { | |
| 2127 return getRepeated(builder, index); | |
| 2128 } | |
| 2129 @Override | |
| 2130 public void setRepeated(final Builder builder, final int index, final Obje
ct value) { | |
| 2131 invokeOrDie(setRepeatedMethod, builder, index, value); | |
| 2132 } | |
| 2133 @Override | |
| 2134 public void addRepeated(final Builder builder, final Object value) { | |
| 2135 invokeOrDie(addRepeatedMethod, builder, value); | |
| 2136 } | |
| 2137 @Override | |
| 2138 public boolean has(final GeneratedMessageV3 message) { | |
| 2139 throw new UnsupportedOperationException( | |
| 2140 "hasField() called on a repeated field."); | |
| 2141 } | |
| 2142 @Override | |
| 2143 public boolean has(GeneratedMessageV3.Builder builder) { | |
| 2144 throw new UnsupportedOperationException( | |
| 2145 "hasField() called on a repeated field."); | |
| 2146 } | |
| 2147 @Override | |
| 2148 public int getRepeatedCount(final GeneratedMessageV3 message) { | |
| 2149 return (Integer) invokeOrDie(getCountMethod, message); | |
| 2150 } | |
| 2151 @Override | |
| 2152 public int getRepeatedCount(GeneratedMessageV3.Builder builder) { | |
| 2153 return (Integer) invokeOrDie(getCountMethodBuilder, builder); | |
| 2154 } | |
| 2155 @Override | |
| 2156 public void clear(final Builder builder) { | |
| 2157 invokeOrDie(clearMethod, builder); | |
| 2158 } | |
| 2159 @Override | |
| 2160 public Message.Builder newBuilder() { | |
| 2161 throw new UnsupportedOperationException( | |
| 2162 "newBuilderForField() called on a non-Message type."); | |
| 2163 } | |
| 2164 @Override | |
| 2165 public Message.Builder getBuilder(GeneratedMessageV3.Builder builder) { | |
| 2166 throw new UnsupportedOperationException( | |
| 2167 "getFieldBuilder() called on a non-Message type."); | |
| 2168 } | |
| 2169 @Override | |
| 2170 public Message.Builder getRepeatedBuilder(GeneratedMessageV3.Builder build
er, int index) { | |
| 2171 throw new UnsupportedOperationException( | |
| 2172 "getRepeatedFieldBuilder() called on a non-Message type."); | |
| 2173 } | |
| 2174 } | |
| 2175 | |
| 2176 private static class MapFieldAccessor implements FieldAccessor { | |
| 2177 MapFieldAccessor( | |
| 2178 final FieldDescriptor descriptor, final String camelCaseName, | |
| 2179 final Class<? extends GeneratedMessageV3> messageClass, | |
| 2180 final Class<? extends Builder> builderClass) { | |
| 2181 field = descriptor; | |
| 2182 Method getDefaultInstanceMethod = | |
| 2183 getMethodOrDie(messageClass, "getDefaultInstance"); | |
| 2184 MapField defaultMapField = getMapField( | |
| 2185 (GeneratedMessageV3) invokeOrDie(getDefaultInstanceMethod, null)); | |
| 2186 mapEntryMessageDefaultInstance = | |
| 2187 defaultMapField.getMapEntryMessageDefaultInstance(); | |
| 2188 } | |
| 2189 | |
| 2190 private final FieldDescriptor field; | |
| 2191 private final Message mapEntryMessageDefaultInstance; | |
| 2192 | |
| 2193 private MapField<?, ?> getMapField(GeneratedMessageV3 message) { | |
| 2194 return (MapField<?, ?>) message.internalGetMapField(field.getNumber()); | |
| 2195 } | |
| 2196 | |
| 2197 private MapField<?, ?> getMapField(GeneratedMessageV3.Builder builder) { | |
| 2198 return (MapField<?, ?>) builder.internalGetMapField(field.getNumber()); | |
| 2199 } | |
| 2200 | |
| 2201 private MapField<?, ?> getMutableMapField( | |
| 2202 GeneratedMessageV3.Builder builder) { | |
| 2203 return (MapField<?, ?>) builder.internalGetMutableMapField( | |
| 2204 field.getNumber()); | |
| 2205 } | |
| 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 | |
| 2221 @Override | |
| 2222 public Object get(GeneratedMessageV3 message) { | |
| 2223 List result = new ArrayList(); | |
| 2224 for (int i = 0; i < getRepeatedCount(message); i++) { | |
| 2225 result.add(getRepeated(message, i)); | |
| 2226 } | |
| 2227 return Collections.unmodifiableList(result); | |
| 2228 } | |
| 2229 | |
| 2230 @Override | |
| 2231 public Object get(Builder builder) { | |
| 2232 List result = new ArrayList(); | |
| 2233 for (int i = 0; i < getRepeatedCount(builder); i++) { | |
| 2234 result.add(getRepeated(builder, i)); | |
| 2235 } | |
| 2236 return Collections.unmodifiableList(result); | |
| 2237 } | |
| 2238 | |
| 2239 @Override | |
| 2240 public Object getRaw(GeneratedMessageV3 message) { | |
| 2241 return get(message); | |
| 2242 } | |
| 2243 | |
| 2244 @Override | |
| 2245 public Object getRaw(GeneratedMessageV3.Builder builder) { | |
| 2246 return get(builder); | |
| 2247 } | |
| 2248 | |
| 2249 @Override | |
| 2250 public void set(Builder builder, Object value) { | |
| 2251 clear(builder); | |
| 2252 for (Object entry : (List) value) { | |
| 2253 addRepeated(builder, entry); | |
| 2254 } | |
| 2255 } | |
| 2256 | |
| 2257 @Override | |
| 2258 public Object getRepeated(GeneratedMessageV3 message, int index) { | |
| 2259 return getMapField(message).getList().get(index); | |
| 2260 } | |
| 2261 | |
| 2262 @Override | |
| 2263 public Object getRepeated(Builder builder, int index) { | |
| 2264 return getMapField(builder).getList().get(index); | |
| 2265 } | |
| 2266 | |
| 2267 @Override | |
| 2268 public Object getRepeatedRaw(GeneratedMessageV3 message, int index) { | |
| 2269 return getRepeated(message, index); | |
| 2270 } | |
| 2271 | |
| 2272 @Override | |
| 2273 public Object getRepeatedRaw(Builder builder, int index) { | |
| 2274 return getRepeated(builder, index); | |
| 2275 } | |
| 2276 | |
| 2277 @Override | |
| 2278 public void setRepeated(Builder builder, int index, Object value) { | |
| 2279 getMutableMapField(builder).getMutableList().set(index, coerceType((Mess
age) value)); | |
| 2280 } | |
| 2281 | |
| 2282 @Override | |
| 2283 public void addRepeated(Builder builder, Object value) { | |
| 2284 getMutableMapField(builder).getMutableList().add(coerceType((Message) va
lue)); | |
| 2285 } | |
| 2286 | |
| 2287 @Override | |
| 2288 public boolean has(GeneratedMessageV3 message) { | |
| 2289 throw new UnsupportedOperationException( | |
| 2290 "hasField() is not supported for repeated fields."); | |
| 2291 } | |
| 2292 | |
| 2293 @Override | |
| 2294 public boolean has(Builder builder) { | |
| 2295 throw new UnsupportedOperationException( | |
| 2296 "hasField() is not supported for repeated fields."); | |
| 2297 } | |
| 2298 | |
| 2299 @Override | |
| 2300 public int getRepeatedCount(GeneratedMessageV3 message) { | |
| 2301 return getMapField(message).getList().size(); | |
| 2302 } | |
| 2303 | |
| 2304 @Override | |
| 2305 public int getRepeatedCount(Builder builder) { | |
| 2306 return getMapField(builder).getList().size(); | |
| 2307 } | |
| 2308 | |
| 2309 @Override | |
| 2310 public void clear(Builder builder) { | |
| 2311 getMutableMapField(builder).getMutableList().clear(); | |
| 2312 } | |
| 2313 | |
| 2314 @Override | |
| 2315 public com.google.protobuf.Message.Builder newBuilder() { | |
| 2316 return mapEntryMessageDefaultInstance.newBuilderForType(); | |
| 2317 } | |
| 2318 | |
| 2319 @Override | |
| 2320 public com.google.protobuf.Message.Builder getBuilder(Builder builder) { | |
| 2321 throw new UnsupportedOperationException( | |
| 2322 "Nested builder not supported for map fields."); | |
| 2323 } | |
| 2324 | |
| 2325 @Override | |
| 2326 public com.google.protobuf.Message.Builder getRepeatedBuilder(Builder buil
der, int index) { | |
| 2327 throw new UnsupportedOperationException( | |
| 2328 "Nested builder not supported for map fields."); | |
| 2329 } | |
| 2330 } | |
| 2331 | |
| 2332 // --------------------------------------------------------------- | |
| 2333 | |
| 2334 private static final class SingularEnumFieldAccessor | |
| 2335 extends SingularFieldAccessor { | |
| 2336 SingularEnumFieldAccessor( | |
| 2337 final FieldDescriptor descriptor, final String camelCaseName, | |
| 2338 final Class<? extends GeneratedMessageV3> messageClass, | |
| 2339 final Class<? extends Builder> builderClass, | |
| 2340 final String containingOneofCamelCaseName) { | |
| 2341 super(descriptor, camelCaseName, messageClass, builderClass, containingO
neofCamelCaseName); | |
| 2342 | |
| 2343 enumDescriptor = descriptor.getEnumType(); | |
| 2344 | |
| 2345 valueOfMethod = getMethodOrDie(type, "valueOf", | |
| 2346 EnumValueDescriptor.class); | |
| 2347 getValueDescriptorMethod = | |
| 2348 getMethodOrDie(type, "getValueDescriptor"); | |
| 2349 | |
| 2350 supportUnknownEnumValue = descriptor.getFile().supportsUnknownEnumValue(
); | |
| 2351 if (supportUnknownEnumValue) { | |
| 2352 getValueMethod = | |
| 2353 getMethodOrDie(messageClass, "get" + camelCaseName + "Value"); | |
| 2354 getValueMethodBuilder = | |
| 2355 getMethodOrDie(builderClass, "get" + camelCaseName + "Value"); | |
| 2356 setValueMethod = | |
| 2357 getMethodOrDie(builderClass, "set" + camelCaseName + "Value", int.
class); | |
| 2358 } | |
| 2359 } | |
| 2360 | |
| 2361 private EnumDescriptor enumDescriptor; | |
| 2362 | |
| 2363 private Method valueOfMethod; | |
| 2364 private Method getValueDescriptorMethod; | |
| 2365 | |
| 2366 private boolean supportUnknownEnumValue; | |
| 2367 private Method getValueMethod; | |
| 2368 private Method getValueMethodBuilder; | |
| 2369 private Method setValueMethod; | |
| 2370 | |
| 2371 @Override | |
| 2372 public Object get(final GeneratedMessageV3 message) { | |
| 2373 if (supportUnknownEnumValue) { | |
| 2374 int value = (Integer) invokeOrDie(getValueMethod, message); | |
| 2375 return enumDescriptor.findValueByNumberCreatingIfUnknown(value); | |
| 2376 } | |
| 2377 return invokeOrDie(getValueDescriptorMethod, super.get(message)); | |
| 2378 } | |
| 2379 | |
| 2380 @Override | |
| 2381 public Object get(final GeneratedMessageV3.Builder builder) { | |
| 2382 if (supportUnknownEnumValue) { | |
| 2383 int value = (Integer) invokeOrDie(getValueMethodBuilder, builder); | |
| 2384 return enumDescriptor.findValueByNumberCreatingIfUnknown(value); | |
| 2385 } | |
| 2386 return invokeOrDie(getValueDescriptorMethod, super.get(builder)); | |
| 2387 } | |
| 2388 | |
| 2389 @Override | |
| 2390 public void set(final Builder builder, final Object value) { | |
| 2391 if (supportUnknownEnumValue) { | |
| 2392 invokeOrDie(setValueMethod, builder, | |
| 2393 ((EnumValueDescriptor) value).getNumber()); | |
| 2394 return; | |
| 2395 } | |
| 2396 super.set(builder, invokeOrDie(valueOfMethod, null, value)); | |
| 2397 } | |
| 2398 } | |
| 2399 | |
| 2400 private static final class RepeatedEnumFieldAccessor | |
| 2401 extends RepeatedFieldAccessor { | |
| 2402 RepeatedEnumFieldAccessor( | |
| 2403 final FieldDescriptor descriptor, final String camelCaseName, | |
| 2404 final Class<? extends GeneratedMessageV3> messageClass, | |
| 2405 final Class<? extends Builder> builderClass) { | |
| 2406 super(descriptor, camelCaseName, messageClass, builderClass); | |
| 2407 | |
| 2408 enumDescriptor = descriptor.getEnumType(); | |
| 2409 | |
| 2410 valueOfMethod = getMethodOrDie(type, "valueOf", | |
| 2411 EnumValueDescriptor.class); | |
| 2412 getValueDescriptorMethod = | |
| 2413 getMethodOrDie(type, "getValueDescriptor"); | |
| 2414 | |
| 2415 supportUnknownEnumValue = descriptor.getFile().supportsUnknownEnumValue(
); | |
| 2416 if (supportUnknownEnumValue) { | |
| 2417 getRepeatedValueMethod = | |
| 2418 getMethodOrDie(messageClass, "get" + camelCaseName + "Value", int.
class); | |
| 2419 getRepeatedValueMethodBuilder = | |
| 2420 getMethodOrDie(builderClass, "get" + camelCaseName + "Value", int.
class); | |
| 2421 setRepeatedValueMethod = | |
| 2422 getMethodOrDie(builderClass, "set" + camelCaseName + "Value", int.
class, int.class); | |
| 2423 addRepeatedValueMethod = | |
| 2424 getMethodOrDie(builderClass, "add" + camelCaseName + "Value", int.
class); | |
| 2425 } | |
| 2426 } | |
| 2427 private EnumDescriptor enumDescriptor; | |
| 2428 | |
| 2429 private final Method valueOfMethod; | |
| 2430 private final Method getValueDescriptorMethod; | |
| 2431 | |
| 2432 private boolean supportUnknownEnumValue; | |
| 2433 private Method getRepeatedValueMethod; | |
| 2434 private Method getRepeatedValueMethodBuilder; | |
| 2435 private Method setRepeatedValueMethod; | |
| 2436 private Method addRepeatedValueMethod; | |
| 2437 | |
| 2438 @Override | |
| 2439 @SuppressWarnings("unchecked") | |
| 2440 public Object get(final GeneratedMessageV3 message) { | |
| 2441 final List newList = new ArrayList(); | |
| 2442 final int size = getRepeatedCount(message); | |
| 2443 for (int i = 0; i < size; i++) { | |
| 2444 newList.add(getRepeated(message, i)); | |
| 2445 } | |
| 2446 return Collections.unmodifiableList(newList); | |
| 2447 } | |
| 2448 | |
| 2449 @Override | |
| 2450 @SuppressWarnings("unchecked") | |
| 2451 public Object get(final GeneratedMessageV3.Builder builder) { | |
| 2452 final List newList = new ArrayList(); | |
| 2453 final int size = getRepeatedCount(builder); | |
| 2454 for (int i = 0; i < size; i++) { | |
| 2455 newList.add(getRepeated(builder, i)); | |
| 2456 } | |
| 2457 return Collections.unmodifiableList(newList); | |
| 2458 } | |
| 2459 | |
| 2460 @Override | |
| 2461 public Object getRepeated(final GeneratedMessageV3 message, | |
| 2462 final int index) { | |
| 2463 if (supportUnknownEnumValue) { | |
| 2464 int value = (Integer) invokeOrDie(getRepeatedValueMethod, message, ind
ex); | |
| 2465 return enumDescriptor.findValueByNumberCreatingIfUnknown(value); | |
| 2466 } | |
| 2467 return invokeOrDie(getValueDescriptorMethod, | |
| 2468 super.getRepeated(message, index)); | |
| 2469 } | |
| 2470 @Override | |
| 2471 public Object getRepeated(final GeneratedMessageV3.Builder builder, | |
| 2472 final int index) { | |
| 2473 if (supportUnknownEnumValue) { | |
| 2474 int value = (Integer) invokeOrDie(getRepeatedValueMethodBuilder, build
er, index); | |
| 2475 return enumDescriptor.findValueByNumberCreatingIfUnknown(value); | |
| 2476 } | |
| 2477 return invokeOrDie(getValueDescriptorMethod, | |
| 2478 super.getRepeated(builder, index)); | |
| 2479 } | |
| 2480 @Override | |
| 2481 public void setRepeated(final Builder builder, | |
| 2482 final int index, final Object value) { | |
| 2483 if (supportUnknownEnumValue) { | |
| 2484 invokeOrDie(setRepeatedValueMethod, builder, index, | |
| 2485 ((EnumValueDescriptor) value).getNumber()); | |
| 2486 return; | |
| 2487 } | |
| 2488 super.setRepeated(builder, index, invokeOrDie(valueOfMethod, null, | |
| 2489 value)); | |
| 2490 } | |
| 2491 @Override | |
| 2492 public void addRepeated(final Builder builder, final Object value) { | |
| 2493 if (supportUnknownEnumValue) { | |
| 2494 invokeOrDie(addRepeatedValueMethod, builder, | |
| 2495 ((EnumValueDescriptor) value).getNumber()); | |
| 2496 return; | |
| 2497 } | |
| 2498 super.addRepeated(builder, invokeOrDie(valueOfMethod, null, value)); | |
| 2499 } | |
| 2500 } | |
| 2501 | |
| 2502 // --------------------------------------------------------------- | |
| 2503 | |
| 2504 /** | |
| 2505 * Field accessor for string fields. | |
| 2506 * | |
| 2507 * <p>This class makes getFooBytes() and setFooBytes() available for | |
| 2508 * reflection API so that reflection based serialize/parse functions can | |
| 2509 * access the raw bytes of the field to preserve non-UTF8 bytes in the | |
| 2510 * string. | |
| 2511 * | |
| 2512 * <p>This ensures the serialize/parse round-trip safety, which is important | |
| 2513 * for servers which forward messages. | |
| 2514 */ | |
| 2515 private static final class SingularStringFieldAccessor | |
| 2516 extends SingularFieldAccessor { | |
| 2517 SingularStringFieldAccessor( | |
| 2518 final FieldDescriptor descriptor, final String camelCaseName, | |
| 2519 final Class<? extends GeneratedMessageV3> messageClass, | |
| 2520 final Class<? extends Builder> builderClass, | |
| 2521 final String containingOneofCamelCaseName) { | |
| 2522 super(descriptor, camelCaseName, messageClass, builderClass, | |
| 2523 containingOneofCamelCaseName); | |
| 2524 getBytesMethod = getMethodOrDie(messageClass, | |
| 2525 "get" + camelCaseName + "Bytes"); | |
| 2526 getBytesMethodBuilder = getMethodOrDie(builderClass, | |
| 2527 "get" + camelCaseName + "Bytes"); | |
| 2528 setBytesMethodBuilder = getMethodOrDie(builderClass, | |
| 2529 "set" + camelCaseName + "Bytes", ByteString.class); | |
| 2530 } | |
| 2531 | |
| 2532 private final Method getBytesMethod; | |
| 2533 private final Method getBytesMethodBuilder; | |
| 2534 private final Method setBytesMethodBuilder; | |
| 2535 | |
| 2536 @Override | |
| 2537 public Object getRaw(final GeneratedMessageV3 message) { | |
| 2538 return invokeOrDie(getBytesMethod, message); | |
| 2539 } | |
| 2540 | |
| 2541 @Override | |
| 2542 public Object getRaw(GeneratedMessageV3.Builder builder) { | |
| 2543 return invokeOrDie(getBytesMethodBuilder, builder); | |
| 2544 } | |
| 2545 | |
| 2546 @Override | |
| 2547 public void set(GeneratedMessageV3.Builder builder, Object value) { | |
| 2548 if (value instanceof ByteString) { | |
| 2549 invokeOrDie(setBytesMethodBuilder, builder, value); | |
| 2550 } else { | |
| 2551 super.set(builder, value); | |
| 2552 } | |
| 2553 } | |
| 2554 } | |
| 2555 | |
| 2556 // --------------------------------------------------------------- | |
| 2557 | |
| 2558 private static final class SingularMessageFieldAccessor | |
| 2559 extends SingularFieldAccessor { | |
| 2560 SingularMessageFieldAccessor( | |
| 2561 final FieldDescriptor descriptor, final String camelCaseName, | |
| 2562 final Class<? extends GeneratedMessageV3> messageClass, | |
| 2563 final Class<? extends Builder> builderClass, | |
| 2564 final String containingOneofCamelCaseName) { | |
| 2565 super(descriptor, camelCaseName, messageClass, builderClass, | |
| 2566 containingOneofCamelCaseName); | |
| 2567 | |
| 2568 newBuilderMethod = getMethodOrDie(type, "newBuilder"); | |
| 2569 getBuilderMethodBuilder = | |
| 2570 getMethodOrDie(builderClass, "get" + camelCaseName + "Builder"); | |
| 2571 } | |
| 2572 | |
| 2573 private final Method newBuilderMethod; | |
| 2574 private final Method getBuilderMethodBuilder; | |
| 2575 | |
| 2576 private Object coerceType(final Object value) { | |
| 2577 if (type.isInstance(value)) { | |
| 2578 return value; | |
| 2579 } else { | |
| 2580 // The value is not the exact right message type. However, if it | |
| 2581 // is an alternative implementation of the same type -- e.g. a | |
| 2582 // DynamicMessage -- we should accept it. In this case we can make | |
| 2583 // a copy of the message. | |
| 2584 return ((Message.Builder) invokeOrDie(newBuilderMethod, null)) | |
| 2585 .mergeFrom((Message) value).buildPartial(); | |
| 2586 } | |
| 2587 } | |
| 2588 | |
| 2589 @Override | |
| 2590 public void set(final Builder builder, final Object value) { | |
| 2591 super.set(builder, coerceType(value)); | |
| 2592 } | |
| 2593 @Override | |
| 2594 public Message.Builder newBuilder() { | |
| 2595 return (Message.Builder) invokeOrDie(newBuilderMethod, null); | |
| 2596 } | |
| 2597 @Override | |
| 2598 public Message.Builder getBuilder(GeneratedMessageV3.Builder builder) { | |
| 2599 return (Message.Builder) invokeOrDie(getBuilderMethodBuilder, builder); | |
| 2600 } | |
| 2601 } | |
| 2602 | |
| 2603 private static final class RepeatedMessageFieldAccessor | |
| 2604 extends RepeatedFieldAccessor { | |
| 2605 RepeatedMessageFieldAccessor( | |
| 2606 final FieldDescriptor descriptor, final String camelCaseName, | |
| 2607 final Class<? extends GeneratedMessageV3> messageClass, | |
| 2608 final Class<? extends Builder> builderClass) { | |
| 2609 super(descriptor, camelCaseName, messageClass, builderClass); | |
| 2610 | |
| 2611 newBuilderMethod = getMethodOrDie(type, "newBuilder"); | |
| 2612 getBuilderMethodBuilder = getMethodOrDie(builderClass, | |
| 2613 "get" + camelCaseName + "Builder", Integer.TYPE); | |
| 2614 } | |
| 2615 | |
| 2616 private final Method newBuilderMethod; | |
| 2617 private final Method getBuilderMethodBuilder; | |
| 2618 | |
| 2619 private Object coerceType(final Object value) { | |
| 2620 if (type.isInstance(value)) { | |
| 2621 return value; | |
| 2622 } else { | |
| 2623 // The value is not the exact right message type. However, if it | |
| 2624 // is an alternative implementation of the same type -- e.g. a | |
| 2625 // DynamicMessage -- we should accept it. In this case we can make | |
| 2626 // a copy of the message. | |
| 2627 return ((Message.Builder) invokeOrDie(newBuilderMethod, null)) | |
| 2628 .mergeFrom((Message) value).build(); | |
| 2629 } | |
| 2630 } | |
| 2631 | |
| 2632 @Override | |
| 2633 public void setRepeated(final Builder builder, | |
| 2634 final int index, final Object value) { | |
| 2635 super.setRepeated(builder, index, coerceType(value)); | |
| 2636 } | |
| 2637 @Override | |
| 2638 public void addRepeated(final Builder builder, final Object value) { | |
| 2639 super.addRepeated(builder, coerceType(value)); | |
| 2640 } | |
| 2641 @Override | |
| 2642 public Message.Builder newBuilder() { | |
| 2643 return (Message.Builder) invokeOrDie(newBuilderMethod, null); | |
| 2644 } | |
| 2645 @Override | |
| 2646 public Message.Builder getRepeatedBuilder( | |
| 2647 final GeneratedMessageV3.Builder builder, final int index) { | |
| 2648 return (Message.Builder) invokeOrDie( | |
| 2649 getBuilderMethodBuilder, builder, index); | |
| 2650 } | |
| 2651 } | |
| 2652 } | |
| 2653 | |
| 2654 /** | |
| 2655 * Replaces this object in the output stream with a serialized form. | |
| 2656 * Part of Java's serialization magic. Generated sub-classes must override | |
| 2657 * this method by calling {@code return super.writeReplace();} | |
| 2658 * @return a SerializedForm of this message | |
| 2659 */ | |
| 2660 protected Object writeReplace() throws ObjectStreamException { | |
| 2661 return new GeneratedMessageLite.SerializedForm(this); | |
| 2662 } | |
| 2663 | |
| 2664 /** | |
| 2665 * Checks that the {@link Extension} is non-Lite and returns it as a | |
| 2666 * {@link GeneratedExtension}. | |
| 2667 */ | |
| 2668 private static <MessageType extends ExtendableMessage<MessageType>, T> | |
| 2669 Extension<MessageType, T> checkNotLite( | |
| 2670 ExtensionLite<MessageType, T> extension) { | |
| 2671 if (extension.isLite()) { | |
| 2672 throw new IllegalArgumentException("Expected non-lite extension."); | |
| 2673 } | |
| 2674 | |
| 2675 return (Extension<MessageType, T>) extension; | |
| 2676 } | |
| 2677 | |
| 2678 protected static int computeStringSize(final int fieldNumber, final Object val
ue) { | |
| 2679 if (value instanceof String) { | |
| 2680 return CodedOutputStream.computeStringSize(fieldNumber, (String) value); | |
| 2681 } else { | |
| 2682 return CodedOutputStream.computeBytesSize(fieldNumber, (ByteString) value)
; | |
| 2683 } | |
| 2684 } | |
| 2685 | |
| 2686 protected static int computeStringSizeNoTag(final Object value) { | |
| 2687 if (value instanceof String) { | |
| 2688 return CodedOutputStream.computeStringSizeNoTag((String) value); | |
| 2689 } else { | |
| 2690 return CodedOutputStream.computeBytesSizeNoTag((ByteString) value); | |
| 2691 } | |
| 2692 } | |
| 2693 | |
| 2694 protected static void writeString( | |
| 2695 CodedOutputStream output, final int fieldNumber, final Object value) throw
s IOException { | |
| 2696 if (value instanceof String) { | |
| 2697 output.writeString(fieldNumber, (String) value); | |
| 2698 } else { | |
| 2699 output.writeBytes(fieldNumber, (ByteString) value); | |
| 2700 } | |
| 2701 } | |
| 2702 | |
| 2703 protected static void writeStringNoTag( | |
| 2704 CodedOutputStream output, final Object value) throws IOException { | |
| 2705 if (value instanceof String) { | |
| 2706 output.writeStringNoTag((String) value); | |
| 2707 } else { | |
| 2708 output.writeBytesNoTag((ByteString) value); | |
| 2709 } | |
| 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 } | |
| 2838 } | |
| OLD | NEW |