aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar shahan <shahan@google.com>2018-03-15 16:02:08 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-03-15 16:04:14 -0700
commita16887edd766989af3b50dd3b42afab8338c6d4b (patch)
treef7448506338c8acc736c0d4992e2613d1be8297b
parentdc060da6707fc13bb2c6a22ba5326dbe3000f67f (diff)
Makes leaf level AutoCodec marshallers into runtime codecs.
Next change will convert containers. PiperOrigin-RevId: 189261293
-rw-r--r--src/main/java/com/google/devtools/build/lib/buildeventstream/BuildEventStreamProtoCodecRegisterer.java28
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/cpp/CrosstoolConfigProtoCodecRegisterer.java31
-rw-r--r--src/main/java/com/google/devtools/build/lib/skyframe/serialization/CodecRegisterer.java8
-rw-r--r--src/main/java/com/google/devtools/build/lib/skyframe/serialization/CodecScanner.java9
-rw-r--r--src/main/java/com/google/devtools/build/lib/skyframe/serialization/EnumRuntimeCodec.java58
-rw-r--r--src/main/java/com/google/devtools/build/lib/skyframe/serialization/MessageLiteCodec.java64
-rw-r--r--src/main/java/com/google/devtools/build/lib/skyframe/serialization/ObjectCodec.java2
-rw-r--r--src/main/java/com/google/devtools/build/lib/skyframe/serialization/UUIDCodec.java41
-rw-r--r--src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec/Marshallers.java195
-rw-r--r--src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/CharsetCodec.java44
-rw-r--r--src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/PatternCodec.java45
-rw-r--r--src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/StringCodecs.java12
-rw-r--r--src/test/java/com/google/devtools/build/lib/skyframe/serialization/DeserializationContextTest.java2
13 files changed, 331 insertions, 208 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/buildeventstream/BuildEventStreamProtoCodecRegisterer.java b/src/main/java/com/google/devtools/build/lib/buildeventstream/BuildEventStreamProtoCodecRegisterer.java
new file mode 100644
index 0000000000..8c37ab46ef
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/buildeventstream/BuildEventStreamProtoCodecRegisterer.java
@@ -0,0 +1,28 @@
+// Copyright 2018 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.buildeventstream;
+
+import com.google.common.collect.ImmutableList;
+import com.google.devtools.build.lib.skyframe.serialization.CodecRegisterer;
+import com.google.devtools.build.lib.skyframe.serialization.MessageLiteCodec;
+
+class BuildEventStreamProtoCodecRegisterer implements CodecRegisterer<MessageLiteCodec> {
+ @Override
+ public Iterable<MessageLiteCodec> getCodecsToRegister() {
+ return ImmutableList.of(
+ new MessageLiteCodec(BuildEventStreamProtos.BuildEventId::newBuilder),
+ new MessageLiteCodec(BuildEventStreamProtos.BuildEvent::newBuilder));
+ }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CrosstoolConfigProtoCodecRegisterer.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CrosstoolConfigProtoCodecRegisterer.java
new file mode 100644
index 0000000000..bad9dc0319
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CrosstoolConfigProtoCodecRegisterer.java
@@ -0,0 +1,31 @@
+// Copyright 2018 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.rules.cpp;
+
+import com.google.common.collect.ImmutableList;
+import com.google.devtools.build.lib.skyframe.serialization.CodecRegisterer;
+import com.google.devtools.build.lib.skyframe.serialization.MessageLiteCodec;
+import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig;
+
+class CrosstoolConfigProtoCodecRegisterer implements CodecRegisterer<MessageLiteCodec> {
+ @Override
+ public Iterable<MessageLiteCodec> getCodecsToRegister() {
+ return ImmutableList.of(
+ new MessageLiteCodec(CrosstoolConfig.CrosstoolRelease::newBuilder),
+ new MessageLiteCodec(CrosstoolConfig.CToolchain::newBuilder),
+ new MessageLiteCodec(CrosstoolConfig.CToolchain.OptionalFlag::newBuilder),
+ new MessageLiteCodec(CrosstoolConfig.CToolchain.Tool::newBuilder));
+ }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/CodecRegisterer.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/CodecRegisterer.java
index 72d99191e3..9176a978d2 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/CodecRegisterer.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/CodecRegisterer.java
@@ -14,6 +14,8 @@
package com.google.devtools.build.lib.skyframe.serialization;
+import java.util.Collections;
+
/**
* Custom registration behavior for a codec.
*
@@ -31,8 +33,6 @@ package com.google.devtools.build.lib.skyframe.serialization;
*
* <p>Implementations must have a default constructor.
*
- * <p>Multiple {@link CodecRegisterer} implementations for the same {@link ObjectCodec} is illegal.
- *
* <p>Inheriting {@link CodecRegisterer} through a superclass is illegal. It must be directly
* implemented. Also, the generic parameter of {@link CodecRegisterer} must be reified.
*
@@ -40,5 +40,7 @@ package com.google.devtools.build.lib.skyframe.serialization;
*/
public interface CodecRegisterer<T extends ObjectCodec<?>> {
- default void register(ObjectCodecRegistry.Builder builder) {}
+ default Iterable<? extends ObjectCodec<?>> getCodecsToRegister() {
+ return Collections.emptyList();
+ }
}
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/CodecScanner.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/CodecScanner.java
index 1adb15047f..635b5ad755 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/CodecScanner.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/CodecScanner.java
@@ -122,15 +122,14 @@ public class CodecScanner {
HashSet<Class<? extends ObjectCodec<?>>> registered = new HashSet<>();
for (Class<? extends CodecRegisterer<?>> registererType : registerers) {
Class<? extends ObjectCodec<?>> objectCodecType = getObjectCodecType(registererType);
- Preconditions.checkState(
- !registered.contains(objectCodecType),
- "%s has multiple associated CodecRegisterer definitions!",
- objectCodecType);
registered.add(objectCodecType);
Constructor<CodecRegisterer<?>> constructor =
(Constructor<CodecRegisterer<?>>) registererType.getDeclaredConstructor();
constructor.setAccessible(true);
- constructor.newInstance().register(builder);
+ CodecRegisterer<?> registerer = constructor.newInstance();
+ for (ObjectCodec<?> codec : registerer.getCodecsToRegister()) {
+ builder.add(codec);
+ }
}
return registered;
}
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/EnumRuntimeCodec.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/EnumRuntimeCodec.java
new file mode 100644
index 0000000000..d9f2ea4aed
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/EnumRuntimeCodec.java
@@ -0,0 +1,58 @@
+// Copyright 2018 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.skyframe.serialization;
+
+import com.google.protobuf.ByteString;
+import com.google.protobuf.CodedInputStream;
+import com.google.protobuf.CodedOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.nio.ByteBuffer;
+
+@SuppressWarnings("rawtypes")
+class EnumRuntimeCodec implements ObjectCodec<Enum> {
+
+ @Override
+ public Class<Enum> getEncodedClass() {
+ return Enum.class;
+ }
+
+ @Override
+ public void serialize(SerializationContext unusedContext, Enum value, CodedOutputStream codedOut)
+ throws IOException, SerializationException {
+ // We're using Java serialization below because Enums are serializable by default and a
+ // hand-rolled version is going to be very similar.
+ ByteString.Output out = ByteString.newOutput();
+ ObjectOutputStream objOut = new ObjectOutputStream(out);
+ objOut.writeObject(value);
+ codedOut.writeBytesNoTag(out.toByteString());
+ }
+
+ @Override
+ public Enum deserialize(DeserializationContext unusedContext, CodedInputStream codedIn)
+ throws SerializationException, IOException {
+ ByteBuffer buffer = codedIn.readByteBuffer();
+ ObjectInputStream objIn =
+ new ObjectInputStream(
+ new ByteArrayInputStream(buffer.array(), buffer.arrayOffset(), buffer.remaining()));
+ try {
+ return (Enum) objIn.readObject();
+ } catch (ClassNotFoundException e) {
+ throw new SerializationException("Couldn't find class for Enum deserialization?", e);
+ }
+ }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/MessageLiteCodec.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/MessageLiteCodec.java
new file mode 100644
index 0000000000..e7cce534c8
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/MessageLiteCodec.java
@@ -0,0 +1,64 @@
+// Copyright 2018 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.skyframe.serialization;
+
+import com.google.protobuf.CodedInputStream;
+import com.google.protobuf.CodedOutputStream;
+import com.google.protobuf.ExtensionRegistryLite;
+import com.google.protobuf.InvalidProtocolBufferException;
+import com.google.protobuf.MessageLite;
+import java.io.IOException;
+import java.util.function.Supplier;
+
+/** Codec for protos. */
+public class MessageLiteCodec implements ObjectCodec<MessageLite> {
+
+ private final Supplier<MessageLite.Builder> builderSupplier;
+ private final Class<? extends MessageLite> type;
+
+ /**
+ * Constructor.
+ *
+ * @param builderSupplier reference to a proto's newBuilder method
+ */
+ public MessageLiteCodec(Supplier<MessageLite.Builder> builderSupplier) {
+ this.builderSupplier = builderSupplier;
+ this.type = builderSupplier.get().buildPartial().getClass();
+ }
+
+ @Override
+ public Class<? extends MessageLite> getEncodedClass() {
+ return type;
+ }
+
+ @Override
+ public void serialize(
+ SerializationContext unusedContext, MessageLite message, CodedOutputStream codedOut)
+ throws IOException, SerializationException {
+ codedOut.writeMessageNoTag(message);
+ }
+
+ @Override
+ public MessageLite deserialize(DeserializationContext unusedContext, CodedInputStream codedIn)
+ throws IOException, SerializationException {
+ try {
+ MessageLite.Builder builder = builderSupplier.get();
+ codedIn.readMessage(builder, ExtensionRegistryLite.getEmptyRegistry());
+ return builder.build();
+ } catch (InvalidProtocolBufferException e) {
+ throw new SerializationException("Failed to parse proto of type " + type, e);
+ }
+ }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/ObjectCodec.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/ObjectCodec.java
index 4b47b4bad0..a754246db6 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/ObjectCodec.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/ObjectCodec.java
@@ -31,7 +31,7 @@ public interface ObjectCodec<T> {
* <p>This is useful for automatically dispatching to the correct codec, e.g. in {@link
* ObjectCodecs}.
*/
- Class<T> getEncodedClass();
+ Class<? extends T> getEncodedClass();
/**
* Returns additional subtypes of {@code T} that may be serialized/deserialized using this codec
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/UUIDCodec.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/UUIDCodec.java
new file mode 100644
index 0000000000..7e0faa0bdb
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/UUIDCodec.java
@@ -0,0 +1,41 @@
+// Copyright 2018 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.skyframe.serialization;
+
+import com.google.protobuf.CodedInputStream;
+import com.google.protobuf.CodedOutputStream;
+import java.io.IOException;
+import java.util.UUID;
+
+class UUIDCodec implements ObjectCodec<UUID> {
+
+ @Override
+ public Class<UUID> getEncodedClass() {
+ return UUID.class;
+ }
+
+ @Override
+ public void serialize(SerializationContext unusedContext, UUID uuid, CodedOutputStream codedOut)
+ throws SerializationException, IOException {
+ codedOut.writeInt64NoTag(uuid.getMostSignificantBits());
+ codedOut.writeInt64NoTag(uuid.getLeastSignificantBits());
+ }
+
+ @Override
+ public UUID deserialize(DeserializationContext unusedContext, CodedInputStream codedIn)
+ throws SerializationException, IOException {
+ return new UUID(codedIn.readInt64(), codedIn.readInt64());
+ }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec/Marshallers.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec/Marshallers.java
index 692fb9904a..330c2e4429 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec/Marshallers.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec/Marshallers.java
@@ -26,11 +26,7 @@ import com.google.devtools.build.lib.skyframe.serialization.autocodec.Serializat
import com.google.devtools.build.lib.skyframe.serialization.autocodec.SerializationCodeGenerator.Marshaller;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.SerializationCodeGenerator.PrimitiveValueSerializationCodeGenerator;
import com.google.devtools.build.lib.skyframe.serialization.strings.StringCodecs;
-import com.google.protobuf.AbstractMessage;
-import com.google.protobuf.ExtensionRegistryLite;
-import com.google.protobuf.ProtocolMessageEnum;
import com.squareup.javapoet.TypeName;
-import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -38,10 +34,7 @@ import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
-import java.util.UUID;
-import java.util.regex.Pattern;
import javax.annotation.processing.ProcessingEnvironment;
-import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.ArrayType;
import javax.lang.model.type.DeclaredType;
@@ -331,70 +324,6 @@ class Marshallers {
}
};
- private final Marshaller enumMarshaller =
- new Marshaller() {
- @Override
- public boolean matches(DeclaredType type) {
- return env.getTypeUtils().asElement(type).getKind() == ElementKind.ENUM;
- }
-
- @Override
- public void addSerializationCode(Context context) {
- if (isProtoEnum(context.getDeclaredType())) {
- context.builder.addStatement("codedOut.writeInt32NoTag($L.getNumber())", context.name);
- } else {
- context.builder.addStatement("codedOut.writeInt32NoTag($L.ordinal())", context.name);
- }
- }
-
- @Override
- public void addDeserializationCode(Context context) {
- if (isProtoEnum(context.getDeclaredType())) {
- context.builder.addStatement(
- "$L = $T.forNumber(codedIn.readInt32())", context.name, context.getTypeName());
- } else {
- // TODO(shahan): memoize this expensive call to values().
- context.builder.addStatement(
- "$L = $T.values()[codedIn.readInt32()]", context.name, context.getTypeName());
- }
- }
-
- private boolean isProtoEnum(DeclaredType type) {
- return env.getTypeUtils()
- .isSubtype(
- type,
- env.getElementUtils()
- .getTypeElement(ProtocolMessageEnum.class.getCanonicalName())
- .asType());
- }
- };
-
- private static void addStringDeserializationCode(Context context) {
- context.builder.addStatement(
- "$L = $T.asciiOptimized().deserialize(context, codedIn)", context.name, StringCodecs.class);
- }
-
- private final Marshaller stringMarshaller =
- new Marshaller() {
- @Override
- public boolean matches(DeclaredType type) {
- return matchesType(type, String.class);
- }
-
- @Override
- public void addSerializationCode(Context context) {
- context.builder.addStatement(
- "$T.asciiOptimized().serialize(context, $L, codedOut)",
- StringCodecs.class,
- context.name);
- }
-
- @Override
- public void addDeserializationCode(Context context) {
- addStringDeserializationCode(context);
- }
- };
-
private final Marshaller charSequenceMarshaller =
new Marshaller() {
@Override
@@ -412,37 +341,10 @@ class Marshallers {
@Override
public void addDeserializationCode(Context context) {
- addStringDeserializationCode(context);
- }
- };
-
- private final Marshaller uuidMarshller =
- new Marshaller() {
- @Override
- public boolean matches(DeclaredType type) {
- return matchesType(type, UUID.class);
- }
-
- @Override
- public void addSerializationCode(Context context) {
- context.builder.addStatement(
- "codedOut.writeInt64NoTag($L.getMostSignificantBits())", context.name);
- context.builder.addStatement(
- "codedOut.writeInt64NoTag($L.getLeastSignificantBits())", context.name);
- }
-
- @Override
- public void addDeserializationCode(Context context) {
- String mostSignificantBitsName = context.makeName("mostSignificantBits");
- String leastSignificantBitsName = context.makeName("leastSignificantBits");
- context.builder.addStatement("long $L = codedIn.readInt64()", mostSignificantBitsName);
- context.builder.addStatement("long $L = codedIn.readInt64()", leastSignificantBitsName);
context.builder.addStatement(
- "$L = new $T($L, $L)",
+ "$L = $T.asciiOptimized().deserialize(context, codedIn)",
context.name,
- UUID.class,
- mostSignificantBitsName,
- leastSignificantBitsName);
+ StringCodecs.class);
}
};
@@ -737,63 +639,6 @@ class Marshallers {
}
};
- /** Since we cannot add a codec to {@link Pattern}, it needs to be supported natively. */
- private final Marshaller patternMarshaller =
- new Marshaller() {
- @Override
- public boolean matches(DeclaredType type) {
- return matchesType(type, Pattern.class);
- }
-
- @Override
- public void addSerializationCode(Context context) {
- context.builder.addStatement(
- "$T.asciiOptimized().serialize(context, $L.pattern(), codedOut)",
- StringCodecs.class,
- context.name);
- context.builder.addStatement("codedOut.writeInt32NoTag($L.flags())", context.name);
- }
-
- @Override
- public void addDeserializationCode(Context context) {
- context.builder.addStatement(
- "$L = $T.compile("
- + "$T.asciiOptimized().deserialize(context, codedIn), "
- + "codedIn.readInt32())",
- context.name,
- Pattern.class,
- StringCodecs.class);
- }
- };
-
- private final Marshaller protoMarshaller =
- new Marshaller() {
- @Override
- public boolean matches(DeclaredType type) {
- return isSubtype(type, AbstractMessage.class);
- }
-
- @Override
- public void addSerializationCode(Context context) {
- context.builder.addStatement("codedOut.writeMessageNoTag($L)", context.name);
- }
-
- @Override
- public void addDeserializationCode(Context context) {
- String builderName = context.makeName("builder");
- context.builder.addStatement(
- "$T.Builder $L = $T.newBuilder()",
- context.getTypeName(),
- builderName,
- context.getTypeName());
- context.builder.addStatement(
- "codedIn.readMessage($L, $T.getEmptyRegistry())",
- builderName,
- ExtensionRegistryLite.class);
- context.builder.addStatement("$L = $L.build()", context.name, builderName);
- }
- };
-
/** Delegates marshalling back to the context. */
private final Marshaller contextMarshaller =
new Marshaller() {
@@ -813,31 +658,6 @@ class Marshallers {
}
};
- private final Marshaller charsetMarshaller =
- new Marshaller() {
- @Override
- public boolean matches(DeclaredType type) {
- return matchesType(type, Charset.class);
- }
-
- @Override
- public void addSerializationCode(Context context) {
- context.builder.addStatement(
- "$T.asciiOptimized().serialize(context, $L.name(), codedOut)",
- StringCodecs.class,
- context.name);
- }
-
- @Override
- public void addDeserializationCode(Context context) {
- context.builder.addStatement(
- "$L = $T.forName($T.asciiOptimized().deserialize(context, codedIn))",
- context.name,
- Charset.class,
- StringCodecs.class);
- }
- };
-
private final ImmutableList<PrimitiveValueSerializationCodeGenerator> primitiveGenerators =
ImmutableList.of(
INT_CODE_GENERATOR,
@@ -848,20 +668,14 @@ class Marshallers {
private final ImmutableList<Marshaller> marshallers =
ImmutableList.of(
- enumMarshaller,
- stringMarshaller,
charSequenceMarshaller,
supplierMarshaller,
- uuidMarshller,
mapEntryMarshaller,
listMarshaller,
immutableSetMarshaller,
immutableSortedSetMarshaller,
mapMarshaller,
multimapMarshaller,
- patternMarshaller,
- protoMarshaller,
- charsetMarshaller,
contextMarshaller);
/** True when {@code type} has the same type as {@code clazz}. */
@@ -869,11 +683,6 @@ class Marshallers {
return env.getTypeUtils().isSameType(type, getType(clazz));
}
- /** True when {@code type} is a subtype of {@code clazz}. */
- private boolean isSubtype(TypeMirror type, Class<?> clazz) {
- return env.getTypeUtils().isSubtype(type, getType(clazz));
- }
-
/** True when erasure of {@code type} matches erasure of {@code clazz}. */
private boolean matchesErased(TypeMirror type, Class<?> clazz) {
return env.getTypeUtils()
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/CharsetCodec.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/CharsetCodec.java
new file mode 100644
index 0000000000..f011669f17
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/CharsetCodec.java
@@ -0,0 +1,44 @@
+// Copyright 2018 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.skyframe.serialization.strings;
+
+import com.google.devtools.build.lib.skyframe.serialization.DeserializationContext;
+import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec;
+import com.google.devtools.build.lib.skyframe.serialization.SerializationContext;
+import com.google.devtools.build.lib.skyframe.serialization.SerializationException;
+import com.google.protobuf.CodedInputStream;
+import com.google.protobuf.CodedOutputStream;
+import java.io.IOException;
+import java.nio.charset.Charset;
+
+class CharsetCodec implements ObjectCodec<Charset> {
+
+ @Override
+ public Class<Charset> getEncodedClass() {
+ return Charset.class;
+ }
+
+ @Override
+ public void serialize(SerializationContext context, Charset charset, CodedOutputStream codedOut)
+ throws SerializationException, IOException {
+ context.serialize(charset.name(), codedOut);
+ }
+
+ @Override
+ public Charset deserialize(DeserializationContext context, CodedInputStream codedIn)
+ throws SerializationException, IOException {
+ return Charset.forName(context.deserialize(codedIn));
+ }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/PatternCodec.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/PatternCodec.java
new file mode 100644
index 0000000000..3d1d495ef5
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/PatternCodec.java
@@ -0,0 +1,45 @@
+// Copyright 2018 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.skyframe.serialization.strings;
+
+import com.google.devtools.build.lib.skyframe.serialization.DeserializationContext;
+import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec;
+import com.google.devtools.build.lib.skyframe.serialization.SerializationContext;
+import com.google.devtools.build.lib.skyframe.serialization.SerializationException;
+import com.google.protobuf.CodedInputStream;
+import com.google.protobuf.CodedOutputStream;
+import java.io.IOException;
+import java.util.regex.Pattern;
+
+class PatternCodec implements ObjectCodec<Pattern> {
+
+ @Override
+ public Class<Pattern> getEncodedClass() {
+ return Pattern.class;
+ }
+
+ @Override
+ public void serialize(SerializationContext context, Pattern pattern, CodedOutputStream codedOut)
+ throws SerializationException, IOException {
+ context.serialize(pattern.pattern(), codedOut);
+ codedOut.writeInt32NoTag(pattern.flags());
+ }
+
+ @Override
+ public Pattern deserialize(DeserializationContext context, CodedInputStream codedIn)
+ throws SerializationException, IOException {
+ return Pattern.compile(context.deserialize(codedIn), codedIn.readInt32());
+ }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/StringCodecs.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/StringCodecs.java
index a5516a75a6..f9b294dac3 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/StringCodecs.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/StringCodecs.java
@@ -16,7 +16,7 @@ package com.google.devtools.build.lib.skyframe.serialization.strings;
import com.google.devtools.build.lib.skyframe.serialization.CodecRegisterer;
import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec;
-import com.google.devtools.build.lib.skyframe.serialization.ObjectCodecRegistry;
+import java.util.Collections;
import java.util.logging.Logger;
/** Utility for accessing (potentially platform-specific) {@link String} {@link ObjectCodec}s. */
@@ -74,10 +74,11 @@ public final class StringCodecs {
*/
static class StringCodecRegisterer implements CodecRegisterer<StringCodec> {
@Override
- public void register(ObjectCodecRegistry.Builder builder) {
+ public Iterable<? extends ObjectCodec<?>> getCodecsToRegister() {
if (!supportsOptimizedAscii()) {
- builder.add(simple());
+ return Collections.singletonList(simple());
}
+ return Collections.emptyList();
}
}
@@ -88,10 +89,11 @@ public final class StringCodecs {
*/
static class FastStringCodecRegisterer implements CodecRegisterer<FastStringCodec> {
@Override
- public void register(ObjectCodecRegistry.Builder builder) {
+ public Iterable<? extends ObjectCodec<?>> getCodecsToRegister() {
if (supportsOptimizedAscii()) {
- builder.add(asciiOptimized());
+ return Collections.singletonList(asciiOptimized());
}
+ return Collections.emptyList();
}
}
}
diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/serialization/DeserializationContextTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/DeserializationContextTest.java
index ba99518e6f..b80f005467 100644
--- a/src/test/java/com/google/devtools/build/lib/skyframe/serialization/DeserializationContextTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/DeserializationContextTest.java
@@ -118,7 +118,7 @@ public class DeserializationContextTest {
@SuppressWarnings("unchecked")
ObjectCodec<Object> codec = Mockito.mock(ObjectCodec.class);
when(codec.getStrategy()).thenReturn(MemoizationStrategy.MEMOIZE_AFTER);
- when(codec.getEncodedClass()).thenReturn(Object.class);
+ when(codec.getEncodedClass()).thenAnswer(unused -> Object.class);
when(codec.additionalEncodedClasses()).thenReturn(ImmutableList.of());
ObjectCodecRegistry.CodecDescriptor codecDescriptor =
Mockito.mock(ObjectCodecRegistry.CodecDescriptor.class);