diff options
author | 2018-01-11 10:10:37 -0800 | |
---|---|---|
committer | 2018-01-11 10:12:40 -0800 | |
commit | c7667ae8d558dfc146e217bbf5e86954592234d1 (patch) | |
tree | bab52b543a7d41f005a4d94201824427f902f061 /src/main/java/com/google/devtools/build/lib/packages | |
parent | a4ac4a154da69927731f9d432913978f6e807a55 (diff) |
Adds a CODECs for Package and PackageValue.
PiperOrigin-RevId: 181624201
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/packages')
5 files changed, 192 insertions, 7 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/packages/Package.java b/src/main/java/com/google/devtools/build/lib/packages/Package.java index 1bceb6b23e..985dedfbd5 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/Package.java +++ b/src/main/java/com/google/devtools/build/lib/packages/Package.java @@ -35,11 +35,15 @@ import com.google.devtools.build.lib.events.ExtendedEventHandler.Postable; import com.google.devtools.build.lib.events.Location; import com.google.devtools.build.lib.packages.AttributeMap.AcceptsLabelAttribute; import com.google.devtools.build.lib.packages.License.DistributionType; +import com.google.devtools.build.lib.skyframe.serialization.InjectingObjectCodec; +import com.google.devtools.build.lib.skyframe.serialization.SerializationException; import com.google.devtools.build.lib.syntax.SkylarkSemantics; import com.google.devtools.build.lib.util.SpellChecker; import com.google.devtools.build.lib.vfs.Canonicalizer; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; +import com.google.protobuf.CodedInputStream; +import com.google.protobuf.CodedOutputStream; import java.io.IOException; import java.io.PrintStream; import java.util.ArrayList; @@ -53,16 +57,18 @@ import java.util.Set; import javax.annotation.Nullable; /** - * A package, which is a container of {@link Rule}s, each of - * which contains a dictionary of named attributes. + * A package, which is a container of {@link Rule}s, each of which contains a dictionary of named + * attributes. * - * <p>Package instances are intended to be immutable and for all practical - * purposes can be treated as such. Note, however, that some member variables - * exposed via the public interface are not strictly immutable, so until their - * types are guaranteed immutable we're not applying the {@code @Immutable} - * annotation here. + * <p>Package instances are intended to be immutable and for all practical purposes can be treated + * as such. Note, however, that some member variables exposed via the public interface are not + * strictly immutable, so until their types are guaranteed immutable we're not applying the + * {@code @Immutable} annotation here. */ +@SuppressWarnings("JavaLangClash") public class Package { + public static final InjectingObjectCodec<Package, PackageCodecDependencies> CODEC = + new PackageCodec(); /** * Common superclass for all name-conflict exceptions. @@ -1552,4 +1558,33 @@ public class Package { return message + ", defined at " + target.getLocation(); } } + + /** Package codec implementation. */ + private static final class PackageCodec + implements InjectingObjectCodec<Package, PackageCodecDependencies> { + @Override + public Class<Package> getEncodedClass() { + return Package.class; + } + + @Override + public void serialize( + PackageCodecDependencies codecDeps, Package input, CodedOutputStream codedOut) + throws IOException { + codecDeps.getPackageSerializer().serialize(input, codedOut); + } + + @Override + public Package deserialize(PackageCodecDependencies codecDeps, CodedInputStream codedIn) + throws SerializationException, IOException { + try { + return codecDeps.getPackageDeserializer().deserialize(codedIn); + } catch (PackageDeserializationException e) { + throw new SerializationException("Failed to deserialize Package", e); + } catch (InterruptedException e) { + throw new IllegalStateException( + "Unexpected InterruptedException during Package deserialization", e); + } + } + } } diff --git a/src/main/java/com/google/devtools/build/lib/packages/PackageCodecDependencies.java b/src/main/java/com/google/devtools/build/lib/packages/PackageCodecDependencies.java new file mode 100644 index 0000000000..52dc91b5d3 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/packages/PackageCodecDependencies.java @@ -0,0 +1,46 @@ +// 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.packages; + +/** Dependencies of {@value Package#Codec}. */ +public interface PackageCodecDependencies { + + PackageSerializerInterface getPackageSerializer(); + + PackageDeserializerInterface getPackageDeserializer(); + + /** Simplest implementation of PackageCodecDependencies. */ + public static class SimplePackageCodecDependencies implements PackageCodecDependencies { + private final PackageSerializerInterface packageSerializer; + private final PackageDeserializerInterface packageDeserializer; + + public SimplePackageCodecDependencies( + PackageSerializerInterface packageSerializer, + PackageDeserializerInterface packageDeserializer) { + this.packageSerializer = packageSerializer; + this.packageDeserializer = packageDeserializer; + } + + @Override + public PackageSerializerInterface getPackageSerializer() { + return packageSerializer; + } + + @Override + public PackageDeserializerInterface getPackageDeserializer() { + return packageDeserializer; + } + } +} diff --git a/src/main/java/com/google/devtools/build/lib/packages/PackageDeserializationException.java b/src/main/java/com/google/devtools/build/lib/packages/PackageDeserializationException.java new file mode 100644 index 0000000000..992c2aba28 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/packages/PackageDeserializationException.java @@ -0,0 +1,30 @@ +// 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.packages; + +/** Exception thrown when something goes wrong during package deserialization. */ +public class PackageDeserializationException extends Exception { + PackageDeserializationException(String message) { + super(message); + } + + PackageDeserializationException(String message, Exception reason) { + super(message, reason); + } + + PackageDeserializationException(Exception reason) { + super(reason); + } +} diff --git a/src/main/java/com/google/devtools/build/lib/packages/PackageDeserializerInterface.java b/src/main/java/com/google/devtools/build/lib/packages/PackageDeserializerInterface.java new file mode 100644 index 0000000000..ea74a4d682 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/packages/PackageDeserializerInterface.java @@ -0,0 +1,39 @@ +// 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.packages; + +import com.google.protobuf.CodedInputStream; +import java.io.IOException; + +/** + * Interface for Package deserialization. + * + * <p>Provides a layer of indirection for breaking circular dependencies. + */ +public interface PackageDeserializerInterface { + + /** + * Deserializes a {@link Package} from {@code codedIn}. The inverse of {@link + * PackageSerializer#serialize}. + * + * @param codedIn stream to read from + * @return a new {@link Package} as read from {@code codedIn} + * @throws PackageDeserializationException on failures deserializing the input + * @throws IOException on failures reading from {@code codedIn} + * @throws InterruptedException + */ + Package deserialize(CodedInputStream codedIn) + throws PackageDeserializationException, IOException, InterruptedException; +} diff --git a/src/main/java/com/google/devtools/build/lib/packages/PackageSerializerInterface.java b/src/main/java/com/google/devtools/build/lib/packages/PackageSerializerInterface.java new file mode 100644 index 0000000000..af25075cdc --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/packages/PackageSerializerInterface.java @@ -0,0 +1,35 @@ +// 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.packages; + +import com.google.protobuf.CodedOutputStream; +import java.io.IOException; + +/** + * Abstraction layer for Package serialization. + * + * <p>Provides a layer of indirection for breaking circular dependencies. + */ +public interface PackageSerializerInterface { + /** + * Serialize a package to {@code codedOut}. The inverse of {@link + * PackageDeserializer#deserialize}. + * + * @param pkg the {@link Package} to be serialized + * @param codedOut the stream to write {@code pkg}'s serialized representation to + * @throws IOException on failure writing to {@code codedOut} + */ + void serialize(Package pkg, CodedOutputStream codedOut) throws IOException; +} |