diff options
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/packages/PackageDeserializer.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/packages/PackageDeserializer.java | 170 |
1 files changed, 86 insertions, 84 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/packages/PackageDeserializer.java b/src/main/java/com/google/devtools/build/lib/packages/PackageDeserializer.java index d48c5e6bfe..3d4ab28cf6 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/PackageDeserializer.java +++ b/src/main/java/com/google/devtools/build/lib/packages/PackageDeserializer.java @@ -75,89 +75,103 @@ public class PackageDeserializer { // TODO(bazel-team): Subclass ObjectOutputStream to pass this through instead. public static volatile PackageDeserializationEnvironment defaultPackageDeserializationEnvironment; - private class Context { + /** Class encapsulating state for a single package deserialization. */ + private static class DeserializationContext { private final Package.Builder packageBuilder; private final PathFragment buildFilePath; - public Context(Path buildFilePath, Package.Builder packageBuilder) { + public DeserializationContext(Path buildFilePath, Package.Builder packageBuilder) { this.buildFilePath = buildFilePath.asFragment(); this.packageBuilder = packageBuilder; } + } - Location deserializeLocation(Build.Location location) { - return new ExplicitLocation(buildFilePath, location); - } + private final PackageDeserializationEnvironment packageDeserializationEnvironment; - ParsedAttributeValue deserializeAttribute(Type<?> expectedType, - Build.Attribute attrPb) - throws PackageDeserializationException { - Object value = deserializeAttributeValue(expectedType, attrPb); - return new ParsedAttributeValue( - attrPb.hasExplicitlySpecified() && attrPb.getExplicitlySpecified(), value, - deserializeLocation(attrPb.getParseableLocation())); - } + /** + * Creates a {@link PackageDeserializer} using {@link #defaultPackageDeserializationEnvironment}. + */ + public PackageDeserializer() { + this.packageDeserializationEnvironment = defaultPackageDeserializationEnvironment; + } - void deserializeInputFile(Build.SourceFile sourceFile) - throws PackageDeserializationException { - InputFile inputFile; - try { - inputFile = packageBuilder.createInputFile( - deserializeLabel(sourceFile.getName()).getName(), - deserializeLocation(sourceFile.getParseableLocation())); - } catch (GeneratedLabelConflict e) { - throw new PackageDeserializationException(e); - } + public PackageDeserializer(PackageDeserializationEnvironment packageDeserializationEnvironment) { + this.packageDeserializationEnvironment = + Preconditions.checkNotNull(packageDeserializationEnvironment); + } - if (!sourceFile.getVisibilityLabelList().isEmpty() || sourceFile.hasLicense()) { - packageBuilder.setVisibilityAndLicense(inputFile, - PackageFactory.getVisibility(deserializeLabels(sourceFile.getVisibilityLabelList())), - deserializeLicense(sourceFile.getLicense())); - } - } + private static Location deserializeLocation(DeserializationContext context, + Build.Location location) { + return new ExplicitLocation(context.buildFilePath, location); + } - void deserializePackageGroup(Build.PackageGroup packageGroupPb) - throws PackageDeserializationException { - List<String> specifications = new ArrayList<>(); - for (String containedPackage : packageGroupPb.getContainedPackageList()) { - specifications.add("//" + containedPackage); - } + private static ParsedAttributeValue deserializeAttribute(DeserializationContext context, + Type<?> expectedType, Build.Attribute attrPb) throws PackageDeserializationException { + Object value = deserializeAttributeValue(expectedType, attrPb); + return new ParsedAttributeValue( + attrPb.hasExplicitlySpecified() && attrPb.getExplicitlySpecified(), value, + deserializeLocation(context, attrPb.getParseableLocation())); + } - try { - packageBuilder.addPackageGroup( - deserializeLabel(packageGroupPb.getName()).getName(), - specifications, - deserializeLabels(packageGroupPb.getIncludedPackageGroupList()), - NullEventHandler.INSTANCE, // TODO(bazel-team): Handle errors properly - deserializeLocation(packageGroupPb.getParseableLocation())); - } catch (Label.SyntaxException | Package.NameConflictException e) { - throw new PackageDeserializationException(e); - } + private void deserializeInputFile(DeserializationContext context, Build.SourceFile sourceFile) + throws PackageDeserializationException { + InputFile inputFile; + try { + inputFile = context.packageBuilder.createInputFile( + deserializeLabel(sourceFile.getName()).getName(), + deserializeLocation(context, sourceFile.getParseableLocation())); + } catch (GeneratedLabelConflict e) { + throw new PackageDeserializationException(e); } - void deserializeRule(Build.Rule rulePb) throws PackageDeserializationException { - Location ruleLocation = deserializeLocation(rulePb.getParseableLocation()); - RuleClass ruleClass = packageDeserializationEnvironment.getRuleClass(rulePb, ruleLocation); - Map<String, ParsedAttributeValue> attributeValues = new HashMap<>(); - for (Build.Attribute attrPb : rulePb.getAttributeList()) { - Type<?> type = ruleClass.getAttributeByName(attrPb.getName()).getType(); - attributeValues.put(attrPb.getName(), deserializeAttribute(type, attrPb)); - } + if (!sourceFile.getVisibilityLabelList().isEmpty() || sourceFile.hasLicense()) { + context.packageBuilder.setVisibilityAndLicense(inputFile, + PackageFactory.getVisibility(deserializeLabels(sourceFile.getVisibilityLabelList())), + deserializeLicense(sourceFile.getLicense())); + } + } - Label ruleLabel = deserializeLabel(rulePb.getName()); - try { - Rule rule = ruleClass.createRuleWithParsedAttributeValues( - ruleLabel, packageBuilder, ruleLocation, attributeValues, - NullEventHandler.INSTANCE); - packageBuilder.addRule(rule); + private void deserializePackageGroup(DeserializationContext context, + Build.PackageGroup packageGroupPb) throws PackageDeserializationException { + List<String> specifications = new ArrayList<>(); + for (String containedPackage : packageGroupPb.getContainedPackageList()) { + specifications.add("//" + containedPackage); + } - Preconditions.checkState(!rule.containsErrors()); - } catch (NameConflictException | SyntaxException e) { - throw new PackageDeserializationException(e); - } + try { + context.packageBuilder.addPackageGroup( + deserializeLabel(packageGroupPb.getName()).getName(), + specifications, + deserializeLabels(packageGroupPb.getIncludedPackageGroupList()), + NullEventHandler.INSTANCE, // TODO(bazel-team): Handle errors properly + deserializeLocation(context, packageGroupPb.getParseableLocation())); + } catch (Label.SyntaxException | Package.NameConflictException e) { + throw new PackageDeserializationException(e); } } - private final PackageDeserializationEnvironment packageDeserializationEnvironment; + private void deserializeRule(DeserializationContext context, Build.Rule rulePb) + throws PackageDeserializationException { + Location ruleLocation = deserializeLocation(context, rulePb.getParseableLocation()); + RuleClass ruleClass = packageDeserializationEnvironment.getRuleClass(rulePb, ruleLocation); + Map<String, ParsedAttributeValue> attributeValues = new HashMap<>(); + for (Build.Attribute attrPb : rulePb.getAttributeList()) { + Type<?> type = ruleClass.getAttributeByName(attrPb.getName()).getType(); + attributeValues.put(attrPb.getName(), deserializeAttribute(context, type, attrPb)); + } + + Label ruleLabel = deserializeLabel(rulePb.getName()); + try { + Rule rule = ruleClass.createRuleWithParsedAttributeValues( + ruleLabel, context.packageBuilder, ruleLocation, attributeValues, + NullEventHandler.INSTANCE); + context.packageBuilder.addRule(rule); + + Preconditions.checkState(!rule.containsErrors()); + } catch (NameConflictException | SyntaxException e) { + throw new PackageDeserializationException(e); + } + } @Immutable private static final class ExplicitLocation extends Location { @@ -223,18 +237,6 @@ public class PackageDeserializer { } /** - * Creates a {@link PackageDeserializer} using {@link #defaultPackageDeserializationEnvironment}. - */ - public PackageDeserializer() { - this.packageDeserializationEnvironment = defaultPackageDeserializationEnvironment; - } - - public PackageDeserializer(PackageDeserializationEnvironment packageDeserializationEnvironment) { - this.packageDeserializationEnvironment = - Preconditions.checkNotNull(packageDeserializationEnvironment); - } - - /** * Exception thrown when something goes wrong during package deserialization. */ public static class PackageDeserializationException extends Exception { @@ -339,7 +341,7 @@ public class PackageDeserializer { Package.Builder builder, InputStream in) throws PackageDeserializationException, IOException { Path buildFile = packageDeserializationEnvironment.getPath(packagePb.getBuildFilePath()); Preconditions.checkNotNull(buildFile); - Context context = new Context(buildFile, builder); + DeserializationContext context = new DeserializationContext(buildFile, builder); builder.setFilename(buildFile); if (packagePb.hasDefaultVisibilitySet() && packagePb.getDefaultVisibilitySet()) { @@ -401,20 +403,20 @@ public class PackageDeserializer { deserializeTargets(in, context); } - private static void deserializeTargets(InputStream in, Context context) throws IOException, - PackageDeserializationException { + private void deserializeTargets(InputStream in, DeserializationContext context) + throws IOException, PackageDeserializationException { Build.TargetOrTerminator tot; while (!(tot = Build.TargetOrTerminator.parseDelimitedFrom(in)).getIsTerminator()) { Build.Target target = tot.getTarget(); switch (target.getType()) { case SOURCE_FILE: - context.deserializeInputFile(target.getSourceFile()); + deserializeInputFile(context, target.getSourceFile()); break; case PACKAGE_GROUP: - context.deserializePackageGroup(target.getPackageGroup()); + deserializePackageGroup(context, target.getPackageGroup()); break; case RULE: - context.deserializeRule(target.getRule()); + deserializeRule(context, target.getRule()); break; default: throw new IllegalStateException("Unexpected Target type: " + target.getType()); @@ -465,10 +467,10 @@ public class PackageDeserializer { } private static void deserializeEvent( - Context context, StoredEventHandler eventHandler, Build.Event event) { + DeserializationContext context, StoredEventHandler eventHandler, Build.Event event) { Location location = null; if (event.hasLocation()) { - location = context.deserializeLocation(event.getLocation()); + location = deserializeLocation(context, event.getLocation()); } String message = event.getMessage(); |