aboutsummaryrefslogtreecommitdiffhomepage
path: root/java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java')
-rw-r--r--java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java238
1 files changed, 138 insertions, 100 deletions
diff --git a/java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java b/java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java
index c5adc5ad..214971b1 100644
--- a/java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java
+++ b/java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java
@@ -59,15 +59,15 @@ import java.util.Map;
*/
public abstract class GeneratedMessageLite<
MessageType extends GeneratedMessageLite<MessageType, BuilderType>,
- BuilderType extends GeneratedMessageLite.Builder<MessageType, BuilderType>>
+ BuilderType extends GeneratedMessageLite.Builder<MessageType, BuilderType>>
extends AbstractMessageLite<MessageType, BuilderType> {
/** For use by generated code only. Lazily initialized to reduce allocations. */
protected UnknownFieldSetLite unknownFields = UnknownFieldSetLite.getDefaultInstance();
-
+
/** For use by generated code only. */
protected int memoizedSerializedSize = -1;
-
+
@Override
@SuppressWarnings("unchecked") // Guaranteed by runtime.
public final Parser<MessageType> getParserForType() {
@@ -113,7 +113,7 @@ public abstract class GeneratedMessageLite<
}
return memoizedHashCode;
}
-
+
@SuppressWarnings("unchecked") // Guaranteed by runtime
int hashCode(HashCodeVisitor visitor) {
if (memoizedHashCode == 0) {
@@ -125,18 +125,18 @@ public abstract class GeneratedMessageLite<
}
return memoizedHashCode;
}
-
+
@SuppressWarnings("unchecked") // Guaranteed by isInstance + runtime
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
-
+
if (!getDefaultInstanceForType().getClass().isInstance(other)) {
return false;
}
-
+
try {
visit(EqualsVisitor.INSTANCE, (MessageType) other);
} catch (NotEqualsException e) {
@@ -144,7 +144,7 @@ public abstract class GeneratedMessageLite<
}
return true;
}
-
+
/**
* Same as {@link #equals(Object)} but throws {@code NotEqualsException}.
*/
@@ -153,7 +153,7 @@ public abstract class GeneratedMessageLite<
if (this == other) {
return true;
}
-
+
if (!getDefaultInstanceForType().getClass().isInstance(other)) {
return false;
}
@@ -161,7 +161,7 @@ public abstract class GeneratedMessageLite<
visit(visitor, (MessageType) other);
return true;
}
-
+
// The general strategy for unknown fields is to use an UnknownFieldSetLite that is treated as
// mutable during the parsing constructor and immutable after. This allows us to avoid
// any unnecessary intermediary allocations while reducing the generated code size.
@@ -174,10 +174,10 @@ public abstract class GeneratedMessageLite<
unknownFields = UnknownFieldSetLite.newInstance();
}
}
-
+
/**
* Called by subclasses to parse an unknown field. For use by generated code only.
- *
+ *
* @return {@code true} unless the tag is an end-group tag.
*/
protected boolean parseUnknownField(int tag, CodedInputStream input) throws IOException {
@@ -185,7 +185,7 @@ public abstract class GeneratedMessageLite<
if (WireFormat.getTagWireType(tag) == WireFormat.WIRETYPE_END_GROUP) {
return false;
}
-
+
ensureUnknownFieldsInitialized();
return unknownFields.mergeFieldFrom(tag, input);
}
@@ -197,7 +197,7 @@ public abstract class GeneratedMessageLite<
ensureUnknownFieldsInitialized();
unknownFields.mergeVarintField(tag, value);
}
-
+
/**
* Called by subclasses to parse an unknown field. For use by generated code only.
*/
@@ -205,7 +205,7 @@ public abstract class GeneratedMessageLite<
ensureUnknownFieldsInitialized();
unknownFields.mergeLengthDelimitedField(fieldNumber, value);
}
-
+
/**
* Called by subclasses to complete parsing. For use by generated code only.
*/
@@ -292,7 +292,7 @@ public abstract class GeneratedMessageLite<
dynamicMethod(MethodToInvoke.VISIT, visitor, other);
unknownFields = visitor.visitUnknownFields(unknownFields, other.unknownFields);
}
-
+
/**
* Merge some unknown fields into the {@link UnknownFieldSetLite} for this
* message.
@@ -359,9 +359,9 @@ public abstract class GeneratedMessageLite<
if (isBuilt) {
return instance;
}
-
+
instance.makeImmutable();
-
+
isBuilt = true;
return instance;
}
@@ -374,24 +374,24 @@ public abstract class GeneratedMessageLite<
}
return result;
}
-
+
@Override
protected BuilderType internalMergeFrom(MessageType message) {
return mergeFrom(message);
}
-
+
/** All subclasses implement this. */
public BuilderType mergeFrom(MessageType message) {
copyOnWrite();
instance.visit(MergeFromVisitor.INSTANCE, message);
return (BuilderType) this;
}
-
+
@Override
public MessageType getDefaultInstanceForType() {
return defaultInstance;
}
-
+
@Override
public BuilderType mergeFrom(
com.google.protobuf.CodedInputStream input,
@@ -466,12 +466,12 @@ public abstract class GeneratedMessageLite<
super.visit(visitor, other);
extensions = visitor.visitExtensions(extensions, other.extensions);
}
-
+
/**
* Parse an unknown field or an extension. For use by generated code only.
- *
+ *
* <p>For use by generated code only.
- *
+ *
* @return {@code true} unless the tag is an end-group tag.
*/
protected <MessageType extends MessageLite> boolean parseUnknownField(
@@ -590,7 +590,7 @@ public abstract class GeneratedMessageLite<
return true;
}
-
+
private void verifyExtensionContainingType(
final GeneratedExtension<MessageType, ?> extension) {
if (extension.getContainingTypeDefaultInstance() !=
@@ -607,7 +607,7 @@ public abstract class GeneratedMessageLite<
public final <Type> boolean hasExtension(final ExtensionLite<MessageType, Type> extension) {
GeneratedExtension<MessageType, Type> extensionLite =
checkIsLite(extension);
-
+
verifyExtensionContainingType(extensionLite);
return extensions.hasField(extensionLite.descriptor);
}
@@ -618,7 +618,7 @@ public abstract class GeneratedMessageLite<
final ExtensionLite<MessageType, List<Type>> extension) {
GeneratedExtension<MessageType, List<Type>> extensionLite =
checkIsLite(extension);
-
+
verifyExtensionContainingType(extensionLite);
return extensions.getRepeatedFieldCount(extensionLite.descriptor);
}
@@ -629,7 +629,7 @@ public abstract class GeneratedMessageLite<
public final <Type> Type getExtension(final ExtensionLite<MessageType, Type> extension) {
GeneratedExtension<MessageType, Type> extensionLite =
checkIsLite(extension);
-
+
verifyExtensionContainingType(extensionLite);
final Object value = extensions.getField(extensionLite.descriptor);
if (value == null) {
@@ -660,7 +660,7 @@ public abstract class GeneratedMessageLite<
@Override
protected final void makeImmutable() {
super.makeImmutable();
-
+
extensions.makeImmutable();
}
@@ -734,7 +734,7 @@ public abstract class GeneratedMessageLite<
implements ExtendableMessageOrBuilder<MessageType, BuilderType> {
protected ExtendableBuilder(MessageType defaultInstance) {
super(defaultInstance);
-
+
// TODO(dweis): This is kind of an unnecessary clone since we construct a
// new instance in the parent constructor which makes the extensions
// immutable. This extra allocation shouldn't matter in practice
@@ -753,7 +753,7 @@ public abstract class GeneratedMessageLite<
if (!isBuilt) {
return;
}
-
+
super.copyOnWrite();
instance.extensions = instance.extensions.clone();
}
@@ -814,14 +814,14 @@ public abstract class GeneratedMessageLite<
public BuilderType clone() {
return super.clone();
}
-
+
/** Set the value of an extension. */
public final <Type> BuilderType setExtension(
final ExtensionLite<MessageType, Type> extension,
final Type value) {
GeneratedExtension<MessageType, Type> extensionLite =
checkIsLite(extension);
-
+
verifyExtensionContainingType(extensionLite);
copyOnWrite();
instance.extensions.setField(extensionLite.descriptor, extensionLite.toFieldSetType(value));
@@ -834,7 +834,7 @@ public abstract class GeneratedMessageLite<
final int index, final Type value) {
GeneratedExtension<MessageType, List<Type>> extensionLite =
checkIsLite(extension);
-
+
verifyExtensionContainingType(extensionLite);
copyOnWrite();
instance.extensions.setRepeatedField(
@@ -848,7 +848,7 @@ public abstract class GeneratedMessageLite<
final Type value) {
GeneratedExtension<MessageType, List<Type>> extensionLite =
checkIsLite(extension);
-
+
verifyExtensionContainingType(extensionLite);
copyOnWrite();
instance.extensions.addRepeatedField(
@@ -860,7 +860,7 @@ public abstract class GeneratedMessageLite<
public final <Type> BuilderType clearExtension(
final ExtensionLite<MessageType, ?> extension) {
GeneratedExtension<MessageType, ?> extensionLite = checkIsLite(extension);
-
+
verifyExtensionContainingType(extensionLite);
copyOnWrite();
instance.extensions.clearField(extensionLite.descriptor);
@@ -1157,7 +1157,7 @@ public abstract class GeneratedMessageLite<
public static SerializedForm of(MessageLite message) {
return new SerializedForm(message);
}
-
+
private static final long serialVersionUID = 0L;
private final String messageClassName;
@@ -1191,7 +1191,7 @@ public abstract class GeneratedMessageLite<
} catch (ClassNotFoundException e) {
throw new RuntimeException("Unable to find proto buffer class: " + messageClassName, e);
} catch (NoSuchFieldException e) {
- throw new RuntimeException("Unable to find DEFAULT_INSTANCE in " + messageClassName, e);
+ return readResolveFallback();
} catch (SecurityException e) {
throw new RuntimeException("Unable to call DEFAULT_INSTANCE in " + messageClassName, e);
} catch (IllegalAccessException e) {
@@ -1200,8 +1200,35 @@ public abstract class GeneratedMessageLite<
throw new RuntimeException("Unable to understand proto buffer", e);
}
}
+
+ /**
+ * @deprecated from v3.0.0-beta-3+, for compatibility with v2.5.0 and v2.6.1 generated code.
+ */
+ @Deprecated
+ private Object readResolveFallback() throws ObjectStreamException {
+ try {
+ Class<?> messageClass = Class.forName(messageClassName);
+ java.lang.reflect.Field defaultInstanceField =
+ messageClass.getDeclaredField("defaultInstance");
+ defaultInstanceField.setAccessible(true);
+ MessageLite defaultInstance = (MessageLite) defaultInstanceField.get(null);
+ return defaultInstance.newBuilderForType()
+ .mergeFrom(asBytes)
+ .buildPartial();
+ } catch (ClassNotFoundException e) {
+ throw new RuntimeException("Unable to find proto buffer class: " + messageClassName, e);
+ } catch (NoSuchFieldException e) {
+ throw new RuntimeException("Unable to find defaultInstance in " + messageClassName, e);
+ } catch (SecurityException e) {
+ throw new RuntimeException("Unable to call defaultInstance in " + messageClassName, e);
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException("Unable to call parsePartialFrom", e);
+ } catch (InvalidProtocolBufferException e) {
+ throw new RuntimeException("Unable to understand proto buffer", e);
+ }
+ }
}
-
+
/**
* Checks that the {@link Extension} is Lite and returns it as a
* {@link GeneratedExtension}.
@@ -1215,7 +1242,7 @@ public abstract class GeneratedMessageLite<
if (!extension.isLite()) {
throw new IllegalArgumentException("Expected a lite extension.");
}
-
+
return (GeneratedExtension<MessageType, T>) extension;
}
@@ -1227,8 +1254,8 @@ public abstract class GeneratedMessageLite<
protected static final <T extends GeneratedMessageLite<T, ?>> boolean isInitialized(
T message, boolean shouldMemoize) {
return message.dynamicMethod(MethodToInvoke.IS_INITIALIZED, shouldMemoize) != null;
- }
-
+ }
+
protected static final <T extends GeneratedMessageLite<T, ?>> void makeImmutable(T message) {
message.dynamicMethod(MethodToInvoke.MAKE_IMMUTABLE);
}
@@ -1246,7 +1273,7 @@ public abstract class GeneratedMessageLite<
protected static LongList emptyLongList() {
return LongArrayList.emptyList();
}
-
+
protected static LongList mutableCopy(LongList list) {
int size = list.size();
return list.mutableCopyWithCapacity(
@@ -1256,7 +1283,7 @@ public abstract class GeneratedMessageLite<
protected static FloatList emptyFloatList() {
return FloatArrayList.emptyList();
}
-
+
protected static FloatList mutableCopy(FloatList list) {
int size = list.size();
return list.mutableCopyWithCapacity(
@@ -1266,7 +1293,7 @@ public abstract class GeneratedMessageLite<
protected static DoubleList emptyDoubleList() {
return DoubleArrayList.emptyList();
}
-
+
protected static DoubleList mutableCopy(DoubleList list) {
int size = list.size();
return list.mutableCopyWithCapacity(
@@ -1276,7 +1303,7 @@ public abstract class GeneratedMessageLite<
protected static BooleanList emptyBooleanList() {
return BooleanArrayList.emptyList();
}
-
+
protected static BooleanList mutableCopy(BooleanList list) {
int size = list.size();
return list.mutableCopyWithCapacity(
@@ -1286,7 +1313,7 @@ public abstract class GeneratedMessageLite<
protected static <E> ProtobufList<E> emptyProtobufList() {
return ProtobufArrayList.emptyList();
}
-
+
protected static <E> ProtobufList<E> mutableCopy(ProtobufList<E> list) {
int size = list.size();
return list.mutableCopyWithCapacity(
@@ -1300,20 +1327,20 @@ public abstract class GeneratedMessageLite<
*/
protected static class DefaultInstanceBasedParser<T extends GeneratedMessageLite<T, ?>>
extends AbstractParser<T> {
-
+
private T defaultInstance;
-
+
public DefaultInstanceBasedParser(T defaultInstance) {
this.defaultInstance = defaultInstance;
}
-
+
@Override
public T parsePartialFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException {
return GeneratedMessageLite.parsePartialFrom(defaultInstance, input, extensionRegistry);
}
}
-
+
/**
* A static helper method for parsing a partial from input using the extension registry and the
* instance.
@@ -1335,14 +1362,14 @@ public abstract class GeneratedMessageLite<
}
return result;
}
-
+
protected static <T extends GeneratedMessageLite<T, ?>> T parsePartialFrom(
T defaultInstance,
CodedInputStream input)
throws InvalidProtocolBufferException {
return parsePartialFrom(defaultInstance, input, ExtensionRegistryLite.getEmptyRegistry());
}
-
+
/**
* Helper method to check if message is initialized.
*
@@ -1373,7 +1400,7 @@ public abstract class GeneratedMessageLite<
throws InvalidProtocolBufferException {
return checkMessageInitialized(parsePartialFrom(defaultInstance, data, extensionRegistry));
}
-
+
// This is a special case since we want to verify that the last tag is 0. We assume we exhaust the
// ByteString.
private static <T extends GeneratedMessageLite<T, ?>> T parsePartialFrom(
@@ -1393,7 +1420,7 @@ public abstract class GeneratedMessageLite<
throw e;
}
}
-
+
// This is a special case since we want to verify that the last tag is 0. We assume we exhaust the
// ByteString.
private static <T extends GeneratedMessageLite<T, ?>> T parsePartialFrom(
@@ -1477,7 +1504,7 @@ public abstract class GeneratedMessageLite<
return checkMessageInitialized(
parsePartialDelimitedFrom(defaultInstance, input, extensionRegistry));
}
-
+
private static <T extends GeneratedMessageLite<T, ?>> T parsePartialDelimitedFrom(
T defaultInstance,
InputStream input,
@@ -1530,13 +1557,12 @@ public abstract class GeneratedMessageLite<
Object visitOneofLazyMessage(boolean minePresent, Object mine, Object other);
Object visitOneofMessage(boolean minePresent, Object mine, Object other);
void visitOneofNotSet(boolean minePresent);
-
+
/**
* Message fields use null sentinals.
*/
<T extends MessageLite> T visitMessage(T mine, T other);
- LazyFieldLite visitLazyMessage(
- boolean minePresent, LazyFieldLite mine, boolean otherPresent, LazyFieldLite other);
+ LazyFieldLite visitLazyMessage(LazyFieldLite mine, LazyFieldLite other);
<T> ProtobufList<T> visitList(ProtobufList<T> mine, ProtobufList<T> other);
BooleanList visitBooleanList(BooleanList mine, BooleanList other);
@@ -1686,7 +1712,7 @@ public abstract class GeneratedMessageLite<
}
throw NOT_EQUALS;
}
-
+
@Override
public Object visitOneofMessage(boolean minePresent, Object mine, Object other) {
if (minePresent && ((GeneratedMessageLite<?, ?>) mine).equals(this, (MessageLite) other)) {
@@ -1694,7 +1720,7 @@ public abstract class GeneratedMessageLite<
}
throw NOT_EQUALS;
}
-
+
@Override
public void visitOneofNotSet(boolean minePresent) {
if (minePresent) {
@@ -1716,13 +1742,17 @@ public abstract class GeneratedMessageLite<
return mine;
}
-
+
@Override
public LazyFieldLite visitLazyMessage(
- boolean minePresent, LazyFieldLite mine, boolean otherPresent, LazyFieldLite other) {
- if (!minePresent && !otherPresent) {
- return mine;
- } else if (minePresent && otherPresent && mine.equals(other)) {
+ LazyFieldLite mine, LazyFieldLite other) {
+ if (mine == null && other == null) {
+ return null;
+ }
+ if (mine == null || other == null) {
+ throw NOT_EQUALS;
+ }
+ if (mine.equals(other)) {
return mine;
}
throw NOT_EQUALS;
@@ -1813,7 +1843,7 @@ public abstract class GeneratedMessageLite<
// The caller must ensure that the visitor is invoked parameterized with this and this such that
// other is this. This is required due to how oneof cases are handled. See the class comment
// on Visitor for more information.
-
+
private int hashCode = 0;
@Override
@@ -1909,7 +1939,7 @@ public abstract class GeneratedMessageLite<
hashCode = (53 * hashCode) + mine.hashCode();
return mine;
}
-
+
@Override
public Object visitOneofMessage(boolean minePresent, Object mine, Object other) {
return visitMessage((MessageLite) mine, (MessageLite) other);
@@ -1918,7 +1948,7 @@ public abstract class GeneratedMessageLite<
@Override
public void visitOneofNotSet(boolean minePresent) {
if (minePresent) {
- throw new IllegalStateException(); // Can't happen if other == this.
+ throw new IllegalStateException(); // Can't happen if other == this.
}
}
@@ -1939,9 +1969,14 @@ public abstract class GeneratedMessageLite<
}
@Override
- public LazyFieldLite visitLazyMessage(
- boolean minePresent, LazyFieldLite mine, boolean otherPresent, LazyFieldLite other) {
- hashCode = (53 * hashCode) + mine.hashCode();
+ public LazyFieldLite visitLazyMessage(LazyFieldLite mine, LazyFieldLite other) {
+ final int protoHash;
+ if (mine != null) {
+ protoHash = mine.hashCode();
+ } else {
+ protoHash = 37;
+ }
+ hashCode = (53 * hashCode) + protoHash;
return mine;
}
@@ -1996,7 +2031,7 @@ public abstract class GeneratedMessageLite<
hashCode = (53 * hashCode) + mine.hashCode();
return mine;
}
-
+
@Override
public <K, V> MapFieldLite<K, V> visitMap(MapFieldLite<K, V> mine, MapFieldLite<K, V> other) {
hashCode = (53 * hashCode) + mine.hashCode();
@@ -2064,7 +2099,7 @@ public abstract class GeneratedMessageLite<
@Override
public Object visitOneofDouble(boolean minePresent, Object mine, Object other) {
- return other;
+ return other;
}
@Override
@@ -2074,29 +2109,26 @@ public abstract class GeneratedMessageLite<
@Override
public Object visitOneofLong(boolean minePresent, Object mine, Object other) {
- return other;
+ return other;
}
@Override
public Object visitOneofString(boolean minePresent, Object mine, Object other) {
- return other;
+ return other;
}
@Override
public Object visitOneofByteString(boolean minePresent, Object mine, Object other) {
- return other;
+ return other;
}
@Override
public Object visitOneofLazyMessage(boolean minePresent, Object mine, Object other) {
- if (minePresent) {
- LazyFieldLite lazy = (LazyFieldLite) mine;
- lazy.merge((LazyFieldLite) other);
- return lazy;
- }
- return other;
+ LazyFieldLite lazy = minePresent ? (LazyFieldLite) mine : new LazyFieldLite();
+ lazy.merge((LazyFieldLite) other);
+ return lazy;
}
-
+
@Override
public Object visitOneofMessage(boolean minePresent, Object mine, Object other) {
if (minePresent) {
@@ -2104,7 +2136,7 @@ public abstract class GeneratedMessageLite<
}
return other;
}
-
+
@Override
public void visitOneofNotSet(boolean minePresent) {
return;
@@ -2121,12 +2153,13 @@ public abstract class GeneratedMessageLite<
}
@Override
- public LazyFieldLite visitLazyMessage(
- boolean minePresent, LazyFieldLite mine, boolean otherPresent, LazyFieldLite other) {
- // LazyFieldLite's are never null so we can just copy across. Necessary to avoid leakage
- // from builder into immutable message.
- // TODO(dweis): Change to null sentinels?
- mine.merge(other);
+ public LazyFieldLite visitLazyMessage(LazyFieldLite mine, LazyFieldLite other) {
+ if (other != null) {
+ if (mine == null) {
+ mine = new LazyFieldLite();
+ }
+ mine.merge(other);
+ }
return mine;
}
@@ -2140,7 +2173,7 @@ public abstract class GeneratedMessageLite<
}
mine.addAll(other);
}
-
+
return size > 0 ? mine : other;
}
@@ -2154,7 +2187,7 @@ public abstract class GeneratedMessageLite<
}
mine.addAll(other);
}
-
+
return size > 0 ? mine : other;
}
@@ -2168,7 +2201,7 @@ public abstract class GeneratedMessageLite<
}
mine.addAll(other);
}
-
+
return size > 0 ? mine : other;
}
@@ -2182,7 +2215,7 @@ public abstract class GeneratedMessageLite<
}
mine.addAll(other);
}
-
+
return size > 0 ? mine : other;
}
@@ -2196,7 +2229,7 @@ public abstract class GeneratedMessageLite<
}
mine.addAll(other);
}
-
+
return size > 0 ? mine : other;
}
@@ -2210,7 +2243,7 @@ public abstract class GeneratedMessageLite<
}
mine.addAll(other);
}
-
+
return size > 0 ? mine : other;
}
@@ -2232,10 +2265,15 @@ public abstract class GeneratedMessageLite<
return other == UnknownFieldSetLite.getDefaultInstance()
? mine : UnknownFieldSetLite.mutableCopyOf(mine, other);
}
-
+
@Override
public <K, V> MapFieldLite<K, V> visitMap(MapFieldLite<K, V> mine, MapFieldLite<K, V> other) {
- mine.mergeFrom(other);
+ if (!other.isEmpty()) {
+ if (!mine.isMutable()) {
+ mine = mine.mutableCopy();
+ }
+ mine.mergeFrom(other);
+ }
return mine;
}
}