Commit 110e31cb authored by Jisi Liu's avatar Jisi Liu

Merge pull request #1199 from google/google_integrate

Integrate google internal changes to master
parents b13874d5 defa25b3
...@@ -193,7 +193,6 @@ java_EXTRA_DIST= ...@@ -193,7 +193,6 @@ java_EXTRA_DIST=
java/core/src/main/java/com/google/protobuf/BlockingRpcChannel.java \ java/core/src/main/java/com/google/protobuf/BlockingRpcChannel.java \
java/core/src/main/java/com/google/protobuf/BlockingService.java \ java/core/src/main/java/com/google/protobuf/BlockingService.java \
java/core/src/main/java/com/google/protobuf/BooleanArrayList.java \ java/core/src/main/java/com/google/protobuf/BooleanArrayList.java \
java/core/src/main/java/com/google/protobuf/BoundedByteString.java \
java/core/src/main/java/com/google/protobuf/ByteString.java \ java/core/src/main/java/com/google/protobuf/ByteString.java \
java/core/src/main/java/com/google/protobuf/CodedInputStream.java \ java/core/src/main/java/com/google/protobuf/CodedInputStream.java \
java/core/src/main/java/com/google/protobuf/CodedOutputStream.java \ java/core/src/main/java/com/google/protobuf/CodedOutputStream.java \
...@@ -215,7 +214,6 @@ java_EXTRA_DIST= ...@@ -215,7 +214,6 @@ java_EXTRA_DIST=
java/core/src/main/java/com/google/protobuf/LazyFieldLite.java \ java/core/src/main/java/com/google/protobuf/LazyFieldLite.java \
java/core/src/main/java/com/google/protobuf/LazyStringArrayList.java \ java/core/src/main/java/com/google/protobuf/LazyStringArrayList.java \
java/core/src/main/java/com/google/protobuf/LazyStringList.java \ java/core/src/main/java/com/google/protobuf/LazyStringList.java \
java/core/src/main/java/com/google/protobuf/LiteralByteString.java \
java/core/src/main/java/com/google/protobuf/LongArrayList.java \ java/core/src/main/java/com/google/protobuf/LongArrayList.java \
java/core/src/main/java/com/google/protobuf/MapEntry.java \ java/core/src/main/java/com/google/protobuf/MapEntry.java \
java/core/src/main/java/com/google/protobuf/MapEntryLite.java \ java/core/src/main/java/com/google/protobuf/MapEntryLite.java \
...@@ -249,7 +247,7 @@ java_EXTRA_DIST= ...@@ -249,7 +247,7 @@ java_EXTRA_DIST=
java/core/src/main/java/com/google/protobuf/UnknownFieldSet.java \ java/core/src/main/java/com/google/protobuf/UnknownFieldSet.java \
java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java \ java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java \
java/core/src/main/java/com/google/protobuf/UnmodifiableLazyStringList.java \ java/core/src/main/java/com/google/protobuf/UnmodifiableLazyStringList.java \
java/core/src/main/java/com/google/protobuf/UnsafeByteStrings.java \ java/core/src/main/java/com/google/protobuf/UnsafeByteOperations.java \
java/core/src/main/java/com/google/protobuf/Utf8.java \ java/core/src/main/java/com/google/protobuf/Utf8.java \
java/core/src/main/java/com/google/protobuf/WireFormat.java \ java/core/src/main/java/com/google/protobuf/WireFormat.java \
java/core/src/test/java/com/google/protobuf/AbstractMessageTest.java \ java/core/src/test/java/com/google/protobuf/AbstractMessageTest.java \
......
...@@ -37,6 +37,7 @@ set(libprotoc_files ...@@ -37,6 +37,7 @@ set(libprotoc_files
${protobuf_source_dir}/src/google/protobuf/compiler/java/java_enum_field_lite.cc ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_enum_field_lite.cc
${protobuf_source_dir}/src/google/protobuf/compiler/java/java_enum_lite.cc ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_enum_lite.cc
${protobuf_source_dir}/src/google/protobuf/compiler/java/java_extension.cc ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_extension.cc
${protobuf_source_dir}/src/google/protobuf/compiler/java/java_extension_lite.cc
${protobuf_source_dir}/src/google/protobuf/compiler/java/java_field.cc ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_field.cc
${protobuf_source_dir}/src/google/protobuf/compiler/java/java_file.cc ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_file.cc
${protobuf_source_dir}/src/google/protobuf/compiler/java/java_generator.cc ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_generator.cc
......
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
# to make when building protoc. This is particularly useful for passing # to make when building protoc. This is particularly useful for passing
# -j4 to run 4 jobs simultaneously. # -j4 to run 4 jobs simultaneously.
set -e
if test ! -e src/google/protobuf/stubs/common.h; then if test ! -e src/google/protobuf/stubs/common.h; then
cat >&2 << __EOF__ cat >&2 << __EOF__
Could not find source code. Make sure you are running this script from the Could not find source code. Make sure you are running this script from the
...@@ -43,51 +45,48 @@ declare -a RUNTIME_PROTO_FILES=(\ ...@@ -43,51 +45,48 @@ declare -a RUNTIME_PROTO_FILES=(\
CORE_PROTO_IS_CORRECT=0 CORE_PROTO_IS_CORRECT=0
PROCESS_ROUND=1 PROCESS_ROUND=1
TMP=$(mktemp -d)
echo "Updating descriptor protos..." echo "Updating descriptor protos..."
while [ $CORE_PROTO_IS_CORRECT -ne 1 ] while [ $CORE_PROTO_IS_CORRECT -ne 1 ]
do do
echo "Round $PROCESS_ROUND" echo "Round $PROCESS_ROUND"
CORE_PROTO_IS_CORRECT=1 CORE_PROTO_IS_CORRECT=1
for PROTO_FILE in ${RUNTIME_PROTO_FILES[@]}; do
BASE_NAME=${PROTO_FILE%.*}
cp ${BASE_NAME}.pb.h ${BASE_NAME}.pb.h.tmp
cp ${BASE_NAME}.pb.cc ${BASE_NAME}.pb.cc.tmp
done
cp google/protobuf/compiler/plugin.pb.h google/protobuf/compiler/plugin.pb.h.tmp
cp google/protobuf/compiler/plugin.pb.cc google/protobuf/compiler/plugin.pb.cc.tmp
make $@ protoc && make $@ protoc &&
./protoc --cpp_out=dllexport_decl=LIBPROTOBUF_EXPORT:. ${RUNTIME_PROTO_FILES[@]} && \ ./protoc --cpp_out=dllexport_decl=LIBPROTOBUF_EXPORT:$TMP ${RUNTIME_PROTO_FILES[@]} && \
./protoc --cpp_out=dllexport_decl=LIBPROTOC_EXPORT:. google/protobuf/compiler/plugin.proto ./protoc --cpp_out=dllexport_decl=LIBPROTOC_EXPORT:$TMP google/protobuf/compiler/plugin.proto
for PROTO_FILE in ${RUNTIME_PROTO_FILES[@]}; do for PROTO_FILE in ${RUNTIME_PROTO_FILES[@]}; do
BASE_NAME=${PROTO_FILE%.*} BASE_NAME=${PROTO_FILE%.*}
diff ${BASE_NAME}.pb.h ${BASE_NAME}.pb.h.tmp > /dev/null diff ${BASE_NAME}.pb.h $TMP/${BASE_NAME}.pb.h > /dev/null
if test $? -ne 0; then if test $? -ne 0; then
CORE_PROTO_IS_CORRECT=0 CORE_PROTO_IS_CORRECT=0
fi fi
diff ${BASE_NAME}.pb.cc ${BASE_NAME}.pb.cc.tmp > /dev/null diff ${BASE_NAME}.pb.cc $TMP/${BASE_NAME}.pb.cc > /dev/null
if test $? -ne 0; then if test $? -ne 0; then
CORE_PROTO_IS_CORRECT=0 CORE_PROTO_IS_CORRECT=0
fi fi
done done
diff google/protobuf/compiler/plugin.pb.h google/protobuf/compiler/plugin.pb.h.tmp > /dev/null diff google/protobuf/compiler/plugin.pb.h $TMP/google/protobuf/compiler/plugin.pb.h > /dev/null
if test $? -ne 0; then if test $? -ne 0; then
CORE_PROTO_IS_CORRECT=0 CORE_PROTO_IS_CORRECT=0
fi fi
diff google/protobuf/compiler/plugin.pb.cc google/protobuf/compiler/plugin.pb.cc.tmp > /dev/null diff google/protobuf/compiler/plugin.pb.cc $TMP/google/protobuf/compiler/plugin.pb.cc > /dev/null
if test $? -ne 0; then if test $? -ne 0; then
CORE_PROTO_IS_CORRECT=0 CORE_PROTO_IS_CORRECT=0
fi fi
for PROTO_FILE in ${RUNTIME_PROTO_FILES[@]}; do # Only override the output if the files are different to avoid re-compilation
BASE_NAME=${PROTO_FILE%.*} # of the protoc.
rm ${BASE_NAME}.pb.h.tmp if [ $CORE_PROTO_IS_CORRECT -ne 1 ]; then
rm ${BASE_NAME}.pb.cc.tmp for PROTO_FILE in ${RUNTIME_PROTO_FILES[@]}; do
done BASE_NAME=${PROTO_FILE%.*}
rm google/protobuf/compiler/plugin.pb.h.tmp mv $TMP/${BASE_NAME}.pb.h ${BASE_NAME}.pb.h
rm google/protobuf/compiler/plugin.pb.cc.tmp mv $TMP/${BASE_NAME}.pb.cc ${BASE_NAME}.pb.cc
done
mv $TMP/google/protobuf/compiler/plugin.pb.* google/protobuf/compiler/
fi
PROCESS_ROUND=$((PROCESS_ROUND + 1)) PROCESS_ROUND=$((PROCESS_ROUND + 1))
done done
......
...@@ -70,7 +70,15 @@ public final class CodedInputStream { ...@@ -70,7 +70,15 @@ public final class CodedInputStream {
*/ */
public static CodedInputStream newInstance(final byte[] buf, final int off, public static CodedInputStream newInstance(final byte[] buf, final int off,
final int len) { final int len) {
CodedInputStream result = new CodedInputStream(buf, off, len); return newInstance(buf, off, len, false);
}
/**
* Create a new CodedInputStream wrapping the given byte array slice.
*/
public static CodedInputStream newInstance(final byte[] buf, final int off,
final int len, boolean bufferIsImmutable) {
CodedInputStream result = new CodedInputStream(buf, off, len, bufferIsImmutable);
try { try {
// Some uses of CodedInputStream can be more efficient if they know // Some uses of CodedInputStream can be more efficient if they know
// exactly how many bytes are available. By pushing the end point of the // exactly how many bytes are available. By pushing the end point of the
...@@ -113,31 +121,6 @@ public final class CodedInputStream { ...@@ -113,31 +121,6 @@ public final class CodedInputStream {
} }
} }
/**
* Create a new CodedInputStream wrapping a LiteralByteString.
*/
static CodedInputStream newInstance(LiteralByteString byteString) {
CodedInputStream result = new CodedInputStream(byteString);
try {
// Some uses of CodedInputStream can be more efficient if they know
// exactly how many bytes are available. By pushing the end point of the
// buffer as a limit, we allow them to get this information via
// getBytesUntilLimit(). Pushing a limit that we know is at the end of
// the stream can never hurt, since we can never past that point anyway.
result.pushLimit(byteString.size());
} catch (InvalidProtocolBufferException ex) {
// The only reason pushLimit() might throw an exception here is if len
// is negative. Normally pushLimit()'s parameter comes directly off the
// wire, so it's important to catch exceptions in case of corrupt or
// malicious data. However, in this case, we expect that len is not a
// user-supplied value, so we can assume that it being negative indicates
// a programming error. Therefore, throwing an unchecked exception is
// appropriate.
throw new IllegalArgumentException(ex);
}
return result;
}
// ----------------------------------------------------------------- // -----------------------------------------------------------------
/** /**
...@@ -506,7 +489,7 @@ public final class CodedInputStream { ...@@ -506,7 +489,7 @@ public final class CodedInputStream {
// Fast path: We already have the bytes in a contiguous buffer, so // Fast path: We already have the bytes in a contiguous buffer, so
// just copy directly from it. // just copy directly from it.
final ByteString result = bufferIsImmutable && enableAliasing final ByteString result = bufferIsImmutable && enableAliasing
? new BoundedByteString(buffer, bufferPos, size) ? ByteString.wrap(buffer, bufferPos, size)
: ByteString.copyFrom(buffer, bufferPos, size); : ByteString.copyFrom(buffer, bufferPos, size);
bufferPos += size; bufferPos += size;
return result; return result;
...@@ -514,7 +497,7 @@ public final class CodedInputStream { ...@@ -514,7 +497,7 @@ public final class CodedInputStream {
return ByteString.EMPTY; return ByteString.EMPTY;
} else { } else {
// Slow path: Build a byte array first then copy it. // Slow path: Build a byte array first then copy it.
return new LiteralByteString(readRawBytesSlowPath(size)); return ByteString.wrap(readRawBytesSlowPath(size));
} }
} }
...@@ -886,13 +869,13 @@ public final class CodedInputStream { ...@@ -886,13 +869,13 @@ public final class CodedInputStream {
private static final int DEFAULT_SIZE_LIMIT = 64 << 20; // 64MB private static final int DEFAULT_SIZE_LIMIT = 64 << 20; // 64MB
private static final int BUFFER_SIZE = 4096; private static final int BUFFER_SIZE = 4096;
private CodedInputStream(final byte[] buffer, final int off, final int len) { private CodedInputStream(final byte[] buffer, final int off, final int len, boolean bufferIsImmutable) {
this.buffer = buffer; this.buffer = buffer;
bufferSize = off + len; bufferSize = off + len;
bufferPos = off; bufferPos = off;
totalBytesRetired = -off; totalBytesRetired = -off;
input = null; input = null;
bufferIsImmutable = false; this.bufferIsImmutable = bufferIsImmutable;
} }
private CodedInputStream(final InputStream input) { private CodedInputStream(final InputStream input) {
...@@ -904,15 +887,6 @@ public final class CodedInputStream { ...@@ -904,15 +887,6 @@ public final class CodedInputStream {
bufferIsImmutable = false; bufferIsImmutable = false;
} }
private CodedInputStream(final LiteralByteString byteString) {
buffer = byteString.bytes;
bufferPos = byteString.getOffsetIntoBytes();
bufferSize = bufferPos + byteString.size();
totalBytesRetired = -bufferPos;
input = null;
bufferIsImmutable = true;
}
public void enableAliasing(boolean enabled) { public void enableAliasing(boolean enabled) {
this.enableAliasing = enabled; this.enableAliasing = enabled;
} }
......
package com.google.protobuf;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Indicates a public API that can change at any time, and has no guarantee of API stability and
* backward-compatibility.
*
* <p>Usage guidelines:
* <ol>
* <li>This annotation is used only on public API. Internal interfaces should not use it.</li>
* <li>This annotation should only be added to new APIs. Adding it to an existing API is
* considered API-breaking.</li>
* <li>Removing this annotation from an API gives it stable status.</li>
* </ol>
*/
@Retention(RetentionPolicy.SOURCE)
@Target({
ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR,
ElementType.FIELD,
ElementType.METHOD,
ElementType.PACKAGE,
ElementType.TYPE})
@Documented
public @interface ExperimentalApi {
/**
* Context information such as links to discussion thread, tracking issue etc.
*/
String value() default "";
}
...@@ -283,9 +283,31 @@ public class Internal { ...@@ -283,9 +283,31 @@ public class Internal {
// ByteString with the same content. This is to ensure that the generated // ByteString with the same content. This is to ensure that the generated
// hashCode() method will return the same value as the pure reflection // hashCode() method will return the same value as the pure reflection
// based hashCode() method. // based hashCode() method.
return LiteralByteString.hashCode(bytes); return Internal.hashCode(bytes, 0, bytes.length);
}
/**
* Helper method for implementing {@link LiteralByteString#hashCode()}.
*/
static int hashCode(byte[] bytes, int offset, int length) {
// The hash code for a byte array should be the same as the hash code for a
// ByteString with the same content. This is to ensure that the generated
// hashCode() method will return the same value as the pure reflection
// based hashCode() method.
int h = Internal.partialHash(length, bytes, offset, length);
return h == 0 ? 1 : h;
} }
/**
* Helper method for continuously hashing bytes.
*/
static int partialHash(int h, byte[] bytes, int offset, int length) {
for (int i = offset; i < offset + length; i++) {
h = h * 31 + bytes[i];
}
return h;
}
/** /**
* Helper method for implementing {@link Message#equals(Object)} for bytes * Helper method for implementing {@link Message#equals(Object)} for bytes
* field. * field.
...@@ -337,8 +359,7 @@ public class Internal { ...@@ -337,8 +359,7 @@ public class Internal {
public static int hashCodeByteBuffer(ByteBuffer bytes) { public static int hashCodeByteBuffer(ByteBuffer bytes) {
if (bytes.hasArray()) { if (bytes.hasArray()) {
// Fast path. // Fast path.
int h = LiteralByteString.hashCode(bytes.capacity(), bytes.array(), int h = partialHash(bytes.capacity(), bytes.array(), bytes.arrayOffset(), bytes.capacity());
bytes.arrayOffset(), bytes.capacity());
return h == 0 ? 1 : h; return h == 0 ? 1 : h;
} else { } else {
// Read the data into a temporary byte array before calculating the // Read the data into a temporary byte array before calculating the
...@@ -353,7 +374,7 @@ public class Internal { ...@@ -353,7 +374,7 @@ public class Internal {
final int length = duplicated.remaining() <= bufferSize ? final int length = duplicated.remaining() <= bufferSize ?
duplicated.remaining() : bufferSize; duplicated.remaining() : bufferSize;
duplicated.get(buffer, 0, length); duplicated.get(buffer, 0, length);
h = LiteralByteString.hashCode(h, buffer, 0, length); h = partialHash(h, buffer, 0, length);
} }
return h == 0 ? 1 : h; return h == 0 ? 1 : h;
} }
...@@ -385,7 +406,6 @@ public class Internal { ...@@ -385,7 +406,6 @@ public class Internal {
public static final CodedInputStream EMPTY_CODED_INPUT_STREAM = public static final CodedInputStream EMPTY_CODED_INPUT_STREAM =
CodedInputStream.newInstance(EMPTY_BYTE_ARRAY); CodedInputStream.newInstance(EMPTY_BYTE_ARRAY);
/** /**
* Provides an immutable view of {@code List<T>} around a {@code List<F>}. * Provides an immutable view of {@code List<T>} around a {@code List<F>}.
* *
......
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.google.protobuf;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.List;
/**
* This class implements a {@link com.google.protobuf.ByteString} backed by a
* single array of bytes, contiguous in memory. It supports substring by
* pointing to only a sub-range of the underlying byte array, meaning that a
* substring will reference the full byte-array of the string it's made from,
* exactly as with {@link String}.
*
* @author carlanton@google.com (Carl Haverl)
*/
class LiteralByteString extends ByteString.LeafByteString {
private static final long serialVersionUID = 1L;
protected final byte[] bytes;
/**
* Creates a {@code LiteralByteString} backed by the given array, without
* copying.
*
* @param bytes array to wrap
*/
LiteralByteString(byte[] bytes) {
this.bytes = bytes;
}
@Override
public byte byteAt(int index) {
// Unlike most methods in this class, this one is a direct implementation
// ignoring the potential offset because we need to do range-checking in the
// substring case anyway.
return bytes[index];
}
@Override
public int size() {
return bytes.length;
}
// =================================================================
// ByteString -> substring
@Override
public final ByteString substring(int beginIndex, int endIndex) {
final int length = checkRange(beginIndex, endIndex, size());
if (length == 0) {
return ByteString.EMPTY;
}
return new BoundedByteString(bytes, getOffsetIntoBytes() + beginIndex, length);
}
// =================================================================
// ByteString -> byte[]
@Override
protected void copyToInternal(
byte[] target, int sourceOffset, int targetOffset, int numberToCopy) {
// Optimized form, not for subclasses, since we don't call
// getOffsetIntoBytes() or check the 'numberToCopy' parameter.
// TODO(nathanmittler): Is not calling getOffsetIntoBytes really saving that much?
System.arraycopy(bytes, sourceOffset, target, targetOffset, numberToCopy);
}
@Override
public final void copyTo(ByteBuffer target) {
target.put(bytes, getOffsetIntoBytes(), size()); // Copies bytes
}
@Override
public final ByteBuffer asReadOnlyByteBuffer() {
return ByteBuffer.wrap(bytes, getOffsetIntoBytes(), size()).asReadOnlyBuffer();
}
@Override
public final List<ByteBuffer> asReadOnlyByteBufferList() {
return Collections.singletonList(asReadOnlyByteBuffer());
}
@Override
public final void writeTo(OutputStream outputStream) throws IOException {
outputStream.write(toByteArray());
}
@Override
final void writeToInternal(OutputStream outputStream, int sourceOffset, int numberToWrite)
throws IOException {
outputStream.write(bytes, getOffsetIntoBytes() + sourceOffset, numberToWrite);
}
@Override
protected final String toStringInternal(Charset charset) {
return new String(bytes, getOffsetIntoBytes(), size(), charset);
}
// =================================================================
// UTF-8 decoding
@Override
public final boolean isValidUtf8() {
int offset = getOffsetIntoBytes();
return Utf8.isValidUtf8(bytes, offset, offset + size());
}
@Override
protected final int partialIsValidUtf8(int state, int offset, int length) {
int index = getOffsetIntoBytes() + offset;
return Utf8.partialIsValidUtf8(state, bytes, index, index + length);
}
// =================================================================
// equals() and hashCode()
@Override
public final boolean equals(Object other) {
if (other == this) {
return true;
}
if (!(other instanceof ByteString)) {
return false;
}
if (size() != ((ByteString) other).size()) {
return false;
}
if (size() == 0) {
return true;
}
if (other instanceof LiteralByteString) {
LiteralByteString otherAsLiteral = (LiteralByteString) other;
// If we know the hash codes and they are not equal, we know the byte
// strings are not equal.
int thisHash = peekCachedHashCode();
int thatHash = otherAsLiteral.peekCachedHashCode();
if (thisHash != 0 && thatHash != 0 && thisHash != thatHash) {
return false;
}
return equalsRange((LiteralByteString) other, 0, size());
} else {
// RopeByteString and NioByteString.
return other.equals(this);
}
}
/**
* Check equality of the substring of given length of this object starting at
* zero with another {@code LiteralByteString} substring starting at offset.
*
* @param other what to compare a substring in
* @param offset offset into other
* @param length number of bytes to compare
* @return true for equality of substrings, else false.
*/
@Override
final boolean equalsRange(ByteString other, int offset, int length) {
if (length > other.size()) {
throw new IllegalArgumentException("Length too large: " + length + size());
}
if (offset + length > other.size()) {
throw new IllegalArgumentException(
"Ran off end of other: " + offset + ", " + length + ", " + other.size());
}
if (other instanceof LiteralByteString) {
LiteralByteString lbsOther = (LiteralByteString) other;
byte[] thisBytes = bytes;
byte[] otherBytes = lbsOther.bytes;
int thisLimit = getOffsetIntoBytes() + length;
for (
int thisIndex = getOffsetIntoBytes(), otherIndex = lbsOther.getOffsetIntoBytes() + offset;
(thisIndex < thisLimit); ++thisIndex, ++otherIndex) {
if (thisBytes[thisIndex] != otherBytes[otherIndex]) {
return false;
}
}
return true;
}
return other.substring(offset, offset + length).equals(substring(0, length));
}
@Override
protected final int partialHash(int h, int offset, int length) {
return hashCode(h, bytes, getOffsetIntoBytes() + offset, length);
}
static int hashCode(int h, byte[] bytes, int offset, int length) {
for (int i = offset; i < offset + length; i++) {
h = h * 31 + bytes[i];
}
return h;
}
static int hashCode(byte[] bytes) {
int h = hashCode(bytes.length, bytes, 0, bytes.length);
return h == 0 ? 1 : h;
}
// =================================================================
// Input stream
@Override
public final InputStream newInput() {
return new ByteArrayInputStream(bytes, getOffsetIntoBytes(), size()); // No copy
}
@Override
public final CodedInputStream newCodedInput() {
// We trust CodedInputStream not to modify the bytes, or to give anyone
// else access to them.
return CodedInputStream.newInstance(this);
}
// =================================================================
// Internal methods
/**
* Offset into {@code bytes[]} to use, non-zero for substrings.
*
* @return always 0 for this class
*/
protected int getOffsetIntoBytes() {
return 0;
}
}
...@@ -136,7 +136,7 @@ public final class MapFieldLite<K, V> implements MutabilityOracle { ...@@ -136,7 +136,7 @@ public final class MapFieldLite<K, V> implements MutabilityOracle {
private static int calculateHashCodeForObject(Object a) { private static int calculateHashCodeForObject(Object a) {
if (a instanceof byte[]) { if (a instanceof byte[]) {
return LiteralByteString.hashCode((byte[]) a); return Internal.hashCode((byte[]) a);
} }
// Enums should be stored as integers internally. // Enums should be stored as integers internally.
if (a instanceof EnumLite) { if (a instanceof EnumLite) {
......
...@@ -213,14 +213,14 @@ final class RopeByteString extends ByteString { ...@@ -213,14 +213,14 @@ final class RopeByteString extends ByteString {
* @param right string on the right * @param right string on the right
* @return string formed by copying data bytes * @return string formed by copying data bytes
*/ */
private static LiteralByteString concatenateBytes(ByteString left, private static ByteString concatenateBytes(ByteString left,
ByteString right) { ByteString right) {
int leftSize = left.size(); int leftSize = left.size();
int rightSize = right.size(); int rightSize = right.size();
byte[] bytes = new byte[leftSize + rightSize]; byte[] bytes = new byte[leftSize + rightSize];
left.copyTo(bytes, 0, 0, leftSize); left.copyTo(bytes, 0, 0, leftSize);
right.copyTo(bytes, 0, leftSize, rightSize); right.copyTo(bytes, 0, leftSize, rightSize);
return new LiteralByteString(bytes); // Constructor wraps bytes return ByteString.wrap(bytes); // Constructor wraps bytes
} }
/** /**
...@@ -735,7 +735,7 @@ final class RopeByteString extends ByteString { ...@@ -735,7 +735,7 @@ final class RopeByteString extends ByteString {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
Object writeReplace() { Object writeReplace() {
return new LiteralByteString(toByteArray()); return ByteString.wrap(toByteArray());
} }
private void readObject(@SuppressWarnings("unused") ObjectInputStream in) throws IOException { private void readObject(@SuppressWarnings("unused") ObjectInputStream in) throws IOException {
......
...@@ -33,15 +33,18 @@ package com.google.protobuf; ...@@ -33,15 +33,18 @@ package com.google.protobuf;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
/** /**
* Provides unsafe factory methods for {@link ByteString} instances. * Provides a number of unsafe byte operations to be used by advanced applications with high
* performance requirements. These methods are referred to as "unsafe" due to the fact that they
* potentially expose the backing buffer of a {@link ByteString} to the application.
* *
* <p><strong>DISCLAIMER:</strong> The methods in this class should only be called if it is * <p><strong>DISCLAIMER:</strong> The methods in this class should only be called if it is
* guaranteed that the the buffer backing the {@link ByteString} will never change! Mutation of a * guaranteed that the the buffer backing the {@link ByteString} will never change! Mutation of a
* {@link ByteString} can lead to unexpected and undesirable consequences in your application, * {@link ByteString} can lead to unexpected and undesirable consequences in your application,
* and will likely be difficult to debug. Proceed with caution! * and will likely be difficult to debug. Proceed with caution!
*/ */
public final class UnsafeByteStrings { @ExperimentalApi
private UnsafeByteStrings() {} public final class UnsafeByteOperations {
private UnsafeByteOperations() {}
/** /**
* An unsafe operation that returns a {@link ByteString} that is backed by the provided buffer. * An unsafe operation that returns a {@link ByteString} that is backed by the provided buffer.
...@@ -50,6 +53,11 @@ public final class UnsafeByteStrings { ...@@ -50,6 +53,11 @@ public final class UnsafeByteStrings {
* @return a {@link ByteString} backed by the provided buffer. * @return a {@link ByteString} backed by the provided buffer.
*/ */
public static ByteString unsafeWrap(ByteBuffer buffer) { public static ByteString unsafeWrap(ByteBuffer buffer) {
return new NioByteString(buffer); if (buffer.hasArray()) {
final int offset = buffer.arrayOffset();
return ByteString.wrap(buffer.array(), offset + buffer.position(), buffer.remaining());
} else {
return new NioByteString(buffer);
}
} }
} }
...@@ -62,7 +62,7 @@ public class BoundedByteStringTest extends LiteralByteStringTest { ...@@ -62,7 +62,7 @@ public class BoundedByteStringTest extends LiteralByteStringTest {
@Override @Override
public void testToString() throws UnsupportedEncodingException { public void testToString() throws UnsupportedEncodingException {
String testString = "I love unicode \u1234\u5678 characters"; String testString = "I love unicode \u1234\u5678 characters";
LiteralByteString unicode = new LiteralByteString(testString.getBytes(Internal.UTF_8)); ByteString unicode = ByteString.wrap(testString.getBytes(Internal.UTF_8));
ByteString chopped = unicode.substring(2, unicode.size() - 6); ByteString chopped = unicode.substring(2, unicode.size() - 6);
assertEquals(classUnderTest + ".substring() must have the expected type", assertEquals(classUnderTest + ".substring() must have the expected type",
classUnderTest, getActualClassName(chopped)); classUnderTest, getActualClassName(chopped));
...@@ -75,7 +75,7 @@ public class BoundedByteStringTest extends LiteralByteStringTest { ...@@ -75,7 +75,7 @@ public class BoundedByteStringTest extends LiteralByteStringTest {
@Override @Override
public void testCharsetToString() { public void testCharsetToString() {
String testString = "I love unicode \u1234\u5678 characters"; String testString = "I love unicode \u1234\u5678 characters";
LiteralByteString unicode = new LiteralByteString(testString.getBytes(Internal.UTF_8)); ByteString unicode = ByteString.wrap(testString.getBytes(Internal.UTF_8));
ByteString chopped = unicode.substring(2, unicode.size() - 6); ByteString chopped = unicode.substring(2, unicode.size() - 6);
assertEquals(classUnderTest + ".substring() must have the expected type", assertEquals(classUnderTest + ".substring() must have the expected type",
classUnderTest, getActualClassName(chopped)); classUnderTest, getActualClassName(chopped));
......
...@@ -655,11 +655,11 @@ public class ByteStringTest extends TestCase { ...@@ -655,11 +655,11 @@ public class ByteStringTest extends TestCase {
// trees of empty leaves, to make a string that will fail this test. // trees of empty leaves, to make a string that will fail this test.
for (int i = 1; i < duo.size(); ++i) { for (int i = 1; i < duo.size(); ++i) {
assertTrue("Substrings of size() < 2 must not be RopeByteStrings", assertTrue("Substrings of size() < 2 must not be RopeByteStrings",
duo.substring(i - 1, i) instanceof LiteralByteString); duo.substring(i - 1, i) instanceof ByteString.LeafByteString);
} }
for (int i = 1; i < quintet.size(); ++i) { for (int i = 1; i < quintet.size(); ++i) {
assertTrue("Substrings of size() < 2 must not be RopeByteStrings", assertTrue("Substrings of size() < 2 must not be RopeByteStrings",
quintet.substring(i - 1, i) instanceof LiteralByteString); quintet.substring(i - 1, i) instanceof ByteString.LeafByteString);
} }
} }
...@@ -724,7 +724,7 @@ public class ByteStringTest extends TestCase { ...@@ -724,7 +724,7 @@ public class ByteStringTest extends TestCase {
} }
data1[1] = (byte) 11; data1[1] = (byte) 11;
// Test LiteralByteString.writeTo(OutputStream,int,int) // Test LiteralByteString.writeTo(OutputStream,int,int)
LiteralByteString left = new LiteralByteString(data1); ByteString left = ByteString.wrap(data1);
byte[] result = substringUsingWriteTo(left, 1, 1); byte[] result = substringUsingWriteTo(left, 1, 1);
assertEquals(1, result.length); assertEquals(1, result.length);
assertEquals((byte) 11, result[0]); assertEquals((byte) 11, result[0]);
...@@ -733,7 +733,7 @@ public class ByteStringTest extends TestCase { ...@@ -733,7 +733,7 @@ public class ByteStringTest extends TestCase {
for (int i = 0; i < data1.length; i++) { for (int i = 0; i < data1.length; i++) {
data2[i] = (byte) 2; data2[i] = (byte) 2;
} }
LiteralByteString right = new LiteralByteString(data2); ByteString right = ByteString.wrap(data2);
// Concatenate two ByteStrings to create a RopeByteString. // Concatenate two ByteStrings to create a RopeByteString.
ByteString root = left.concat(right); ByteString root = left.concat(right);
// Make sure we are actually testing a RopeByteString with a simple tree // Make sure we are actually testing a RopeByteString with a simple tree
......
...@@ -75,9 +75,7 @@ public class LiteralByteStringTest extends TestCase { ...@@ -75,9 +75,7 @@ public class LiteralByteStringTest extends TestCase {
} }
protected String getActualClassName(Object object) { protected String getActualClassName(Object object) {
String actualClassName = object.getClass().getName(); return object.getClass().getSimpleName();
actualClassName = actualClassName.substring(actualClassName.lastIndexOf('.') + 1);
return actualClassName;
} }
public void testByteAt() { public void testByteAt() {
...@@ -350,14 +348,14 @@ public class LiteralByteStringTest extends TestCase { ...@@ -350,14 +348,14 @@ public class LiteralByteStringTest extends TestCase {
public void testToString() throws UnsupportedEncodingException { public void testToString() throws UnsupportedEncodingException {
String testString = "I love unicode \u1234\u5678 characters"; String testString = "I love unicode \u1234\u5678 characters";
LiteralByteString unicode = new LiteralByteString(testString.getBytes(Internal.UTF_8)); ByteString unicode = ByteString.wrap(testString.getBytes(Internal.UTF_8));
String roundTripString = unicode.toString(UTF_8); String roundTripString = unicode.toString(UTF_8);
assertEquals(classUnderTest + " unicode must match", testString, roundTripString); assertEquals(classUnderTest + " unicode must match", testString, roundTripString);
} }
public void testCharsetToString() { public void testCharsetToString() {
String testString = "I love unicode \u1234\u5678 characters"; String testString = "I love unicode \u1234\u5678 characters";
LiteralByteString unicode = new LiteralByteString(testString.getBytes(Internal.UTF_8)); ByteString unicode = ByteString.wrap(testString.getBytes(Internal.UTF_8));
String roundTripString = unicode.toString(Internal.UTF_8); String roundTripString = unicode.toString(Internal.UTF_8);
assertEquals(classUnderTest + " unicode must match", testString, roundTripString); assertEquals(classUnderTest + " unicode must match", testString, roundTripString);
} }
...@@ -365,7 +363,7 @@ public class LiteralByteStringTest extends TestCase { ...@@ -365,7 +363,7 @@ public class LiteralByteStringTest extends TestCase {
public void testToString_returnsCanonicalEmptyString() { public void testToString_returnsCanonicalEmptyString() {
assertSame(classUnderTest + " must be the same string references", assertSame(classUnderTest + " must be the same string references",
ByteString.EMPTY.toString(Internal.UTF_8), ByteString.EMPTY.toString(Internal.UTF_8),
new LiteralByteString(new byte[]{}).toString(Internal.UTF_8)); ByteString.wrap(new byte[]{}).toString(Internal.UTF_8));
} }
public void testToString_raisesException() { public void testToString_raisesException() {
...@@ -377,7 +375,7 @@ public class LiteralByteStringTest extends TestCase { ...@@ -377,7 +375,7 @@ public class LiteralByteStringTest extends TestCase {
} }
try { try {
new LiteralByteString(referenceBytes).toString("invalid"); ByteString.wrap(referenceBytes).toString("invalid");
fail("Should have thrown an exception."); fail("Should have thrown an exception.");
} catch (UnsupportedEncodingException expected) { } catch (UnsupportedEncodingException expected) {
// This is success // This is success
...@@ -390,15 +388,15 @@ public class LiteralByteStringTest extends TestCase { ...@@ -390,15 +388,15 @@ public class LiteralByteStringTest extends TestCase {
assertFalse(classUnderTest + " must not equal the empty string", assertFalse(classUnderTest + " must not equal the empty string",
stringUnderTest.equals(ByteString.EMPTY)); stringUnderTest.equals(ByteString.EMPTY));
assertEquals(classUnderTest + " empty strings must be equal", assertEquals(classUnderTest + " empty strings must be equal",
new LiteralByteString(new byte[]{}), stringUnderTest.substring(55, 55)); ByteString.wrap(new byte[]{}), stringUnderTest.substring(55, 55));
assertEquals(classUnderTest + " must equal another string with the same value", assertEquals(classUnderTest + " must equal another string with the same value",
stringUnderTest, new LiteralByteString(referenceBytes)); stringUnderTest, ByteString.wrap(referenceBytes));
byte[] mungedBytes = new byte[referenceBytes.length]; byte[] mungedBytes = new byte[referenceBytes.length];
System.arraycopy(referenceBytes, 0, mungedBytes, 0, referenceBytes.length); System.arraycopy(referenceBytes, 0, mungedBytes, 0, referenceBytes.length);
mungedBytes[mungedBytes.length - 5] = (byte) (mungedBytes[mungedBytes.length - 5] ^ 0xFF); mungedBytes[mungedBytes.length - 5] = (byte) (mungedBytes[mungedBytes.length - 5] ^ 0xFF);
assertFalse(classUnderTest + " must not equal every string with the same length", assertFalse(classUnderTest + " must not equal every string with the same length",
stringUnderTest.equals(new LiteralByteString(mungedBytes))); stringUnderTest.equals(ByteString.wrap(mungedBytes)));
} }
public void testHashCode() { public void testHashCode() {
......
...@@ -52,13 +52,12 @@ import java.util.NoSuchElementException; ...@@ -52,13 +52,12 @@ import java.util.NoSuchElementException;
* Tests for {@link NioByteString}. * Tests for {@link NioByteString}.
*/ */
public class NioByteStringTest extends TestCase { public class NioByteStringTest extends TestCase {
private static final ByteString EMPTY = UnsafeByteStrings.unsafeWrap( private static final ByteString EMPTY = new NioByteString(ByteBuffer.wrap(new byte[0]));
ByteBuffer.wrap(new byte[0]));
private static final String CLASSNAME = NioByteString.class.getSimpleName(); private static final String CLASSNAME = NioByteString.class.getSimpleName();
private static final byte[] BYTES = ByteStringTest.getTestBytes(1234, 11337766L); private static final byte[] BYTES = ByteStringTest.getTestBytes(1234, 11337766L);
private static final int EXPECTED_HASH = new LiteralByteString(BYTES).hashCode(); private static final int EXPECTED_HASH = ByteString.wrap(BYTES).hashCode();
private static final ByteBuffer BUFFER = ByteBuffer.wrap(BYTES.clone()); private static final ByteBuffer BUFFER = ByteBuffer.wrap(BYTES.clone());
private static final ByteString TEST_STRING = UnsafeByteStrings.unsafeWrap(BUFFER); private static final ByteString TEST_STRING = new NioByteString(BUFFER);
public void testExpectedType() { public void testExpectedType() {
String actualClassName = getActualClassName(TEST_STRING); String actualClassName = getActualClassName(TEST_STRING);
...@@ -362,7 +361,7 @@ public class NioByteStringTest extends TestCase { ...@@ -362,7 +361,7 @@ public class NioByteStringTest extends TestCase {
public void testToString_returnsCanonicalEmptyString() { public void testToString_returnsCanonicalEmptyString() {
assertSame(CLASSNAME + " must be the same string references", assertSame(CLASSNAME + " must be the same string references",
EMPTY.toString(UTF_8), EMPTY.toString(UTF_8),
UnsafeByteStrings.unsafeWrap(ByteBuffer.wrap(new byte[0])).toString(UTF_8)); new NioByteString(ByteBuffer.wrap(new byte[0])).toString(UTF_8));
} }
public void testToString_raisesException() { public void testToString_raisesException() {
...@@ -389,11 +388,11 @@ public class NioByteStringTest extends TestCase { ...@@ -389,11 +388,11 @@ public class NioByteStringTest extends TestCase {
assertEquals(CLASSNAME + " empty strings must be equal", assertEquals(CLASSNAME + " empty strings must be equal",
EMPTY, TEST_STRING.substring(55, 55)); EMPTY, TEST_STRING.substring(55, 55));
assertEquals(CLASSNAME + " must equal another string with the same value", assertEquals(CLASSNAME + " must equal another string with the same value",
TEST_STRING, UnsafeByteStrings.unsafeWrap(BUFFER)); TEST_STRING, new NioByteString(BUFFER));
byte[] mungedBytes = mungedBytes(); byte[] mungedBytes = mungedBytes();
assertFalse(CLASSNAME + " must not equal every string with the same length", assertFalse(CLASSNAME + " must not equal every string with the same length",
TEST_STRING.equals(UnsafeByteStrings.unsafeWrap(ByteBuffer.wrap(mungedBytes)))); TEST_STRING.equals(new NioByteString(ByteBuffer.wrap(mungedBytes))));
} }
public void testEqualsLiteralByteString() { public void testEqualsLiteralByteString() {
...@@ -451,7 +450,7 @@ public class NioByteStringTest extends TestCase { ...@@ -451,7 +450,7 @@ public class NioByteStringTest extends TestCase {
} }
public void testPeekCachedHashCode() { public void testPeekCachedHashCode() {
ByteString newString = UnsafeByteStrings.unsafeWrap(BUFFER); ByteString newString = new NioByteString(BUFFER);
assertEquals(CLASSNAME + ".peekCachedHashCode() should return zero at first", 0, assertEquals(CLASSNAME + ".peekCachedHashCode() should return zero at first", 0,
newString.peekCachedHashCode()); newString.peekCachedHashCode());
newString.hashCode(); newString.hashCode();
...@@ -541,6 +540,6 @@ public class NioByteStringTest extends TestCase { ...@@ -541,6 +540,6 @@ public class NioByteStringTest extends TestCase {
} }
private static ByteString forString(String str) { private static ByteString forString(String str) {
return UnsafeByteStrings.unsafeWrap(ByteBuffer.wrap(str.getBytes(UTF_8))); return new NioByteString(ByteBuffer.wrap(str.getBytes(UTF_8)));
} }
} }
...@@ -84,7 +84,6 @@ ...@@ -84,7 +84,6 @@
<include>**/AbstractMessageLite.java</include> <include>**/AbstractMessageLite.java</include>
<include>**/AbstractParser.java</include> <include>**/AbstractParser.java</include>
<include>**/AbstractProtobufList.java</include> <include>**/AbstractProtobufList.java</include>
<include>**/BoundedByteString.java</include>
<include>**/BooleanArrayList.java</include> <include>**/BooleanArrayList.java</include>
<include>**/ByteString.java</include> <include>**/ByteString.java</include>
<include>**/CodedInputStream.java</include> <include>**/CodedInputStream.java</include>
...@@ -101,7 +100,6 @@ ...@@ -101,7 +100,6 @@
<include>**/LazyFieldLite.java</include> <include>**/LazyFieldLite.java</include>
<include>**/LazyStringArrayList.java</include> <include>**/LazyStringArrayList.java</include>
<include>**/LazyStringList.java</include> <include>**/LazyStringList.java</include>
<include>**/LiteralByteString.java</include>
<include>**/LongArrayList.java</include> <include>**/LongArrayList.java</include>
<include>**/MapEntryLite.java</include> <include>**/MapEntryLite.java</include>
<include>**/MapFieldLite.java</include> <include>**/MapFieldLite.java</include>
...@@ -119,7 +117,7 @@ ...@@ -119,7 +117,7 @@
<include>**/UninitializedMessageException.java</include> <include>**/UninitializedMessageException.java</include>
<include>**/UnknownFieldSetLite.java</include> <include>**/UnknownFieldSetLite.java</include>
<include>**/UnmodifiableLazyStringList.java</include> <include>**/UnmodifiableLazyStringList.java</include>
<include>**/UnsafeByteStrings.java</include> <include>**/UnsafeByteOperations.java</include>
<include>**/Utf8.java</include> <include>**/Utf8.java</include>
<include>**/WireFormat.java</include> <include>**/WireFormat.java</include>
</includes> </includes>
......
...@@ -1153,6 +1153,58 @@ typedef GPB_ENUM(GPBSourceCodeInfo_Location_FieldNumber) { ...@@ -1153,6 +1153,58 @@ typedef GPB_ENUM(GPBSourceCodeInfo_Location_FieldNumber) {
@end @end
#pragma mark - GPBGeneratedCodeInfo
typedef GPB_ENUM(GPBGeneratedCodeInfo_FieldNumber) {
GPBGeneratedCodeInfo_FieldNumber_AnnotationArray = 1,
};
// Describes the relationship between generated code and its original source
// file. A GeneratedCodeInfo message is associated with only one generated
// source file, but may contain references to different source .proto files.
@interface GPBGeneratedCodeInfo : GPBMessage
// An Annotation connects some span of text in generated code to an element
// of its generating .proto file.
// |annotationArray| contains |GPBGeneratedCodeInfo_Annotation|
@property(nonatomic, readwrite, strong, null_resettable) NSMutableArray *annotationArray;
@property(nonatomic, readonly) NSUInteger annotationArray_Count;
@end
#pragma mark - GPBGeneratedCodeInfo_Annotation
typedef GPB_ENUM(GPBGeneratedCodeInfo_Annotation_FieldNumber) {
GPBGeneratedCodeInfo_Annotation_FieldNumber_PathArray = 1,
GPBGeneratedCodeInfo_Annotation_FieldNumber_SourceFile = 2,
GPBGeneratedCodeInfo_Annotation_FieldNumber_Begin = 3,
GPBGeneratedCodeInfo_Annotation_FieldNumber_End = 4,
};
@interface GPBGeneratedCodeInfo_Annotation : GPBMessage
// Identifies the element in the original source .proto file. This field
// is formatted the same as SourceCodeInfo.Location.path.
@property(nonatomic, readwrite, strong, null_resettable) GPBInt32Array *pathArray;
@property(nonatomic, readonly) NSUInteger pathArray_Count;
// Identifies the filesystem path to the original source .proto.
@property(nonatomic, readwrite) BOOL hasSourceFile;
@property(nonatomic, readwrite, copy, null_resettable) NSString *sourceFile;
// Identifies the starting offset in bytes in the generated code
// that relates to the identified object.
@property(nonatomic, readwrite) BOOL hasBegin;
@property(nonatomic, readwrite) int32_t begin;
// Identifies the ending offset in bytes in the generated code that
// relates to the identified offset. The end offset should be one past
// the last relevant byte (so the length of the text = end - begin).
@property(nonatomic, readwrite) BOOL hasEnd;
@property(nonatomic, readwrite) int32_t end;
@end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END
CF_EXTERN_C_END CF_EXTERN_C_END
......
...@@ -1434,7 +1434,11 @@ typedef struct GPBFileOptions__storage_ { ...@@ -1434,7 +1434,11 @@ typedef struct GPBFileOptions__storage_ {
.offset = offsetof(GPBFileOptions__storage_, javananoUseDeprecatedPackage), .offset = offsetof(GPBFileOptions__storage_, javananoUseDeprecatedPackage),
.defaultValue.valueBool = NO, .defaultValue.valueBool = NO,
.dataTypeSpecific.className = NULL, .dataTypeSpecific.className = NULL,
#if GPBOBJC_INCLUDE_FIELD_OPTIONS
.fieldOptions = "\000\000\000\002\030\001",
#else
.fieldOptions = NULL, .fieldOptions = NULL,
#endif // GPBOBJC_INCLUDE_FIELD_OPTIONS
}, },
{ {
.name = "uninterpretedOptionArray", .name = "uninterpretedOptionArray",
...@@ -2441,5 +2445,150 @@ typedef struct GPBSourceCodeInfo_Location__storage_ { ...@@ -2441,5 +2445,150 @@ typedef struct GPBSourceCodeInfo_Location__storage_ {
@end @end
#pragma mark - GPBGeneratedCodeInfo
@implementation GPBGeneratedCodeInfo
@dynamic annotationArray, annotationArray_Count;
typedef struct GPBGeneratedCodeInfo__storage_ {
uint32_t _has_storage_[1];
NSMutableArray *annotationArray;
} GPBGeneratedCodeInfo__storage_;
// This method is threadsafe because it is initially called
// in +initialize for each subclass.
+ (GPBDescriptor *)descriptor {
static GPBDescriptor *descriptor = nil;
if (!descriptor) {
static GPBMessageFieldDescription fields[] = {
{
.name = "annotationArray",
.number = GPBGeneratedCodeInfo_FieldNumber_AnnotationArray,
.hasIndex = GPBNoHasBit,
.flags = GPBFieldRepeated,
.dataType = GPBDataTypeMessage,
.offset = offsetof(GPBGeneratedCodeInfo__storage_, annotationArray),
.defaultValue.valueMessage = nil,
.dataTypeSpecific.className = GPBStringifySymbol(GPBGeneratedCodeInfo_Annotation),
.fieldOptions = NULL,
},
};
GPBDescriptor *localDescriptor =
[GPBDescriptor allocDescriptorForClass:[GPBGeneratedCodeInfo class]
rootClass:[GPBDescriptorRoot class]
file:GPBDescriptorRoot_FileDescriptor()
fields:fields
fieldCount:sizeof(fields) / sizeof(GPBMessageFieldDescription)
oneofs:NULL
oneofCount:0
enums:NULL
enumCount:0
ranges:NULL
rangeCount:0
storageSize:sizeof(GPBGeneratedCodeInfo__storage_)
wireFormat:NO];
NSAssert(descriptor == nil, @"Startup recursed!");
descriptor = localDescriptor;
}
return descriptor;
}
@end
#pragma mark - GPBGeneratedCodeInfo_Annotation
@implementation GPBGeneratedCodeInfo_Annotation
@dynamic pathArray, pathArray_Count;
@dynamic hasSourceFile, sourceFile;
@dynamic hasBegin, begin;
@dynamic hasEnd, end;
typedef struct GPBGeneratedCodeInfo_Annotation__storage_ {
uint32_t _has_storage_[1];
int32_t begin;
int32_t end;
GPBInt32Array *pathArray;
NSString *sourceFile;
} GPBGeneratedCodeInfo_Annotation__storage_;
// This method is threadsafe because it is initially called
// in +initialize for each subclass.
+ (GPBDescriptor *)descriptor {
static GPBDescriptor *descriptor = nil;
if (!descriptor) {
static GPBMessageFieldDescription fields[] = {
{
.name = "pathArray",
.number = GPBGeneratedCodeInfo_Annotation_FieldNumber_PathArray,
.hasIndex = GPBNoHasBit,
.flags = GPBFieldRepeated | GPBFieldPacked,
.dataType = GPBDataTypeInt32,
.offset = offsetof(GPBGeneratedCodeInfo_Annotation__storage_, pathArray),
.defaultValue.valueMessage = nil,
.dataTypeSpecific.className = NULL,
#if GPBOBJC_INCLUDE_FIELD_OPTIONS
.fieldOptions = "\000\000\000\002\020\001",
#else
.fieldOptions = NULL,
#endif // GPBOBJC_INCLUDE_FIELD_OPTIONS
},
{
.name = "sourceFile",
.number = GPBGeneratedCodeInfo_Annotation_FieldNumber_SourceFile,
.hasIndex = 1,
.flags = GPBFieldOptional,
.dataType = GPBDataTypeString,
.offset = offsetof(GPBGeneratedCodeInfo_Annotation__storage_, sourceFile),
.defaultValue.valueString = nil,
.dataTypeSpecific.className = NULL,
.fieldOptions = NULL,
},
{
.name = "begin",
.number = GPBGeneratedCodeInfo_Annotation_FieldNumber_Begin,
.hasIndex = 2,
.flags = GPBFieldOptional,
.dataType = GPBDataTypeInt32,
.offset = offsetof(GPBGeneratedCodeInfo_Annotation__storage_, begin),
.defaultValue.valueInt32 = 0,
.dataTypeSpecific.className = NULL,
.fieldOptions = NULL,
},
{
.name = "end",
.number = GPBGeneratedCodeInfo_Annotation_FieldNumber_End,
.hasIndex = 3,
.flags = GPBFieldOptional,
.dataType = GPBDataTypeInt32,
.offset = offsetof(GPBGeneratedCodeInfo_Annotation__storage_, end),
.defaultValue.valueInt32 = 0,
.dataTypeSpecific.className = NULL,
.fieldOptions = NULL,
},
};
GPBDescriptor *localDescriptor =
[GPBDescriptor allocDescriptorForClass:[GPBGeneratedCodeInfo_Annotation class]
rootClass:[GPBDescriptorRoot class]
file:GPBDescriptorRoot_FileDescriptor()
fields:fields
fieldCount:sizeof(fields) / sizeof(GPBMessageFieldDescription)
oneofs:NULL
oneofCount:0
enums:NULL
enumCount:0
ranges:NULL
rangeCount:0
storageSize:sizeof(GPBGeneratedCodeInfo_Annotation__storage_)
wireFormat:NO];
NSAssert(descriptor == nil, @"Startup recursed!");
descriptor = localDescriptor;
}
return descriptor;
}
@end
// @@protoc_insertion_point(global_scope) // @@protoc_insertion_point(global_scope)
...@@ -342,6 +342,8 @@ libprotoc_la_SOURCES = \ ...@@ -342,6 +342,8 @@ libprotoc_la_SOURCES = \
google/protobuf/compiler/java/java_enum_lite.h \ google/protobuf/compiler/java/java_enum_lite.h \
google/protobuf/compiler/java/java_extension.cc \ google/protobuf/compiler/java/java_extension.cc \
google/protobuf/compiler/java/java_extension.h \ google/protobuf/compiler/java/java_extension.h \
google/protobuf/compiler/java/java_extension_lite.cc \
google/protobuf/compiler/java/java_extension_lite.h \
google/protobuf/compiler/java/java_field.cc \ google/protobuf/compiler/java/java_field.cc \
google/protobuf/compiler/java/java_field.h \ google/protobuf/compiler/java/java_field.h \
google/protobuf/compiler/java/java_file.cc \ google/protobuf/compiler/java/java_file.cc \
...@@ -727,6 +729,7 @@ protobuf_test_SOURCES = \ ...@@ -727,6 +729,7 @@ protobuf_test_SOURCES = \
google/protobuf/compiler/cpp/cpp_unittest.h \ google/protobuf/compiler/cpp/cpp_unittest.h \
google/protobuf/compiler/cpp/cpp_unittest.cc \ google/protobuf/compiler/cpp/cpp_unittest.cc \
google/protobuf/compiler/cpp/cpp_plugin_unittest.cc \ google/protobuf/compiler/cpp/cpp_plugin_unittest.cc \
google/protobuf/compiler/cpp/metadata_test.cc \
google/protobuf/compiler/java/java_plugin_unittest.cc \ google/protobuf/compiler/java/java_plugin_unittest.cc \
google/protobuf/compiler/java/java_doc_comment_unittest.cc \ google/protobuf/compiler/java/java_doc_comment_unittest.cc \
google/protobuf/compiler/objectivec/objectivec_helpers_unittest.cc \ google/protobuf/compiler/objectivec/objectivec_helpers_unittest.cc \
......
...@@ -28,97 +28,31 @@ ...@@ -28,97 +28,31 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.google.protobuf; #include <memory>
#ifndef _SHARED_PTR_H
import java.io.IOException; #include <google/protobuf/stubs/shared_ptr.h>
import java.io.InvalidObjectException; #endif
import java.io.ObjectInputStream;
#include <google/protobuf/compiler/cpp/cpp_helpers.h>
/** #include <google/protobuf/compiler/cpp/cpp_generator.h>
* This class is used to represent the substring of a {@link ByteString} over a #include <google/protobuf/compiler/command_line_interface.h>
* single byte array. In terms of the public API of {@link ByteString}, you end #include <google/protobuf/io/zero_copy_stream.h>
* up here by calling {@link ByteString#copyFrom(byte[])} followed by {@link #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
* ByteString#substring(int, int)}. #include <google/protobuf/io/printer.h>
* #include <google/protobuf/descriptor.pb.h>
* <p>This class contains most of the overhead involved in creating a substring
* from a {@link LiteralByteString}. The overhead involves some range-checking #include <google/protobuf/testing/googletest.h>
* and two extra fields. #include <gtest/gtest.h>
* #include <google/protobuf/testing/file.h>
* @author carlanton@google.com (Carl Haverl)
*/ namespace google {
final class BoundedByteString extends LiteralByteString { namespace protobuf {
namespace compiler {
private final int bytesOffset; namespace cpp {
private final int bytesLength; namespace {
/** } // namespace
* Creates a {@code BoundedByteString} backed by the sub-range of given array, } // namespace cpp
* without copying. } // namespace compiler
* } // namespace protobuf
* @param bytes array to wrap } // namespace google
* @param offset index to first byte to use in bytes
* @param length number of bytes to use from bytes
* @throws IllegalArgumentException if {@code offset < 0}, {@code length < 0},
* or if {@code offset + length >
* bytes.length}.
*/
BoundedByteString(byte[] bytes, int offset, int length) {
super(bytes);
checkRange(offset, offset + length, bytes.length);
this.bytesOffset = offset;
this.bytesLength = length;
}
/**
* Gets the byte at the given index.
* Throws {@link ArrayIndexOutOfBoundsException}
* for backwards-compatibility reasons although it would more properly be
* {@link IndexOutOfBoundsException}.
*
* @param index index of byte
* @return the value
* @throws ArrayIndexOutOfBoundsException {@code index} is < 0 or >= size
*/
@Override
public byte byteAt(int index) {
// We must check the index ourselves as we cannot rely on Java array index
// checking for substrings.
checkIndex(index, size());
return bytes[bytesOffset + index];
}
@Override
public int size() {
return bytesLength;
}
@Override
protected int getOffsetIntoBytes() {
return bytesOffset;
}
// =================================================================
// ByteString -> byte[]
@Override
protected void copyToInternal(byte[] target, int sourceOffset, int targetOffset,
int numberToCopy) {
System.arraycopy(bytes, getOffsetIntoBytes() + sourceOffset, target,
targetOffset, numberToCopy);
}
// =================================================================
// Serializable
private static final long serialVersionUID = 1L;
Object writeReplace() {
return new LiteralByteString(toByteArray());
}
private void readObject(@SuppressWarnings("unused") ObjectInputStream in) throws IOException {
throw new InvalidObjectException(
"BoundedByteStream instances are not to be serialized directly");
}
}
This diff is collapsed.
This diff is collapsed.
...@@ -379,7 +379,7 @@ message FileOptions { ...@@ -379,7 +379,7 @@ message FileOptions {
// Whether the nano proto compiler should generate in the deprecated non-nano // Whether the nano proto compiler should generate in the deprecated non-nano
// suffixed package. // suffixed package.
optional bool javanano_use_deprecated_package = 38; optional bool javanano_use_deprecated_package = 38 [deprecated = true];
// The parser stores options it doesn't recognize here. See above. // The parser stores options it doesn't recognize here. See above.
repeated UninterpretedOption uninterpreted_option = 999; repeated UninterpretedOption uninterpreted_option = 999;
...@@ -777,3 +777,29 @@ message SourceCodeInfo { ...@@ -777,3 +777,29 @@ message SourceCodeInfo {
repeated string leading_detached_comments = 6; repeated string leading_detached_comments = 6;
} }
} }
// Describes the relationship between generated code and its original source
// file. A GeneratedCodeInfo message is associated with only one generated
// source file, but may contain references to different source .proto files.
message GeneratedCodeInfo {
// An Annotation connects some span of text in generated code to an element
// of its generating .proto file.
repeated Annotation annotation = 1;
message Annotation {
// Identifies the element in the original source .proto file. This field
// is formatted the same as SourceCodeInfo.Location.path.
repeated int32 path = 1 [packed=true];
// Identifies the filesystem path to the original source .proto.
optional string source_file = 2;
// Identifies the starting offset in bytes in the generated code
// that relates to the identified object.
optional int32 begin = 3;
// Identifies the ending offset in bytes in the generated code that
// relates to the identified offset. The end offset should be one past
// the last relevant byte (so the length of the text = end - begin).
optional int32 end = 4;
}
}
...@@ -44,6 +44,35 @@ message MapOut { ...@@ -44,6 +44,35 @@ message MapOut {
map<string, MapM> map1 = 1; map<string, MapM> map1 = 1;
map<string, MapOut> map2 = 2; map<string, MapOut> map2 = 2;
map<int32, string> map3 = 3; map<int32, string> map3 = 3;
map<bool, string> map4 = 5;
string bar = 4;
}
// A message with exactly the same wire representation as MapOut, but using
// repeated message fields instead of map fields. We use this message to test
// the wire-format compatibility of the JSON transcoder (e.g., whether it
// handles missing keys correctly).
message MapOutWireFormat {
message Map1Entry {
string key = 1;
MapM value = 2;
}
repeated Map1Entry map1 = 1;
message Map2Entry {
string key = 1;
MapOut value = 2;
}
repeated Map2Entry map2 = 2;
message Map3Entry {
int32 key = 1;
string value = 2;
}
repeated Map3Entry map3 = 3;
message Map4Entry {
bool key = 1;
string value = 2;
}
repeated Map4Entry map4 = 5;
string bar = 4; string bar = 4;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment