Skip to content

Commit

Permalink
Integrated internal changes from Google
Browse files Browse the repository at this point in the history
This includes all internal changes from around May 20 to now.
  • Loading branch information
acozzette committed Jun 29, 2016
1 parent c18aa77 commit d64a2d9
Show file tree
Hide file tree
Showing 216 changed files with 13,676 additions and 5,406 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ java_EXTRA_DIST=
java/core/src/main/java/com/google/protobuf/Extension.java \
java/core/src/main/java/com/google/protobuf/ExtensionLite.java \
java/core/src/main/java/com/google/protobuf/ExtensionRegistry.java \
java/core/src/main/java/com/google/protobuf/ExtensionRegistryFactory.java \
java/core/src/main/java/com/google/protobuf/ExtensionRegistryLite.java \
java/core/src/main/java/com/google/protobuf/FieldSet.java \
java/core/src/main/java/com/google/protobuf/FloatArrayList.java \
Expand Down Expand Up @@ -273,6 +274,7 @@ java_EXTRA_DIST=
java/core/src/test/java/com/google/protobuf/DoubleArrayListTest.java \
java/core/src/test/java/com/google/protobuf/DynamicMessageTest.java \
java/core/src/test/java/com/google/protobuf/EnumTest.java \
java/core/src/test/java/com/google/protobuf/ExtensionRegistryFactoryTest.java \
java/core/src/test/java/com/google/protobuf/FieldPresenceTest.java \
java/core/src/test/java/com/google/protobuf/FloatArrayListTest.java \
java/core/src/test/java/com/google/protobuf/ForceFieldBuildersPreRun.java \
Expand Down
95 changes: 94 additions & 1 deletion java/core/src/main/java/com/google/protobuf/AbstractMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,40 @@ public abstract class AbstractMessage
// TODO(dweis): Update GeneratedMessage to parameterize with MessageType and BuilderType.
extends AbstractMessageLite
implements Message {

@Override
public boolean isInitialized() {
return MessageReflection.isInitialized(this);
}

/**
* Interface for the parent of a Builder that allows the builder to
* communicate invalidations back to the parent for use when using nested
* builders.
*/
protected interface BuilderParent {

/**
* A builder becomes dirty whenever a field is modified -- including fields
* in nested builders -- and becomes clean when build() is called. Thus,
* when a builder becomes dirty, all its parents become dirty as well, and
* when it becomes clean, all its children become clean. The dirtiness
* state is used to invalidate certain cached values.
* <br>
* To this end, a builder calls markDirty() on its parent whenever it
* transitions from clean to dirty. The parent must propagate this call to
* its own parent, unless it was already dirty, in which case the
* grandparent must necessarily already be dirty as well. The parent can
* only transition back to "clean" after calling build() on all children.
*/
void markDirty();
}

/** Create a nested builder. */
protected Message.Builder newBuilderForType(BuilderParent parent) {
throw new UnsupportedOperationException("Nested builder is not supported for this type.");
}


@Override
public List<String> findInitializationErrors() {
Expand Down Expand Up @@ -460,6 +488,31 @@ public String toString() {
MessageReflection.findMissingFields(message));
}

/**
* Used to support nested builders and called to mark this builder as clean.
* Clean builders will propagate the {@link BuildParent#markDirty()} event
* to their parent builders, while dirty builders will not, as their parents
* should be dirty already.
*
* NOTE: Implementations that don't support nested builders don't need to
* override this method.
*/
void markClean() {
throw new IllegalStateException("Should be overriden by subclasses.");
}

/**
* Used to support nested builders and called when this nested builder is
* no longer used by its parent builder and should release the reference
* to its parent builder.
*
* NOTE: Implementations that don't support nested builders don't need to
* override this method.
*/
void dispose() {
throw new IllegalStateException("Should be overriden by subclasses.");
}

// ===============================================================
// The following definitions seem to be required in order to make javac
// not produce weird errors like:
Expand Down Expand Up @@ -550,4 +603,44 @@ public boolean mergeDelimitedFrom(
return super.mergeDelimitedFrom(input, extensionRegistry);
}
}

/**
* @deprecated from v3.0.0-beta-3+, for compatiblity with v2.5.0 and v2.6.1
* generated code.
*/
@Deprecated
protected static int hashLong(long n) {
return (int) (n ^ (n >>> 32));
}
//
/**
* @deprecated from v3.0.0-beta-3+, for compatiblity with v2.5.0 and v2.6.1
* generated code.
*/
@Deprecated
protected static int hashBoolean(boolean b) {
return b ? 1231 : 1237;
}
//
/**
* @deprecated from v3.0.0-beta-3+, for compatiblity with v2.5.0 and v2.6.1
* generated code.
*/
@Deprecated
protected static int hashEnum(EnumLite e) {
return e.getNumber();
}
//
/**
* @deprecated from v3.0.0-beta-3+, for compatiblity with v2.5.0 and v2.6.1
* generated code.
*/
@Deprecated
protected static int hashEnumList(List<? extends EnumLite> list) {
int hash = 1;
for (EnumLite e : list) {
hash = 31 * hash + hashEnum(e);
}
return hash;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
*/
public abstract class AbstractMessageLite<
MessageType extends AbstractMessageLite<MessageType, BuilderType>,
BuilderType extends AbstractMessageLite.Builder<MessageType, BuilderType>>
BuilderType extends AbstractMessageLite.Builder<MessageType, BuilderType>>
implements MessageLite {
protected int memoizedHashCode = 0;

@Override
public ByteString toByteString() {
try {
Expand All @@ -57,9 +57,7 @@ public ByteString toByteString() {
writeTo(out.getCodedOutput());
return out.build();
} catch (IOException e) {
throw new RuntimeException(
"Serializing to a ByteString threw an IOException (should " +
"never happen).", e);
throw new RuntimeException(getSerializingExceptionMessage("ByteString"), e);
}
}

Expand All @@ -72,9 +70,7 @@ public byte[] toByteArray() {
output.checkNoSpaceLeft();
return result;
} catch (IOException e) {
throw new RuntimeException(
"Serializing to a byte array threw an IOException " +
"(should never happen).", e);
throw new RuntimeException(getSerializingExceptionMessage("byte array"), e);
}
}

Expand Down Expand Up @@ -109,6 +105,11 @@ UninitializedMessageException newUninitializedMessageException() {
return new UninitializedMessageException(this);
}

private String getSerializingExceptionMessage(String target) {
return "Serializing " + getClass().getName() + " to a " + target
+ " threw an IOException (should never happen).";
}

protected static void checkByteStringIsUtf8(ByteString byteString)
throws IllegalArgumentException {
if (!byteString.isValidUtf8()) {
Expand All @@ -120,7 +121,7 @@ protected static <T> void addAll(final Iterable<T> values,
final Collection<? super T> list) {
Builder.addAll(values, list);
}

/**
* A partial implementation of the {@link Message.Builder} interface which
* implements as many methods of that interface as possible in terms of
Expand Down Expand Up @@ -156,9 +157,7 @@ public BuilderType mergeFrom(final ByteString data) throws InvalidProtocolBuffer
} catch (InvalidProtocolBufferException e) {
throw e;
} catch (IOException e) {
throw new RuntimeException(
"Reading from a ByteString threw an IOException (should " +
"never happen).", e);
throw new RuntimeException(getReadingExceptionMessage("ByteString"), e);
}
}

Expand All @@ -174,9 +173,7 @@ public BuilderType mergeFrom(
} catch (InvalidProtocolBufferException e) {
throw e;
} catch (IOException e) {
throw new RuntimeException(
"Reading from a ByteString threw an IOException (should " +
"never happen).", e);
throw new RuntimeException(getReadingExceptionMessage("ByteString"), e);
}
}

Expand All @@ -197,9 +194,7 @@ public BuilderType mergeFrom(final byte[] data, final int off, final int len)
} catch (InvalidProtocolBufferException e) {
throw e;
} catch (IOException e) {
throw new RuntimeException(
"Reading from a byte array threw an IOException (should " +
"never happen).", e);
throw new RuntimeException(getReadingExceptionMessage("byte array"), e);
}
}

Expand All @@ -225,9 +220,7 @@ public BuilderType mergeFrom(
} catch (InvalidProtocolBufferException e) {
throw e;
} catch (IOException e) {
throw new RuntimeException(
"Reading from a byte array threw an IOException (should " +
"never happen).", e);
throw new RuntimeException(getReadingExceptionMessage("byte array"), e);
}
}

Expand Down Expand Up @@ -321,20 +314,25 @@ public boolean mergeDelimitedFrom(final InputStream input) throws IOException {
return mergeDelimitedFrom(input,
ExtensionRegistryLite.getEmptyRegistry());
}

@Override
@SuppressWarnings("unchecked") // isInstance takes care of this
public BuilderType mergeFrom(final MessageLite other) {
if (!getDefaultInstanceForType().getClass().isInstance(other)) {
throw new IllegalArgumentException(
"mergeFrom(MessageLite) can only merge messages of the same type.");
}

return internalMergeFrom((MessageType) other);
}

protected abstract BuilderType internalMergeFrom(MessageType message);

private String getReadingExceptionMessage(String target) {
return "Reading " + getClass().getName() + " from a " + target
+ " threw an IOException (should never happen).";
}

/**
* Construct an UninitializedMessageException reporting missing fields in
* the given message.
Expand Down
Loading

0 comments on commit d64a2d9

Please sign in to comment.