Skip to content

Commit

Permalink
Update LeafObjectCodec to allow use of SerializationDependencyProvider.
Browse files Browse the repository at this point in the history
This is useful in a couple of places and doesn't break any asynchrony
requirements.

PiperOrigin-RevId: 593860553
Change-Id: I9946cb13a522b655445f26ef5ee7d93592bfe67e
  • Loading branch information
aoeui authored and copybara-github committed Dec 26, 2023
1 parent 5969629 commit 743e24b
Show file tree
Hide file tree
Showing 22 changed files with 127 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ public Class<? extends BigInteger> getEncodedClass() {
}

@Override
public void serialize(BigInteger obj, CodedOutputStream codedOut) throws IOException {
public void serialize(
SerializationDependencyProvider dependencies, BigInteger obj, CodedOutputStream codedOut)
throws IOException {
codedOut.writeByteArrayNoTag(obj.toByteArray());
}

@Override
public BigInteger deserialize(CodedInputStream codedIn)
public BigInteger deserialize(
SerializationDependencyProvider dependencies, CodedInputStream codedIn)
throws SerializationException, IOException {
return new BigInteger(codedIn.readByteArray());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ public Class<? extends BitSet> getEncodedClass() {
}

@Override
public void serialize(BitSet obj, CodedOutputStream codedOut) throws IOException {
public void serialize(
SerializationDependencyProvider dependencies, BitSet obj, CodedOutputStream codedOut)
throws IOException {
long[] data = obj.toLongArray();
DELEGATE.serialize(data, codedOut);
DELEGATE.serialize(dependencies, data, codedOut);
}

@Override
public BitSet deserialize(CodedInputStream codedIn) throws IOException {
return BitSet.valueOf(DELEGATE.deserialize(codedIn));
public BitSet deserialize(SerializationDependencyProvider dependencies, CodedInputStream codedIn)
throws IOException {
return BitSet.valueOf(DELEGATE.deserialize(dependencies, codedIn));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ public Class<Boolean> getEncodedClass() {
}

@Override
public void serialize(Boolean value, CodedOutputStream codedOut) throws IOException {
public void serialize(
SerializationDependencyProvider dependencies, Boolean value, CodedOutputStream codedOut)
throws IOException {
codedOut.writeBoolNoTag(value);
}

@Override
public Boolean deserialize(CodedInputStream codedIn) throws IOException {
public Boolean deserialize(SerializationDependencyProvider dependencies, CodedInputStream codedIn)
throws IOException {
return codedIn.readBool();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ public Class<? extends byte[]> getEncodedClass() {
}

@Override
public void serialize(byte[] obj, CodedOutputStream codedOut) throws IOException {
public void serialize(
SerializationDependencyProvider dependencies, byte[] obj, CodedOutputStream codedOut)
throws IOException {
codedOut.writeByteArrayNoTag(obj);
}

@Override
public byte[] deserialize(CodedInputStream codedIn) throws IOException {
public byte[] deserialize(SerializationDependencyProvider dependencies, CodedInputStream codedIn)
throws IOException {
return codedIn.readByteArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ public Class<? extends ByteString> getEncodedClass() {
}

@Override
public void serialize(ByteString obj, CodedOutputStream codedOut) throws IOException {
public void serialize(
SerializationDependencyProvider dependencies, ByteString obj, CodedOutputStream codedOut)
throws IOException {
codedOut.writeBytesNoTag(obj);
}

@Override
public ByteString deserialize(CodedInputStream codedIn) throws IOException {
public ByteString deserialize(
SerializationDependencyProvider dependencies, CodedInputStream codedIn) throws IOException {
return codedIn.readBytes();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,26 @@ public Class<Class<?>> getEncodedClass() {
}

@Override
public void serialize(Class<?> obj, CodedOutputStream codedOut)
public void serialize(
SerializationDependencyProvider dependencies, Class<?> obj, CodedOutputStream codedOut)
throws SerializationException, IOException {
codedOut.writeBoolNoTag(obj.isPrimitive());
if (obj.isPrimitive()) {
codedOut.writeInt32NoTag(Preconditions.checkNotNull(PRIMITIVE_CLASS_INDEX_MAP.get(obj), obj));
} else {
stringCodec().serialize(obj.getName(), codedOut);
stringCodec().serialize(dependencies, obj.getName(), codedOut);
}
}

@Override
public Class<?> deserialize(CodedInputStream codedIn) throws SerializationException, IOException {
public Class<?> deserialize(
SerializationDependencyProvider dependencies, CodedInputStream codedIn)
throws SerializationException, IOException {
boolean isPrimitive = codedIn.readBool();
if (isPrimitive) {
return PRIMITIVE_CLASS_INDEX_MAP.inverse().get(codedIn.readInt32());
}
String className = stringCodec().deserialize(codedIn);
String className = stringCodec().deserialize(dependencies, codedIn);
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ public Class<? extends T> getEncodedClass() {
}

@Override
public void serialize(T obj, CodedOutputStream codedOut) throws SerializationException {
public void serialize(
SerializationDependencyProvider dependencies, T obj, CodedOutputStream codedOut)
throws SerializationException {
throw new SerializationException(message);
}

@Override
public T deserialize(CodedInputStream codedIn) throws SerializationException {
public T deserialize(SerializationDependencyProvider dependencies, CodedInputStream codedIn)
throws SerializationException {
throw new SerializationException(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ public Class<? extends Double> getEncodedClass() {
}

@Override
public void serialize(Double value, CodedOutputStream codedOut) throws IOException {
public void serialize(
SerializationDependencyProvider dependencies, Double value, CodedOutputStream codedOut)
throws IOException {
codedOut.writeDoubleNoTag(value);
}

@Override
public Double deserialize(CodedInputStream codedIn) throws IOException {
public Double deserialize(SerializationDependencyProvider dependencies, CodedInputStream codedIn)
throws IOException {
return codedIn.readDouble();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
/** Encodes a Duration. */
public class DurationCodec extends LeafObjectCodec<Duration> {
@Override
public void serialize(Duration obj, CodedOutputStream codedOut) throws IOException {
public void serialize(
SerializationDependencyProvider dependencies, Duration obj, CodedOutputStream codedOut)
throws IOException {
codedOut.writeInt64NoTag(obj.getSeconds());
codedOut.writeInt32NoTag(obj.getNano());
}

@Override
public Duration deserialize(CodedInputStream codedIn) throws IOException {
public Duration deserialize(
SerializationDependencyProvider dependencies, CodedInputStream codedIn) throws IOException {
return Duration.ofSeconds(codedIn.readInt64(), codedIn.readInt32());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ public Class<? extends List> getEncodedClass() {
}

@Override
public void serialize(List unusedValue, CodedOutputStream unusedCodedOut) {}
public void serialize(
SerializationDependencyProvider dependencies,
List unusedValue,
CodedOutputStream unusedCodedOut) {}

@Override
public List deserialize(CodedInputStream unusedCodedIn) {
public List deserialize(
SerializationDependencyProvider dependencies, CodedInputStream unusedCodedIn) {
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ public Class<T> getEncodedClass() {
}

@Override
public void serialize(T value, CodedOutputStream codedOut) throws IOException {
public void serialize(
SerializationDependencyProvider dependencies, T value, CodedOutputStream codedOut)
throws IOException {
Preconditions.checkNotNull(value, "Enum value for %s is null", enumClass);
codedOut.writeEnumNoTag(value.ordinal());
}

@Override
public T deserialize(CodedInputStream codedIn) throws SerializationException, IOException {
public T deserialize(SerializationDependencyProvider dependencies, CodedInputStream codedIn)
throws SerializationException, IOException {
int ordinal = codedIn.readEnum();
try {
return values.get(ordinal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
public class HashCodeCodec extends LeafObjectCodec<HashCode> {

@Override
public void serialize(HashCode obj, CodedOutputStream codedOut) throws IOException {
public void serialize(
SerializationDependencyProvider dependencies, HashCode obj, CodedOutputStream codedOut)
throws IOException {
codedOut.writeByteArrayNoTag(obj.asBytes());
}

@Override
public HashCode deserialize(CodedInputStream codedIn) throws IOException {
public HashCode deserialize(
SerializationDependencyProvider dependencies, CodedInputStream codedIn) throws IOException {
return HashCode.fromBytes(codedIn.readByteArray());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ public Class<Integer> getEncodedClass() {
}

@Override
public void serialize(Integer value, CodedOutputStream codedOut) throws IOException {
public void serialize(
SerializationDependencyProvider dependencies, Integer value, CodedOutputStream codedOut)
throws IOException {
codedOut.writeInt32NoTag(value);
}

@Override
public Integer deserialize(CodedInputStream codedIn) throws IOException {
public Integer deserialize(SerializationDependencyProvider dependencies, CodedInputStream codedIn)
throws IOException {
return codedIn.readInt32();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,28 @@ public abstract class LeafObjectCodec<T> implements ObjectCodec<T> {
@Override
public final void serialize(SerializationContext context, T obj, CodedOutputStream codedOut)
throws SerializationException, IOException {
serialize(obj, codedOut);
serialize((SerializationDependencyProvider) context, obj, codedOut);
}

/**
* This has the same contract as {@link #serialize}, but never uses the {@link
* SerializationContext}.
* This has the same contract as {@link #serialize}, but may only depend on {@link
* SerializationDependencyProvider} instead of the full {@link SerializationContext}.
*/
public abstract void serialize(T obj, CodedOutputStream codedOut)
public abstract void serialize(
SerializationDependencyProvider dependencies, T obj, CodedOutputStream codedOut)
throws SerializationException, IOException;

@Override
public final T deserialize(DeserializationContext context, CodedInputStream codedIn)
throws SerializationException, IOException {
return deserialize(codedIn);
return deserialize((SerializationDependencyProvider) context, codedIn);
}

/**
* This has the same contract as {@link #deserialize}, but never uses the {@link
* DeserializationContext}.
* This has the same contract as {@link #deserialize}, but may only depend on {@link
* SerializationDependencyProvider} instead of the full {@link DeserializationContext}.
*/
public abstract T deserialize(CodedInputStream codedIn)
public abstract T deserialize(
SerializationDependencyProvider dependencies, CodedInputStream codedIn)
throws SerializationException, IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ public Class<? extends long[]> getEncodedClass() {
}

@Override
public void serialize(long[] obj, CodedOutputStream codedOut) throws IOException {
public void serialize(
SerializationDependencyProvider dependencies, long[] obj, CodedOutputStream codedOut)
throws IOException {
codedOut.writeInt32NoTag(obj.length);
for (long l : obj) {
codedOut.writeInt64NoTag(l);
}
}

@Override
public long[] deserialize(CodedInputStream codedIn) throws IOException {
public long[] deserialize(SerializationDependencyProvider dependencies, CodedInputStream codedIn)
throws IOException {
long[] result = new long[codedIn.readInt32()];
for (int i = 0; i < result.length; i++) {
result[i] = codedIn.readInt64();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ public Class<Long> getEncodedClass() {
}

@Override
public void serialize(Long value, CodedOutputStream codedOut) throws IOException {
public void serialize(
SerializationDependencyProvider dependencies, Long value, CodedOutputStream codedOut)
throws IOException {
codedOut.writeInt64NoTag(value);
}

@Override
public Long deserialize(CodedInputStream codedIn) throws IOException {
public Long deserialize(SerializationDependencyProvider dependencies, CodedInputStream codedIn)
throws IOException {
return codedIn.readInt64();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ public Class<? extends MessageLite> getEncodedClass() {
}

@Override
public void serialize(MessageLite message, CodedOutputStream codedOut) throws IOException {
public void serialize(
SerializationDependencyProvider dependencies, MessageLite message, CodedOutputStream codedOut)
throws IOException {
codedOut.writeMessageNoTag(message);
}

@Override
public MessageLite deserialize(CodedInputStream codedIn)
public MessageLite deserialize(
SerializationDependencyProvider dependencies, CodedInputStream codedIn)
throws IOException, SerializationException {
// Don't hold on to full byte array when constructing this proto.
codedIn.enableAliasing(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,27 @@ public Class<Method> getEncodedClass() {
}

@Override
public void serialize(Method obj, CodedOutputStream codedOut)
public void serialize(
SerializationDependencyProvider dependencies, Method obj, CodedOutputStream codedOut)
throws SerializationException, IOException {
CLASS_CODEC.serialize(obj.getDeclaringClass(), codedOut);
stringCodec().serialize(obj.getName(), codedOut);
CLASS_CODEC.serialize(dependencies, obj.getDeclaringClass(), codedOut);
stringCodec().serialize(dependencies, obj.getName(), codedOut);
Class<?>[] parameterTypes = obj.getParameterTypes();
codedOut.writeInt32NoTag(parameterTypes.length);
for (Class<?> parameter : parameterTypes) {
CLASS_CODEC.serialize(parameter, codedOut);
CLASS_CODEC.serialize(dependencies, parameter, codedOut);
}
}

@Override
public Method deserialize(CodedInputStream codedIn) throws SerializationException, IOException {
Class<?> clazz = CLASS_CODEC.deserialize(codedIn);
String name = stringCodec().deserialize(codedIn);
public Method deserialize(SerializationDependencyProvider dependencies, CodedInputStream codedIn)
throws SerializationException, IOException {
Class<?> clazz = CLASS_CODEC.deserialize(dependencies, codedIn);
String name = stringCodec().deserialize(dependencies, codedIn);

Class<?>[] parameters = new Class<?>[codedIn.readInt32()];
for (int i = 0; i < parameters.length; i++) {
parameters[i] = CLASS_CODEC.deserialize(codedIn);
parameters[i] = CLASS_CODEC.deserialize(dependencies, codedIn);
}
try {
return clazz.getDeclaredMethod(name, parameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,15 @@ public Class<T> getEncodedClass() {
}

@Override
public void serialize(T t, CodedOutputStream codedOut) throws IOException {
public void serialize(
SerializationDependencyProvider dependencies, T t, CodedOutputStream codedOut)
throws IOException {
codedOut.writeByteArrayNoTag(mnemonic);
}

@Override
public T deserialize(CodedInputStream codedIn) throws SerializationException, IOException {
public T deserialize(SerializationDependencyProvider dependencies, CodedInputStream codedIn)
throws SerializationException, IOException {
// Get ByteBuffer instead of raw bytes, as it may be a direct view of the data and not a copy,
// which is much more efficient.
ByteBuffer readMnemonic = codedIn.readByteBuffer();
Expand Down
Loading

0 comments on commit 743e24b

Please sign in to comment.