aboutsummaryrefslogtreecommitdiff
path: root/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java')
-rw-r--r--java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java105
1 files changed, 64 insertions, 41 deletions
diff --git a/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java b/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java
index 2a5d8b50..4acd8f2f 100644
--- a/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java
+++ b/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java
@@ -30,6 +30,8 @@
package com.google.protobuf;
+import static com.google.protobuf.Internal.checkNotNull;
+
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.EnumDescriptor;
import com.google.protobuf.Descriptors.EnumValueDescriptor;
@@ -41,13 +43,12 @@ import com.google.protobuf.Descriptors.OneofDescriptor;
// class without breaking binary compatibility with old generated code that still subclasses
// the old GeneratedMessageV3 class. To allow these different GeneratedMessageV3V? classes to
// interoperate (e.g., a GeneratedMessageV3V3 object has a message extension field whose class
-// type is GeneratedMessageV3V4), these classes still share a common parent class AbstarctMessage
+// type is GeneratedMessageV3V4), these classes still share a common parent class AbstractMessage
// and are using the same GeneratedMessage.GeneratedExtension class for extension definitions.
// Since this class becomes GeneratedMessageV3V? in opensource, we have to add an import here
// to be able to use GeneratedMessage.GeneratedExtension. The GeneratedExtension definition in
// this file is also excluded from opensource to avoid conflict.
import com.google.protobuf.GeneratedMessage.GeneratedExtension;
-
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectStreamException;
@@ -277,13 +278,30 @@ public abstract class GeneratedMessageV3 extends AbstractMessage
/**
* Called by subclasses to parse an unknown field.
+ *
* @return {@code true} unless the tag is an end-group tag.
*/
protected boolean parseUnknownField(
CodedInputStream input,
UnknownFieldSet.Builder unknownFields,
ExtensionRegistryLite extensionRegistry,
- int tag) throws IOException {
+ int tag)
+ throws IOException {
+ if (input.shouldDiscardUnknownFields()) {
+ return input.skipField(tag);
+ }
+ return unknownFields.mergeFieldFrom(tag, input);
+ }
+
+ protected boolean parseUnknownFieldProto3(
+ CodedInputStream input,
+ UnknownFieldSet.Builder unknownFields,
+ ExtensionRegistryLite extensionRegistry,
+ int tag)
+ throws IOException {
+ if (input.shouldDiscardUnknownFieldsProto3()) {
+ return input.skipField(tag);
+ }
return unknownFields.mergeFieldFrom(tag, input);
}
@@ -340,6 +358,10 @@ public abstract class GeneratedMessageV3 extends AbstractMessage
throw e.unwrapIOException();
}
}
+
+ protected static boolean canUseUnsafe() {
+ return UnsafeUtil.hasUnsafeArrayOperations() && UnsafeUtil.hasUnsafeByteBufferOperations();
+ }
@Override
public void writeTo(final CodedOutputStream output) throws IOException {
@@ -619,17 +641,25 @@ public abstract class GeneratedMessageV3 extends AbstractMessage
return (BuilderType) this;
}
+ protected BuilderType setUnknownFieldsProto3(final UnknownFieldSet unknownFields) {
+ if (CodedInputStream.getProto3DiscardUnknownFieldsDefault()) {
+ return (BuilderType) this;
+ }
+ this.unknownFields = unknownFields;
+ onChanged();
+ return (BuilderType) this;
+ }
+
@Override
public BuilderType mergeUnknownFields(
final UnknownFieldSet unknownFields) {
- this.unknownFields =
+ return setUnknownFields(
UnknownFieldSet.newBuilder(this.unknownFields)
.mergeFrom(unknownFields)
- .build();
- onChanged();
- return (BuilderType) this;
+ .build());
}
+
@Override
public boolean isInitialized() {
for (final FieldDescriptor field : getDescriptorForType().getFields()) {
@@ -666,18 +696,6 @@ public abstract class GeneratedMessageV3 extends AbstractMessage
}
/**
- * Called by subclasses to parse an unknown field.
- * @return {@code true} unless the tag is an end-group tag.
- */
- protected boolean parseUnknownField(
- final CodedInputStream input,
- final UnknownFieldSet.Builder unknownFields,
- final ExtensionRegistryLite extensionRegistry,
- final int tag) throws IOException {
- return unknownFields.mergeFieldFrom(tag, input);
- }
-
- /**
* Implementation of {@link BuilderParent} for giving to our children. This
* small inner class makes it so we don't publicly expose the BuilderParent
* methods.
@@ -866,6 +884,7 @@ public abstract class GeneratedMessageV3 extends AbstractMessage
/** Check if a singular extension is present. */
@Override
+ @SuppressWarnings("unchecked")
public final <Type> boolean hasExtension(final ExtensionLite<MessageType, Type> extensionLite) {
Extension<MessageType, Type> extension = checkNotLite(extensionLite);
@@ -875,6 +894,7 @@ public abstract class GeneratedMessageV3 extends AbstractMessage
/** Get the number of elements in a repeated extension. */
@Override
+ @SuppressWarnings("unchecked")
public final <Type> int getExtensionCount(
final ExtensionLite<MessageType, List<Type>> extensionLite) {
Extension<MessageType, List<Type>> extension = checkNotLite(extensionLite);
@@ -985,8 +1005,23 @@ public abstract class GeneratedMessageV3 extends AbstractMessage
ExtensionRegistryLite extensionRegistry,
int tag) throws IOException {
return MessageReflection.mergeFieldFrom(
- input, unknownFields, extensionRegistry, getDescriptorForType(),
- new MessageReflection.ExtensionAdapter(extensions), tag);
+ input, input.shouldDiscardUnknownFields() ? null : unknownFields, extensionRegistry,
+ getDescriptorForType(), new MessageReflection.ExtensionAdapter(extensions), tag);
+ }
+
+ @Override
+ protected boolean parseUnknownFieldProto3(
+ CodedInputStream input,
+ UnknownFieldSet.Builder unknownFields,
+ ExtensionRegistryLite extensionRegistry,
+ int tag) throws IOException {
+ return MessageReflection.mergeFieldFrom(
+ input,
+ input.shouldDiscardUnknownFieldsProto3() ? null : unknownFields,
+ extensionRegistry,
+ getDescriptorForType(),
+ new MessageReflection.ExtensionAdapter(extensions),
+ tag);
}
@@ -1456,21 +1491,6 @@ public abstract class GeneratedMessageV3 extends AbstractMessage
return super.isInitialized() && extensionsAreInitialized();
}
- /**
- * Called by subclasses to parse an unknown field or an extension.
- * @return {@code true} unless the tag is an end-group tag.
- */
- @Override
- protected boolean parseUnknownField(
- final CodedInputStream input,
- final UnknownFieldSet.Builder unknownFields,
- final ExtensionRegistryLite extensionRegistry,
- final int tag) throws IOException {
- return MessageReflection.mergeFieldFrom(
- input, unknownFields, extensionRegistry, getDescriptorForType(),
- new MessageReflection.BuilderAdapter(this), tag);
- }
-
// ---------------------------------------------------------------
// Reflection
@@ -2219,6 +2239,7 @@ public abstract class GeneratedMessageV3 extends AbstractMessage
}
@Override
+ @SuppressWarnings("unchecked")
public Object get(GeneratedMessageV3 message) {
List result = new ArrayList();
for (int i = 0; i < getRepeatedCount(message); i++) {
@@ -2228,6 +2249,7 @@ public abstract class GeneratedMessageV3 extends AbstractMessage
}
@Override
+ @SuppressWarnings("unchecked")
public Object get(Builder builder) {
List result = new ArrayList();
for (int i = 0; i < getRepeatedCount(builder); i++) {
@@ -2273,7 +2295,7 @@ public abstract class GeneratedMessageV3 extends AbstractMessage
public Object getRepeatedRaw(Builder builder, int index) {
return getRepeated(builder, index);
}
-
+
@Override
public void setRepeated(Builder builder, int index, Object value) {
getMutableMapField(builder).getMutableList().set(index, coerceType((Message) value));
@@ -2674,7 +2696,7 @@ public abstract class GeneratedMessageV3 extends AbstractMessage
return (Extension<MessageType, T>) extension;
}
-
+
protected static int computeStringSize(final int fieldNumber, final Object value) {
if (value instanceof String) {
return CodedOutputStream.computeStringSize(fieldNumber, (String) value);
@@ -2682,7 +2704,7 @@ public abstract class GeneratedMessageV3 extends AbstractMessage
return CodedOutputStream.computeBytesSize(fieldNumber, (ByteString) value);
}
}
-
+
protected static int computeStringSizeNoTag(final Object value) {
if (value instanceof String) {
return CodedOutputStream.computeStringSizeNoTag((String) value);
@@ -2690,7 +2712,7 @@ public abstract class GeneratedMessageV3 extends AbstractMessage
return CodedOutputStream.computeBytesSizeNoTag((ByteString) value);
}
}
-
+
protected static void writeString(
CodedOutputStream output, final int fieldNumber, final Object value) throws IOException {
if (value instanceof String) {
@@ -2699,7 +2721,7 @@ public abstract class GeneratedMessageV3 extends AbstractMessage
output.writeBytes(fieldNumber, (ByteString) value);
}
}
-
+
protected static void writeStringNoTag(
CodedOutputStream output, final Object value) throws IOException {
if (value instanceof String) {
@@ -2836,3 +2858,4 @@ public abstract class GeneratedMessageV3 extends AbstractMessage
}
}
}
+